Commit 16f0fb7d authored by Florent Chehab's avatar Florent Chehab

Added nb_versions field, auto compute number of versions and tests #55

parent 30b5034c
# Generated by Django 2.1.7 on 2019-03-10 16:26
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('backend_app', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='campus',
name='nb_version',
field=models.PositiveIntegerField(default=0),
),
migrations.AddField(
model_name='campustaggeditem',
name='nb_version',
field=models.PositiveIntegerField(default=0),
),
migrations.AddField(
model_name='citytaggeditem',
name='nb_version',
field=models.PositiveIntegerField(default=0),
),
migrations.AddField(
model_name='countrydri',
name='nb_version',
field=models.PositiveIntegerField(default=0),
),
migrations.AddField(
model_name='countryscholarship',
name='nb_version',
field=models.PositiveIntegerField(default=0),
),
migrations.AddField(
model_name='countrytaggeditem',
name='nb_version',
field=models.PositiveIntegerField(default=0),
),
migrations.AddField(
model_name='fortestingversioning',
name='nb_version',
field=models.PositiveIntegerField(default=0),
),
migrations.AddField(
model_name='universitydri',
name='nb_version',
field=models.PositiveIntegerField(default=0),
),
migrations.AddField(
model_name='universityinfo',
name='nb_version',
field=models.PositiveIntegerField(default=0),
),
migrations.AddField(
model_name='universityscholarship',
name='nb_version',
field=models.PositiveIntegerField(default=0),
),
migrations.AddField(
model_name='universitysemestersdates',
name='nb_version',
field=models.PositiveIntegerField(default=0),
),
migrations.AddField(
model_name='universitytaggeditem',
name='nb_version',
field=models.PositiveIntegerField(default=0),
),
]
# Generated by Django 2.1.7 on 2019-03-10 16:31
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('backend_app', '0002_auto_20190310_1726'),
]
operations = [
migrations.RenameField(
model_name='campus',
old_name='nb_version',
new_name='nb_versions',
),
migrations.RenameField(
model_name='campustaggeditem',
old_name='nb_version',
new_name='nb_versions',
),
migrations.RenameField(
model_name='citytaggeditem',
old_name='nb_version',
new_name='nb_versions',
),
migrations.RenameField(
model_name='countrydri',
old_name='nb_version',
new_name='nb_versions',
),
migrations.RenameField(
model_name='countryscholarship',
old_name='nb_version',
new_name='nb_versions',
),
migrations.RenameField(
model_name='countrytaggeditem',
old_name='nb_version',
new_name='nb_versions',
),
migrations.RenameField(
model_name='fortestingversioning',
old_name='nb_version',
new_name='nb_versions',
),
migrations.RenameField(
model_name='universitydri',
old_name='nb_version',
new_name='nb_versions',
),
migrations.RenameField(
model_name='universityinfo',
old_name='nb_version',
new_name='nb_versions',
),
migrations.RenameField(
model_name='universityscholarship',
old_name='nb_version',
new_name='nb_versions',
),
migrations.RenameField(
model_name='universitysemestersdates',
old_name='nb_version',
new_name='nb_versions',
),
migrations.RenameField(
model_name='universitytaggeditem',
old_name='nb_version',
new_name='nb_versions',
),
]
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.core import serializers as djangoSerializers from django.core import serializers as djangoSerializers
from django.core.serializers.base import DeserializationError from django.core.serializers.base import DeserializationError
from django.db import models
import reversion import reversion
from backend_app.custom import MySerializerWithJSON from backend_app.custom import MySerializerWithJSON
...@@ -20,6 +21,9 @@ class MyModelVersionned(MyModel): ...@@ -20,6 +21,9 @@ class MyModelVersionned(MyModel):
Custom MyModel that will be versionned in the app Custom MyModel that will be versionned in the app
""" """
# We store the current number of versions for better performance
nb_versions = models.PositiveIntegerField(default=0)
@classmethod @classmethod
def get_serializer(cls): def get_serializer(cls):
""" """
...@@ -39,20 +43,11 @@ class MyModelVersionnedSerializer(MyModelSerializer): ...@@ -39,20 +43,11 @@ class MyModelVersionnedSerializer(MyModelSerializer):
""" """
# Add a nb_versions field # Add a nb_versions field
nb_versions = serializers.SerializerMethodField() nb_versions = serializers.IntegerField(read_only=True)
# Add a content_type_id field to be able to find versions # Add a content_type_id field to be able to find versions
content_type_id = serializers.SerializerMethodField() content_type_id = serializers.SerializerMethodField()
def get_nb_versions(self, obj):
"""
Serializer for the nb_version field
With a bit of optimization
"""
if self.FORCE_FULL_DISPLAY or self.context["view"].action != "list":
versions = Version.objects.get_for_object(obj)
return len(versions)
return None
def get_content_type_id(self, obj): def get_content_type_id(self, obj):
""" """
Serializer for content type Serializer for content type
......
...@@ -24,6 +24,11 @@ def squash_revision_by_user(sender, obj, **kwargs): ...@@ -24,6 +24,11 @@ def squash_revision_by_user(sender, obj, **kwargs):
else: else:
break break
# We update the number of versions directly here
# So that it doesn't have to be recomputed every time
obj.nb_versions = len(Version.objects.get_for_object(obj))
obj.save()
new_revision_saved.connect( new_revision_saved.connect(
squash_revision_by_user, dispatch_uid="receiver_concat_revisions" squash_revision_by_user, dispatch_uid="receiver_concat_revisions"
......
...@@ -27,6 +27,7 @@ class SquashVersionsTestCase(WithUserTestCase): ...@@ -27,6 +27,7 @@ class SquashVersionsTestCase(WithUserTestCase):
""" """
Test to check that when two different users successively Test to check that when two different users successively
Modify a model, no squashing is performed Modify a model, no squashing is performed
nb_versions is incremented
""" """
with reversion.create_revision(): with reversion.create_revision():
self.obj.bbb = "v2 other user" self.obj.bbb = "v2 other user"
...@@ -37,6 +38,7 @@ class SquashVersionsTestCase(WithUserTestCase): ...@@ -37,6 +38,7 @@ class SquashVersionsTestCase(WithUserTestCase):
versions = self.get_versions(self.obj) versions = self.get_versions(self.obj)
self.assertEqual(len(versions), 2) self.assertEqual(len(versions), 2)
self.assertEqual(len(versions), self.obj.nb_versions)
first_edit = self.get_version_data(versions[1]) first_edit = self.get_version_data(versions[1])
second_edit = self.get_version_data(versions[0]) second_edit = self.get_version_data(versions[0])
...@@ -46,7 +48,8 @@ class SquashVersionsTestCase(WithUserTestCase): ...@@ -46,7 +48,8 @@ class SquashVersionsTestCase(WithUserTestCase):
def test_squashing(self): def test_squashing(self):
""" """
Test to check that when a user save two different Test to check that when a user save two different
versions of a same model, no squashing is performed versions of a same model, squashing is performed
nb_versions is not incremented
""" """
with reversion.create_revision(): with reversion.create_revision():
self.obj.bbb = "v2" self.obj.bbb = "v2"
...@@ -57,5 +60,6 @@ class SquashVersionsTestCase(WithUserTestCase): ...@@ -57,5 +60,6 @@ class SquashVersionsTestCase(WithUserTestCase):
versions = self.get_versions(self.obj) versions = self.get_versions(self.obj)
self.assertEqual(len(versions), 1) self.assertEqual(len(versions), 1)
self.assertEqual(len(versions), self.obj.nb_versions)
version_data = self.get_version_data(versions[0]) version_data = self.get_version_data(versions[0])
self.assertEqual(version_data["bbb"], "v2") self.assertEqual(version_data["bbb"], "v2")
...@@ -37,6 +37,7 @@ class VersioningTestCase(WithUserTestCase): ...@@ -37,6 +37,7 @@ class VersioningTestCase(WithUserTestCase):
""" """
Test to check that versioning is working Test to check that versioning is working
We also check that new_revision_saved is called We also check that new_revision_saved is called
We also check that the number of versions in the field is incremented
""" """
def _test_signal_sent(sender, obj, **kwargs): def _test_signal_sent(sender, obj, **kwargs):
...@@ -53,6 +54,7 @@ class VersioningTestCase(WithUserTestCase): ...@@ -53,6 +54,7 @@ class VersioningTestCase(WithUserTestCase):
versions = Version.objects.get_for_object(instance) versions = Version.objects.get_for_object(instance)
self.assertEqual(len(versions), 1) self.assertEqual(len(versions), 1)
self.assertEqual(len(versions), instance.nb_versions)
self.assertTrue(self.signal_was_called) self.assertTrue(self.signal_was_called)
self.reset_signal_called() self.reset_signal_called()
...@@ -62,6 +64,8 @@ class VersioningTestCase(WithUserTestCase): ...@@ -62,6 +64,8 @@ class VersioningTestCase(WithUserTestCase):
) )
self.assertEqual(response.status_code, 200) self.assertEqual(response.status_code, 200)
instance = self._test_retreive_instance(data_2)
versions = Version.objects.get_for_object(instance) versions = Version.objects.get_for_object(instance)
self.assertEqual(len(versions), 2) self.assertEqual(len(versions), 2)
self.assertEqual(len(versions), instance.nb_versions)
self.assertTrue(self.signal_was_called) self.assertTrue(self.signal_was_called)
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