Skip to content
This repository

Use VersionAdmin.revision_manager rather than default_revision_manager #7

Merged
merged 4 commits into from almost 2 years ago

2 participants

Mark Lavin Jens Diemer
Mark Lavin
mlavin commented June 12, 2012

This change is to support using a non-default revision_manager on the CompareVersionAdmin. This is already support by the base VersionAdmin. Still needs some tests which I'm happy to write. Just wanted to get the discussion started to see if this seemed like a reasonable approach.

Jens Diemer
Owner
jedie commented June 12, 2012

I use only the default revision_manager, so i didn't need this, yet. But if it work and the unittests didn't fail i would merge this.

Mark Lavin
mlavin commented June 19, 2012

Rebased to the latest master and added tests.

Jens Diemer jedie merged commit af0b807 into from June 20, 2012
Jens Diemer jedie closed this June 20, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
10  reversion_compare/admin.py
@@ -21,7 +21,6 @@
21 21
 from django.utils.text import capfirst
22 22
 from django.utils.translation import ugettext as _
23 23
 
24  
-import reversion
25 24
 from reversion.admin import VersionAdmin
26 25
 from reversion.models import Version, VERSION_TYPE_CHOICES, VERSION_CHANGE, \
27 26
     has_int_pk
@@ -30,7 +29,6 @@
30 29
 from reversion_compare.helpers import html_diff, compare_queryset
31 30
 from django.conf import settings
32 31
 from django.contrib.contenttypes.models import ContentType
33  
-from reversion import get_adapter
34 32
 
35 33
 
36 34
 logger = logging.getLogger(__name__)
@@ -196,14 +194,14 @@ def debug(self):
196 194
 
197 195
 
198 196
 class CompareObjects(object):
199  
-    def __init__(self, field, field_name, obj, version1, version2):
  197
+    def __init__(self, field, field_name, obj, version1, version2, manager):
200 198
         self.field = field
201 199
         self.field_name = field_name
202 200
         self.obj = obj
203 201
 
204 202
         model = self.obj.__class__
205 203
         self.has_int_pk = has_int_pk(model)
206  
-        self.adapter = get_adapter(model) # VersionAdapter instance
  204
+        self.adapter = manager.get_adapter(model) # VersionAdapter instance
207 205
 
208 206
         # is a related field (ForeignKey, ManyToManyField etc.)
209 207
         self.is_related = self.field.rel is not None
@@ -572,7 +570,7 @@ def compare(self, obj, version1, version2):
572 570
             if self.compare_exclude and field_name in self.compare_exclude:
573 571
                 continue
574 572
 
575  
-            obj_compare = CompareObjects(field, field_name, obj, version1, version2)
  573
+            obj_compare = CompareObjects(field, field_name, obj, version1, version2, self.revision_manager)
576 574
             #obj_compare.debug()
577 575
 
578 576
             is_related = obj_compare.is_related
@@ -615,7 +613,7 @@ def compare_view(self, request, object_id, extra_context=None):
615 613
 
616 614
         object_id = unquote(object_id) # Underscores in primary key get quoted to "_5F"
617 615
         obj = get_object_or_404(self.model, pk=object_id)
618  
-        queryset = reversion.get_for_object(obj)
  616
+        queryset = self.revision_manager.get_for_object(obj)
619 617
         version1 = get_object_or_404(queryset, pk=version_id1)
620 618
         version2 = get_object_or_404(queryset, pk=version_id2)
621 619
 
75  reversion_compare/tests.py
@@ -27,6 +27,7 @@
27 27
 #    management.call_command("test", "reversion_compare.PersonPetModelTest", verbosity=2, traceback=True, interactive=False)
28 28
     sys.exit()
29 29
 
  30
+from django.core.urlresolvers import reverse
30 31
 from django.db.models.loading import get_models, get_app
31 32
 from django.test import TestCase
32 33
 from django.contrib.auth.models import User
@@ -50,10 +51,11 @@
50 51
 from reversion_compare import helpers
51 52
 
52 53
 from reversion_compare_test_project.reversion_compare_test_app.models import SimpleModel, Person, Pet, \
53  
-    Factory, Car, VariantModel
  54
+    Factory, Car, VariantModel, CustomModel
54 55
 
55 56
 # Needs to import admin module to register all models via CompareVersionAdmin/VersionAdmin
56 57
 import reversion_compare_test_project.reversion_compare_test_app.admin
  58
+from reversion_compare_test_project.reversion_compare_test_app.admin import custom_revision_manager
57 59
 
58 60
 
59 61
 class TestData(object):
@@ -221,6 +223,16 @@ def create_VariantModel_data(self):
221 223
             print "version 1:", item
222 224
 
223 225
         return item
  226
+        
  227
+    def create_CustomModel_data(self):
  228
+        with reversion.create_revision():
  229
+            item1 = CustomModel.objects.create(text="version one")
  230
+
  231
+        if self.verbose:
  232
+            print "version 1:", item1
  233
+
  234
+        return item1
  235
+
224 236
 
225 237
 class BaseTestCase(TestCase):
226 238
     def setUp(self):
@@ -274,7 +286,9 @@ def test_admin_login(self):
274 286
     def test_model_registering(self):
275 287
         test_app = get_app(app_label="reversion_compare_test_app")
276 288
         models = get_models(app_mod=test_app, include_auto_created=False, include_deferred=False, only_installed=True)
277  
-        self.assertEqual(len(reversion.get_registered_models()), len(models))
  289
+        default_registered = len(reversion.get_registered_models())
  290
+        custom_registered = len(custom_revision_manager.get_registered_models())
  291
+        self.assertEqual(default_registered + custom_registered, len(models))
278 292
 
279 293
 
280 294
 class SimpleModelTest(BaseTestCase):
@@ -648,4 +662,59 @@ def test_textfield(self):
648 662
 <ins>+nisi ut aliquip ex ea commodo consequat. Duis added aute irure dolor in reprehenderit in voluptate velit</ins>
649 663
 """)
650 664
         self.assertNotContains(response, "first line")
651  
-        self.assertNotContains(response, "last line")
  665
+        self.assertNotContains(response, "last line")
  666
+        
  667
+        
  668
+class CustomModelTest(BaseTestCase):
  669
+    "Test a model which uses a custom reversion manager."
  670
+    
  671
+    def setUp(self):
  672
+        super(CustomModelTest, self).setUp()
  673
+        test_data = TestData(verbose=False)
  674
+        self.item = test_data.create_CustomModel_data()
  675
+        
  676
+    def test_initial_state(self):
  677
+        "Test initial data creation and model registration."
  678
+        self.assertTrue(custom_revision_manager.is_registered(CustomModel))
  679
+        self.assertEqual(CustomModel.objects.count(), 1)
  680
+        self.assertEqual(custom_revision_manager.get_for_object(self.item).count(), 1)
  681
+        self.assertEqual(Revision.objects.all().count(), 1)
  682
+        
  683
+    def test_text_diff(self):
  684
+        "Generate a new revision and check for a correctly generated diff."
  685
+        with reversion.create_revision():
  686
+            self.item.text = "version two"
  687
+            self.item.save()
  688
+        queryset = custom_revision_manager.get_for_object(self.item)
  689
+        version_ids = queryset.values_list("pk", flat=True)
  690
+        self.assertEqual(len(version_ids), 2)
  691
+        url_name = 'admin:%s_%s_compare' % (CustomModel._meta.app_label, CustomModel._meta.module_name)
  692
+        diff_url = reverse(url_name, args=(self.item.pk, ))
  693
+        data = {"version_id2": version_ids[0], "version_id1": version_ids[1]}
  694
+        response = self.client.get(diff_url, data=data)
  695
+        self.assertContains(response, "<del>- version one</del>")
  696
+        self.assertContains(response, "<ins>+ version two</ins>")
  697
+        
  698
+    def test_version_selection(self):
  699
+        "Generate two revisions and view the version history selection."
  700
+        with reversion.create_revision():
  701
+            self.item.text = "version two"
  702
+            self.item.save()
  703
+        with reversion.create_revision():
  704
+            self.item.text = "version three"
  705
+            self.item.save()
  706
+        queryset = custom_revision_manager.get_for_object(self.item)
  707
+        version_ids = queryset.values_list("pk", flat=True)
  708
+        self.assertEqual(len(version_ids), 3)
  709
+        url_name = 'admin:%s_%s_history' % (CustomModel._meta.app_label, CustomModel._meta.module_name)
  710
+        history_url = reverse(url_name, args=(self.item.pk, ))
  711
+        response = self.client.get(history_url)
  712
+        self.assertContainsHtml(response,
  713
+            '<input type="submit" value="compare">',
  714
+            '<input type="radio" name="version_id1" value="%i" style="visibility:hidden" />' % version_ids[0],
  715
+            '<input type="radio" name="version_id2" value="%i" checked="checked" />' % version_ids[0],
  716
+            '<input type="radio" name="version_id1" value="%i" checked="checked" />' % version_ids[1],
  717
+            '<input type="radio" name="version_id2" value="%i" />' % version_ids[1],
  718
+            '<input type="radio" name="version_id1" value="%i" />' % version_ids[2],
  719
+            '<input type="radio" name="version_id2" value="%i" />' % version_ids[2],
  720
+        )
10  reversion_compare_test_project/reversion_compare_test_app/admin.py
@@ -19,9 +19,10 @@
19 19
 from reversion_compare.helpers import html_diff
20 20
 
21 21
 from reversion_compare_test_project.reversion_compare_test_app.models import SimpleModel, Factory, Car, Person, Pet,\
22  
-    VariantModel
  22
+    VariantModel, CustomModel
23 23
 
24 24
 from reversion.models import Revision, Version
  25
+from reversion.revisions import RevisionManager
25 26
 
26 27
 
27 28
 #------------------------------------------------------------------------------
@@ -77,6 +78,13 @@ class VariantModelAdmin(CompareVersionAdmin):
77 78
 admin.site.register(VariantModel, VariantModelAdmin)
78 79
 
79 80
 
  81
+custom_revision_manager = RevisionManager("custom")
  82
+
  83
+class CustomModelAdmin(CompareVersionAdmin):
  84
+    revision_manager = custom_revision_manager
  85
+admin.site.register(CustomModel, CustomModelAdmin)
  86
+
  87
+
80 88
 
81 89
 
82 90
 """
5  reversion_compare_test_project/reversion_compare_test_app/models.py
@@ -115,6 +115,11 @@ class VariantModel(models.Model):
115 115
 
116 116
 #------------------------------------------------------------------------------
117 117
 
  118
+
  119
+class CustomModel(models.Model):
  120
+    "Model which uses a custom version manager."
  121
+    text = models.TextField()
  122
+
118 123
 """
119 124
 
120 125
 class ParentModel(models.Model):
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.