Commit be54f018 authored by Florent Chehab's avatar Florent Chehab
Browse files

Tweaks, need to unit test this stuff

parent 9ebbc2f0
# Generated by Django 2.0.3 on 2018-08-24 20:51
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('backend', '0009_auto_20180824_2218'),
]
operations = [
migrations.RenameField(
model_name='pendingmoderation',
old_name='author',
new_name='updated_by',
),
migrations.AddField(
model_name='pendingmoderation',
name='updated_on',
field=models.DateTimeField(null=True),
),
]
...@@ -4,5 +4,5 @@ from .scholarship import Scholarship # noqa: F401 ...@@ -4,5 +4,5 @@ from .scholarship import Scholarship # noqa: F401
from .photo import Photo # noqa: F401 from .photo import Photo # noqa: F401
from .driRestrictedModule import DriRestrictedModule, DriRestrictedModuleSerializer # noqa: F401 from .driRestrictedModule import DriRestrictedModule, DriRestrictedModuleSerializer # noqa: F401
from .currency import Currency, CurrencyViewSet, CurrencySerializer # noqa: F401 from .currency import Currency, CurrencyViewSet, CurrencySerializer # noqa: F401
from .myModel import MyModel, MyModelSerializer from .myModel import MyModel, MyModelSerializer # noqa: F401
# MyModelVersionned, MyModelVersionnedSerializer # noqa: F401 # MyModelVersionned, MyModelVersionnedSerializer # noqa: F401
from django.db import models from django.db import models
from django.contrib.auth.models import User from django.contrib.auth.models import User
from rest_framework import serializers from rest_framework import serializers
from backend.signals import new_revision_saved # from backend.signals import new_revision_saved
import reversion # import reversion
from django.utils import timezone from django.utils import timezone
from backend.models.tools import PendingModeration, PendingModerationSerializer from backend.models.tools import PendingModeration
from django.conf import settings from django.conf import settings
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
...@@ -35,18 +35,13 @@ def is_member(group_name, user): ...@@ -35,18 +35,13 @@ def is_member(group_name, user):
return user.groups.filter(name=group_name).exists() return user.groups.filter(name=group_name).exists()
def date_time_to_str(date):
s = serializers.DateTimeField()
class MyModelSerializer(serializers.ModelSerializer): class MyModelSerializer(serializers.ModelSerializer):
moderated_on = serializers.DateTimeField( moderated_on = serializers.DateTimeField(
format="%Y-%m-%d %H:%M:%S", read_only=True) format="%Y-%m-%d %H:%M:%S", read_only=True)
updated_on = serializers.DateTimeField( updated_on = serializers.DateTimeField(
format="%Y-%m-%d %H:%M:%S", read_only=True) format="%Y-%m-%d %H:%M:%S", read_only=True)
# moderated_by = serializers.ReadOnlyField() moderated_by = serializers.CharField(read_only=True)
# updated_by = serializers.ReadOnlyField() updated_by = serializers.CharField(read_only=True)
# TODO Changed
updated_by_username = serializers.SerializerMethodField(read_only=True) updated_by_username = serializers.SerializerMethodField(read_only=True)
...@@ -85,21 +80,17 @@ class MyModelSerializer(serializers.ModelSerializer): ...@@ -85,21 +80,17 @@ class MyModelSerializer(serializers.ModelSerializer):
self.validated_data['updated_by'] = user self.validated_data['updated_by'] = user
self.validated_data['updated_on'] = now self.validated_data['updated_on'] = now
def set_model_attr_for_moder(self):
user = self.context['request'].user
self.validated_data['updated_by'] = user.pk
self.validated_data['updated_on'] = timezone.now().strftime(
"%Y-%m-%d %H:%M:%S")
# also return the user because it's usefull...
return user
def clean_validated_data(self): def clean_validated_data(self):
self.validated_data['moderated_by'] = None self.validated_data['moderated_by'] = None
self.validated_data['moderated_on'] = None self.validated_data['moderated_on'] = None
self.validated_data['updated_by'] = None self.validated_data['updated_by'] = None
self.validated_data['updated_on'] = None self.validated_data['updated_on'] = None
def get_pending_models(self):
ct = ContentType.objects.get_for_model(self.Meta.model)
return PendingModeration.objects.filter(
content_type=ct, object_id=self.instance.pk)
def save(self, **kwargs): def save(self, **kwargs):
self.clean_validated_data() self.clean_validated_data()
if self.moderation_required(): if self.moderation_required():
...@@ -110,47 +101,45 @@ class MyModelSerializer(serializers.ModelSerializer): ...@@ -110,47 +101,45 @@ class MyModelSerializer(serializers.ModelSerializer):
self.instance = super( self.instance = super(
MyModelSerializer, self).save(**kwargs) MyModelSerializer, self).save(**kwargs)
ct = ContentType.objects.get_for_model(self.Meta.model) objs_pending_db = self.get_pending_models()
objs_pending_db = PendingModeration.objects.filter( user = self.context['request'].user
content_type=ct, object_id=self.instance.pk)
self.clean_validated_data() # Make that it is done...
if len(objs_pending_db) > 0: if len(objs_pending_db) > 0:
pending_instance = objs_pending_db[0] pending_instance = objs_pending_db[0]
pending_instance.updated_on = timezone.now()
pending_instance.updated_by = user
pending_instance.new_object = self.validated_data pending_instance.new_object = self.validated_data
pending_instance.save()
else: else:
pending_instance = PendingModeration( pending_instance = PendingModeration(
referenced_object=self.instance, referenced_object=self.instance,
author=user, updated_by=user,
updated_on=timezone.now(),
new_object=self.validated_data new_object=self.validated_data
) )
pending_instance.save() pending_instance.save()
return self.instance return self.instance
else: else:
print("Pas besoin de modération") print("Pas besoin de modération")
moderated_and_updated = True
if self.instance is None: if self.instance is None:
self.set_model_attr_no_moder(moderated_and_updated)
return super(MyModelSerializer, self).save(**kwargs) return super(MyModelSerializer, self).save(**kwargs)
else: else:
# We check if we don't have any moderation pending # We check if we don't have any moderation pending
moderated_and_updated = True objs_pending_db = self.get_pending_models()
print(self.Meta.model)
ct = ContentType.objects.get_for_model(self.Meta.model)
objs_pending_db = PendingModeration.objects.filter(
content_type=ct, object_id=self.instance.pk)
print(len(objs_pending_db))
if len(objs_pending_db) > 0: if len(objs_pending_db) > 0:
# obj_pending_db = objs_pending_db[0] obj_pending_db = objs_pending_db[0]
# there should be only one self.clean_validated_data() # Make that it is done...
# TODO add checks to know if moderator did some modifications or not if obj_pending_db.new_object == self.validated_data:
# Change updated_by and on accordingly moderated_and_updated = False
# if obj_pending_db.new_object == self.validated_date self.validated_data['updated_by'] = obj_pending_db.updated_by
# moderated_and_updated = False self.validated_data['updated_on'] = obj_pending_db.updated_on
objs_pending_db.delete() objs_pending_db.delete()
self.set_model_attr_no_moder(moderated_and_updated) self.set_model_attr_no_moder(moderated_and_updated)
print('Pas besoin de modération')
return super(MyModelSerializer, self).save(**kwargs) return super(MyModelSerializer, self).save(**kwargs)
......
...@@ -11,7 +11,8 @@ class PendingModeration(models.Model): ...@@ -11,7 +11,8 @@ class PendingModeration(models.Model):
object_id = models.CharField(max_length=100) # 100 should be ok object_id = models.CharField(max_length=100) # 100 should be ok
referenced_object = GenericForeignKey('content_type', 'object_id') referenced_object = GenericForeignKey('content_type', 'object_id')
author = models.ForeignKey(User, on_delete=models.CASCADE) updated_by = models.ForeignKey(User, on_delete=models.CASCADE)
updated_on = models.DateTimeField(null=True)
new_object = JSONField(default={}) new_object = JSONField(default={})
......
from backend.models.university import University from backend.models.university import University
from rest_framework import serializers, viewsets, permissions from rest_framework import viewsets, permissions
from backend.models.university.campus import CampusSerializer from backend.models.university.campus import CampusSerializer
from backend.models.tools import NoDeleteIfNotAdmin from backend.models.tools import NoDeleteIfNotAdmin
from backend.models.module import MyModelSerializer from backend.models.module import MyModelSerializer
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment