Fix JSON serialization

This commit is contained in:
Yohann D'ANELLO 2020-03-11 00:41:37 +01:00
parent 6baf437eef
commit be877276f8
1 changed files with 22 additions and 10 deletions

View File

@ -2,9 +2,10 @@
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.core import serializers
from django.db.models.signals import pre_save, post_save, post_delete from django.db.models.signals import pre_save, post_save, post_delete
from django.dispatch import receiver from django.dispatch import receiver
from rest_framework.renderers import JSONRenderer
from rest_framework.serializers import ModelSerializer
import getpass import getpass
from note.models import NoteUser, Alias from note.models import NoteUser, Alias
@ -19,11 +20,10 @@ EXCLUDED = [
'cas_server.user', 'cas_server.user',
'cas_server.userattributes', 'cas_server.userattributes',
'contenttypes.contenttype', 'contenttypes.contenttype',
'logs.changelog', 'logs.changelog', # Never remove this line
'migrations.migration', 'migrations.migration',
'note.noteuser', 'note.note' # We only store the subclasses
'note.noteclub', 'note.transaction',
'note.notespecial',
'sessions.session', 'sessions.session',
] ]
@ -63,7 +63,7 @@ def save_object(sender, instance, **kwargs):
# IMPORTANT : l'utilisateur dans la VM doit être un des alias note du respo info # IMPORTANT : l'utilisateur dans la VM doit être un des alias note du respo info
ip = "127.0.0.1" ip = "127.0.0.1"
username = Alias.normalize(getpass.getuser()) username = Alias.normalize(getpass.getuser())
note = NoteUser.objects.filter(alias__normalized_name="^" + username + "$") note = NoteUser.objects.filter(alias__normalized_name=username)
if not note.exists(): if not note.exists():
print("WARNING: A model attempted to be saved in the DB, but the actor is unknown: " + username) print("WARNING: A model attempted to be saved in the DB, but the actor is unknown: " + username)
else: else:
@ -74,9 +74,14 @@ def save_object(sender, instance, **kwargs):
if instance.last_login != previous.last_login: if instance.last_login != previous.last_login:
return return
# Les modèles sont sauvegardés au format JSON # On crée notre propre sérialiseur JSON pour pouvoir sauvegarder les modèles
previous_json = serializers.serialize('json', [previous, ])[1:-1] if previous else None class CustomSerializer(ModelSerializer):
instance_json = serializers.serialize('json', [instance, ])[1:-1] class Meta:
model = instance.__class__
fields = '__all__'
previous_json = JSONRenderer().render(CustomSerializer(previous).data)
instance_json = JSONRenderer().render(CustomSerializer(instance).data)
if previous_json == instance_json: if previous_json == instance_json:
# Pas de log s'il n'y a pas de modification # Pas de log s'il n'y a pas de modification
@ -104,7 +109,14 @@ def delete_object(sender, instance, **kwargs):
# Si un utilisateur est connecté, on récupère l'utilisateur courant ainsi que son adresse IP # Si un utilisateur est connecté, on récupère l'utilisateur courant ainsi que son adresse IP
user, ip = get_current_authenticated_user(), get_current_ip() user, ip = get_current_authenticated_user(), get_current_ip()
instance_json = serializers.serialize('json', [instance, ])[1:-1] # On crée notre propre sérialiseur JSON pour pouvoir sauvegarder les modèles
class CustomSerializer(ModelSerializer):
class Meta:
model = instance.__class__
fields = '__all__'
instance_json = JSONRenderer().render(CustomSerializer(instance).data)
Changelog.objects.create(user=user, Changelog.objects.create(user=user,
ip=ip, ip=ip,
model=ContentType.objects.get_for_model(instance), model=ContentType.objects.get_for_model(instance),