Commit be54f018 authored by Florent Chehab's avatar Florent Chehab

Tweaks, need to unit test this stuff

parent 9ebbc2f0
Pipeline #26535 passed with stages
in 1 minute and 37 seconds
# 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
from .photo import Photo # noqa: F401
from .driRestrictedModule import DriRestrictedModule, DriRestrictedModuleSerializer # 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
from django.db import models
from django.contrib.auth.models import User
from rest_framework import serializers
from backend.signals import new_revision_saved
import reversion
# from backend.signals import new_revision_saved
# import reversion
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.contrib.contenttypes.models import ContentType
......@@ -35,18 +35,13 @@ def is_member(group_name, user):
return user.groups.filter(name=group_name).exists()
def date_time_to_str(date):
s = serializers.DateTimeField()
class MyModelSerializer(serializers.ModelSerializer):
moderated_on = serializers.DateTimeField(
format="%Y-%m-%d %H:%M:%S", read_only=True)
updated_on = serializers.DateTimeField(
format="%Y-%m-%d %H:%M:%S", read_only=True)
# moderated_by = serializers.ReadOnlyField()
# updated_by = serializers.ReadOnlyField()
# TODO Changed
moderated_by = serializers.CharField(read_only=True)
updated_by = serializers.CharField(read_only=True)
updated_by_username = serializers.SerializerMethodField(read_only=True)
......@@ -85,21 +80,17 @@ class MyModelSerializer(serializers.ModelSerializer):
self.validated_data['updated_by'] = user
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):
self.validated_data['moderated_by'] = None
self.validated_data['moderated_on'] = None
self.validated_data['updated_by'] = 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):
self.clean_validated_data()
if self.moderation_required():
......@@ -110,47 +101,45 @@ class MyModelSerializer(serializers.ModelSerializer):
self.instance = super(
MyModelSerializer, self).save(**kwargs)
ct = ContentType.objects.get_for_model(self.Meta.model)
objs_pending_db = PendingModeration.objects.filter(
content_type=ct, object_id=self.instance.pk)
objs_pending_db = self.get_pending_models()
user = self.context['request'].user
self.clean_validated_data() # Make that it is done...
if len(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.save()
else:
pending_instance = PendingModeration(
referenced_object=self.instance,
author=user,
updated_by=user,
updated_on=timezone.now(),
new_object=self.validated_data
)
pending_instance.save()
pending_instance.save()
return self.instance
else:
print("Pas besoin de modération")
moderated_and_updated = True
if self.instance is None:
self.set_model_attr_no_moder(moderated_and_updated)
return super(MyModelSerializer, self).save(**kwargs)
else:
# We check if we don't have any moderation pending
moderated_and_updated = True
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))
objs_pending_db = self.get_pending_models()
if len(objs_pending_db) > 0:
# obj_pending_db = objs_pending_db[0]
# there should be only one
# TODO add checks to know if moderator did some modifications or not
# Change updated_by and on accordingly
# if obj_pending_db.new_object == self.validated_date
# moderated_and_updated = False
obj_pending_db = objs_pending_db[0]
self.clean_validated_data() # Make that it is done...
if obj_pending_db.new_object == self.validated_data:
moderated_and_updated = False
self.validated_data['updated_by'] = obj_pending_db.updated_by
self.validated_data['updated_on'] = obj_pending_db.updated_on
objs_pending_db.delete()
self.set_model_attr_no_moder(moderated_and_updated)
print('Pas besoin de modération')
return super(MyModelSerializer, self).save(**kwargs)
......
......@@ -11,7 +11,8 @@ class PendingModeration(models.Model):
object_id = models.CharField(max_length=100) # 100 should be ok
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={})
......
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.tools import NoDeleteIfNotAdmin
from backend.models.module import MyModelSerializer
......
Markdown is supported
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