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

Activate Versionning when using the REST API.

Squashing history by user added
Closes #11
parent 1c37ecc2
default_app_config = 'backend.apps.BackendConfig'
......@@ -3,3 +3,6 @@ from django.apps import AppConfig
class BackendConfig(AppConfig):
name = 'backend'
def ready(self):
import backend.signals # noqa: F401
from django.db import models
from rest_framework import serializers
from django.contrib.auth.models import User
from backend.signals import new_revision_saved
import reversion
class VersionnedModule(models.Model):
......@@ -23,5 +25,14 @@ class VersionnedModuleSerializer(serializers.ModelSerializer):
return None
def save(self, **kwargs):
self.validated_data['updated_by'] = self.context['request'].user
super(VersionnedModuleSerializer, self).save(**kwargs)
# Retrieve the user info from the request
user = self.context['request'].user
self.validated_data['updated_by'] = user
# Create a revision for the update or creation
with reversion.create_revision():
super(VersionnedModuleSerializer, self).save(**kwargs)
reversion.set_user(user)
# Signal save to perform concat of revisions
new_revision_saved.send(sender=self.__class__, obj=self.instance)
from reversion.models import Version
import django.dispatch
new_revision_saved = django.dispatch.Signal(providing_args=["obj"])
def squashRevisionByUser(sender, obj, **kwargs):
"""
TODO add comment and unit test
"""
versions = Version.objects\
.get_for_object(obj)\
.select_related('revision')\
.order_by('-revision__date_created')
last_edit = versions[0]
user_made_last_edit = last_edit.revision.user
if user_made_last_edit is not None:
for v in versions[1:]:
if v.revision.user == user_made_last_edit:
v.delete()
else:
break
new_revision_saved.connect(
squashRevisionByUser,
dispatch_uid="receiver_concat_revisions"
)
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