Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Use VersionAdmin.revision_manager rather than default_revision_manager #7

Merged
merged 4 commits into from

2 participants

Mark Lavin Jens Diemer
Mark Lavin

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

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

Rebased to the latest master and added tests.

Jens Diemer jedie merged commit af0b807 into from
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
View
@@ -21,7 +21,6 @@
from django.utils.text import capfirst
from django.utils.translation import ugettext as _
-import reversion
from reversion.admin import VersionAdmin
from reversion.models import Version, VERSION_TYPE_CHOICES, VERSION_CHANGE, \
has_int_pk
@@ -30,7 +29,6 @@
from reversion_compare.helpers import html_diff, compare_queryset
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
-from reversion import get_adapter
logger = logging.getLogger(__name__)
@@ -196,14 +194,14 @@ def debug(self):
class CompareObjects(object):
- def __init__(self, field, field_name, obj, version1, version2):
+ def __init__(self, field, field_name, obj, version1, version2, manager):
self.field = field
self.field_name = field_name
self.obj = obj
model = self.obj.__class__
self.has_int_pk = has_int_pk(model)
- self.adapter = get_adapter(model) # VersionAdapter instance
+ self.adapter = manager.get_adapter(model) # VersionAdapter instance
# is a related field (ForeignKey, ManyToManyField etc.)
self.is_related = self.field.rel is not None
@@ -572,7 +570,7 @@ def compare(self, obj, version1, version2):
if self.compare_exclude and field_name in self.compare_exclude:
continue
- obj_compare = CompareObjects(field, field_name, obj, version1, version2)
+ obj_compare = CompareObjects(field, field_name, obj, version1, version2, self.revision_manager)
#obj_compare.debug()
is_related = obj_compare.is_related
@@ -615,7 +613,7 @@ def compare_view(self, request, object_id, extra_context=None):
object_id = unquote(object_id) # Underscores in primary key get quoted to "_5F"
obj = get_object_or_404(self.model, pk=object_id)
- queryset = reversion.get_for_object(obj)
+ queryset = self.revision_manager.get_for_object(obj)
version1 = get_object_or_404(queryset, pk=version_id1)
version2 = get_object_or_404(queryset, pk=version_id2)
75 reversion_compare/tests.py
View
@@ -27,6 +27,7 @@
# management.call_command("test", "reversion_compare.PersonPetModelTest", verbosity=2, traceback=True, interactive=False)
sys.exit()
+from django.core.urlresolvers import reverse
from django.db.models.loading import get_models, get_app
from django.test import TestCase
from django.contrib.auth.models import User
@@ -50,10 +51,11 @@
from reversion_compare import helpers
from reversion_compare_test_project.reversion_compare_test_app.models import SimpleModel, Person, Pet, \
- Factory, Car, VariantModel
+ Factory, Car, VariantModel, CustomModel
# Needs to import admin module to register all models via CompareVersionAdmin/VersionAdmin
import reversion_compare_test_project.reversion_compare_test_app.admin
+from reversion_compare_test_project.reversion_compare_test_app.admin import custom_revision_manager
class TestData(object):
@@ -221,6 +223,16 @@ def create_VariantModel_data(self):
print "version 1:", item
return item
+
+ def create_CustomModel_data(self):
+ with reversion.create_revision():
+ item1 = CustomModel.objects.create(text="version one")
+
+ if self.verbose:
+ print "version 1:", item1
+
+ return item1
+
class BaseTestCase(TestCase):
def setUp(self):
@@ -274,7 +286,9 @@ def test_admin_login(self):
def test_model_registering(self):
test_app = get_app(app_label="reversion_compare_test_app")
models = get_models(app_mod=test_app, include_auto_created=False, include_deferred=False, only_installed=True)
- self.assertEqual(len(reversion.get_registered_models()), len(models))
+ default_registered = len(reversion.get_registered_models())
+ custom_registered = len(custom_revision_manager.get_registered_models())
+ self.assertEqual(default_registered + custom_registered, len(models))
class SimpleModelTest(BaseTestCase):
@@ -648,4 +662,59 @@ def test_textfield(self):
<ins>+nisi ut aliquip ex ea commodo consequat. Duis added aute irure dolor in reprehenderit in voluptate velit</ins>
""")
self.assertNotContains(response, "first line")
- self.assertNotContains(response, "last line")
+ self.assertNotContains(response, "last line")
+
+
+class CustomModelTest(BaseTestCase):
+ "Test a model which uses a custom reversion manager."
+
+ def setUp(self):
+ super(CustomModelTest, self).setUp()
+ test_data = TestData(verbose=False)
+ self.item = test_data.create_CustomModel_data()
+
+ def test_initial_state(self):
+ "Test initial data creation and model registration."
+ self.assertTrue(custom_revision_manager.is_registered(CustomModel))
+ self.assertEqual(CustomModel.objects.count(), 1)
+ self.assertEqual(custom_revision_manager.get_for_object(self.item).count(), 1)
+ self.assertEqual(Revision.objects.all().count(), 1)
+
+ def test_text_diff(self):
+ "Generate a new revision and check for a correctly generated diff."
+ with reversion.create_revision():
+ self.item.text = "version two"
+ self.item.save()
+ queryset = custom_revision_manager.get_for_object(self.item)
+ version_ids = queryset.values_list("pk", flat=True)
+ self.assertEqual(len(version_ids), 2)
+ url_name = 'admin:%s_%s_compare' % (CustomModel._meta.app_label, CustomModel._meta.module_name)
+ diff_url = reverse(url_name, args=(self.item.pk, ))
+ data = {"version_id2": version_ids[0], "version_id1": version_ids[1]}
+ response = self.client.get(diff_url, data=data)
+ self.assertContains(response, "<del>- version one</del>")
+ self.assertContains(response, "<ins>+ version two</ins>")
+
+ def test_version_selection(self):
+ "Generate two revisions and view the version history selection."
+ with reversion.create_revision():
+ self.item.text = "version two"
+ self.item.save()
+ with reversion.create_revision():
+ self.item.text = "version three"
+ self.item.save()
+ queryset = custom_revision_manager.get_for_object(self.item)
+ version_ids = queryset.values_list("pk", flat=True)
+ self.assertEqual(len(version_ids), 3)
+ url_name = 'admin:%s_%s_history' % (CustomModel._meta.app_label, CustomModel._meta.module_name)
+ history_url = reverse(url_name, args=(self.item.pk, ))
+ response = self.client.get(history_url)
+ self.assertContainsHtml(response,
+ '<input type="submit" value="compare">',
+ '<input type="radio" name="version_id1" value="%i" style="visibility:hidden" />' % version_ids[0],
+ '<input type="radio" name="version_id2" value="%i" checked="checked" />' % version_ids[0],
+ '<input type="radio" name="version_id1" value="%i" checked="checked" />' % version_ids[1],
+ '<input type="radio" name="version_id2" value="%i" />' % version_ids[1],
+ '<input type="radio" name="version_id1" value="%i" />' % version_ids[2],
+ '<input type="radio" name="version_id2" value="%i" />' % version_ids[2],
+ )
10 reversion_compare_test_project/reversion_compare_test_app/admin.py
View
@@ -19,9 +19,10 @@
from reversion_compare.helpers import html_diff
from reversion_compare_test_project.reversion_compare_test_app.models import SimpleModel, Factory, Car, Person, Pet,\
- VariantModel
+ VariantModel, CustomModel
from reversion.models import Revision, Version
+from reversion.revisions import RevisionManager
#------------------------------------------------------------------------------
@@ -77,6 +78,13 @@ class VariantModelAdmin(CompareVersionAdmin):
admin.site.register(VariantModel, VariantModelAdmin)
+custom_revision_manager = RevisionManager("custom")
+
+class CustomModelAdmin(CompareVersionAdmin):
+ revision_manager = custom_revision_manager
+admin.site.register(CustomModel, CustomModelAdmin)
+
+
"""
5 reversion_compare_test_project/reversion_compare_test_app/models.py
View
@@ -115,6 +115,11 @@ class VariantModel(models.Model):
#------------------------------------------------------------------------------
+
+class CustomModel(models.Model):
+ "Model which uses a custom version manager."
+ text = models.TextField()
+
"""
class ParentModel(models.Model):
Something went wrong with that request. Please try again.