Commit 73f73809 authored by Florent Chehab's avatar Florent Chehab

Versionning corrected and bug regarding model moderation on versionnedModel corrected

parent dbf4e75c
Pipeline #26660 passed with stages
in 2 minutes and 58 seconds
......@@ -53,6 +53,7 @@ CLASSIC_MODELS = [
Country,
City,
Currency,
University,
Department,
Offer,
Semester,
......@@ -66,7 +67,6 @@ CLASSIC_MODELS = [
]
VERSIONNED_MODELS = [
University,
Campus,
UniversityScholarship,
UniversityInfo,
......
......@@ -11,6 +11,9 @@ class CityTransport(BasicModule):
city = models.OneToOneField(
City, on_delete=models.CASCADE, related_name='city_transport', primary_key=True)
def get_serializer(self):
return CityTransportSerializer
class CityTransportSerializer(BasicModuleSerializer):
class Meta:
......
......@@ -3,5 +3,5 @@ from .myModelSerializer import MyModelSerializer # noqa: F401
from .myModelViewSet import MyModelViewSet # noqa: F401
from .pendingModeration import PendingModeration, PendingModerationSerializer, PendingModerationViewSet # noqa: F401
from .forTestingModeration import ForTestingModeration, ForTestingModerationSerializer, ForTestingModerationViewSet # noqa: F401
from .myModelVersionned import MyModelVersionned, MyModelVersionnedSerializer, MyModelVersionnedViewSet # noqa: F401
from .myModelVersionned import MyModelVersionned, MyModelVersionnedSerializer, MyModelVersionnedViewSet, VersionViewSet # noqa: F401
from .forTestingVersionning import ForTestingVersionning, ForTestingVersionningSerializer, ForTestingVersionningViewSet # noqa: F401
......@@ -10,6 +10,9 @@ class ForTestingVersionning(MyModelVersionned):
"""
bbb = models.CharField(max_length=100)
def get_serializer(self):
return ForTestingVersionningSerializer
class ForTestingVersionningSerializer(MyModelVersionnedSerializer):
"""
......
......@@ -15,7 +15,8 @@ class MyModelSerializer(serializers.ModelSerializer):
format="%Y-%m-%d %H:%M:%S", read_only=True)
moderated_by = serializers.CharField(read_only=True)
updated_by = serializers.CharField(read_only=True)
pending_moderation = PendingModerationSerializer(many=True, read_only=True)
pending_moderation = serializers.SerializerMethodField()
# pending_moderation = PendingModerationSerializer(many=True, read_only=True)
# TODO : updated_by_username useless ? See in rest API
updated_by_username = serializers.SerializerMethodField(read_only=True)
......@@ -26,6 +27,12 @@ class MyModelSerializer(serializers.ModelSerializer):
else:
return None
def get_pending_moderation(self, obj):
if self.context['view'].action != 'list':
print(self.context['view'].action)
return PendingModerationSerializer(obj.pending_moderation, many=True, read_only=True, context=self.context).data
return None
class Meta:
model = MyModel
......@@ -86,6 +93,9 @@ class MyModelSerializer(serializers.ModelSerializer):
pass
def save(self, **kwargs):
return self.my_save(**kwargs)
def my_save(self, **kwargs):
user = self.get_user_in_request()
self.clean_validated_data()
self.my_pre_save()
......
from backend.models.my_model import MyModel, MyModelSerializer, MyModelViewSet
from backend.signals import new_revision_saved
from rest_framework import serializers, permissions, mixins, viewsets
import reversion
from reversion.models import Version
from django.contrib.contenttypes.models import ContentType
from django.core.serializers.base import DeserializationError
from django.core import serializers as djangoSerializers
class MyModelVersionned(MyModel):
def get_serializer(self):
"""
This function is required for handling
versionning easily.
You have to put the correct value in each subclass
"""
return None
class Meta:
abstract = True
class MyModelVersionnedSerializer(MyModelSerializer):
nb_versions = serializers.SerializerMethodField()
content_type_id = serializers.SerializerMethodField()
def get_nb_versions(self, obj):
if self.context['view'].action != 'list':
versions = Version.objects.get_for_object(obj)
return len(versions)
return None
def get_content_type_id(self, obj):
return ContentType.objects.get_for_model(self.Meta.model).id
def save(self, **kwargs):
# Retrieve the user info from the request
user = self.context['request'].user
res = None
with reversion.create_revision():
super(MyModelSerializer, self).save(**kwargs)
res = self.my_save(**kwargs)
reversion.set_user(user)
# Signal save to perform squash of revisions
new_revision_saved.send(sender=self.__class__, obj=self.instance)
return res
class MyModelVersionnedViewSet(MyModelViewSet):
serializer_class = MyModelVersionnedSerializer
class VersionSerializer(serializers.ModelSerializer):
# serialized_data = serializers.JSONField()
data = serializers.SerializerMethodField()
def get_data(self, obj):
data = obj.serialized_data
try:
tmp = list(djangoSerializers.deserialize(
obj.format, data, ignorenonexistent=True))[0]
print(tmp.object)
# Version is valid,
obj_serializer = tmp.object.get_serializer()
if obj_serializer is None:
return "THE PROGRAMMER FORGOT TO SET THE SERIALIZER IN THE MODEL"
else:
new_context = dict(self.context)
new_context['view'].action = 'list'
return obj_serializer(tmp.object, context=new_context).data
except (DeserializationError, djangoSerializers.SerializerDoesNotExist):
obj.delete() # The version is not valid regarding the model, we should delete it !
# Might result in inconsistent nb of versions but that's fine.
return None
class Meta:
model = Version
fields = ('data', 'id')
class VersionViewSet(mixins.ListModelMixin,
viewsets.GenericViewSet): # TODO better presentation
permission_classes = (permissions.IsAuthenticated,)
serializer_class = VersionSerializer
def get_queryset(self):
content_type_id = self.kwargs['content_type_id']
object_pk = self.kwargs['object_pk']
ct = ContentType.objects.get_for_id(content_type_id)
model = ct.model_class()
obj = model.objects.get(pk=object_pk)
return Version.objects.get_for_object(obj)
# queryset = get_queryset()
......@@ -53,6 +53,7 @@ from backend.models.user import PreviousDepartureFeedbackViewSet
from backend.models.user import UserDataViewSet
from backend.models.my_model import PendingModerationViewSet
from backend.models.my_model import VersionViewSet
from rest_framework.documentation import include_docs_urls
......@@ -134,5 +135,7 @@ router.register(r'user/recommendations_list', RecommendationListViewSet)
router.register(r'user/data', UserDataViewSet, "user_data-detail")
router.register(r'pending_moderation', PendingModerationViewSet)
router.register(
r'version/(?P<content_type_id>[0-9]+)/(?P<object_pk>[0-9A-Za-z]+)', VersionViewSet, "versions-list")
urlpatterns += [url(r'^api/', include(router.urls))]
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