diff --git a/simple_history/admin.py b/simple_history/admin.py index a861333e7..a105ee301 100644 --- a/simple_history/admin.py +++ b/simple_history/admin.py @@ -6,7 +6,6 @@ from django.contrib import admin from django.contrib.admin import helpers from django.contrib.contenttypes.models import ContentType -from django.core.urlresolvers import reverse from django.shortcuts import get_object_or_404, render from django.utils.text import capfirst from django.utils.html import mark_safe @@ -18,6 +17,10 @@ from django.contrib.admin.utils import unquote except ImportError: # Django < 1.7 from django.contrib.admin.util import unquote +try: + from django.urls import reverse +except ImportError: # Django < 1.10 + from django.core.urlresolvers import reverse try: from django.utils.version import get_complete_version except ImportError: diff --git a/simple_history/models.py b/simple_history/models.py index ccba2b83e..9fb67f891 100644 --- a/simple_history/models.py +++ b/simple_history/models.py @@ -20,6 +20,10 @@ from django.apps import apps except ImportError: # Django < 1.7 from django.db.models import get_app +try: + from django.urls import reverse +except ImportError: # Django < 1.10 + from django.core.urlresolvers import reverse try: from south.modelsinspector import add_introspection_rules except ImportError: # south not present @@ -204,14 +208,12 @@ def get_extra_fields(self, model, fields): user_model = getattr(settings, 'AUTH_USER_MODEL', 'auth.User') - @models.permalink def revert_url(self): """URL for this change in the default admin site.""" opts = model._meta app_label, model_name = opts.app_label, opts.model_name - return ('%s:%s_%s_simple_history' % - (admin.site.name, app_label, model_name), - [getattr(self, opts.pk.attname), self.history_id]) + viewname = '%s:%s_%s_simple_history' % (admin.site.name, app_label, model_name) + return reverse(viewname, args=[getattr(self, opts.pk.attname), self.history_id]) def get_instance(self): return model(**{ diff --git a/simple_history/tests/tests/test_admin.py b/simple_history/tests/tests/test_admin.py index 2d71b6319..a0aee53e8 100644 --- a/simple_history/tests/tests/test_admin.py +++ b/simple_history/tests/tests/test_admin.py @@ -6,7 +6,6 @@ from django.contrib.messages.storage.fallback import FallbackStorage from django.test.utils import override_settings from django.test.client import RequestFactory -from django.core.urlresolvers import reverse from django.conf import settings from django.contrib.auth import get_user_model from django.utils.encoding import force_text @@ -19,6 +18,10 @@ from django.contrib.admin.utils import quote except ImportError: # Django < 1.7 from django.contrib.admin.util import quote +try: + from django.urls import reverse +except ImportError: # Django < 1.10 + from django.core.urlresolvers import reverse User = get_user_model() today = datetime(2021, 1, 1, 10, 0) diff --git a/simple_history/tests/tests/test_models.py b/simple_history/tests/tests/test_models.py index 3b4e03f04..7934d6d53 100644 --- a/simple_history/tests/tests/test_models.py +++ b/simple_history/tests/tests/test_models.py @@ -343,6 +343,14 @@ def test_model_with_excluded_fields(self): self.assertIn('question', all_fields_names) self.assertNotIn('pub_date', all_fields_names) + def test_revert_url(self): + p = Poll(question="what's up?", pub_date=today) + p.save() + record = p.history.all()[0] + revert_url = record.revert_url() + expected_url = '/admin/tests/poll/{}/history/{}/'.format(p.id, record.id) + self.assertEqual(revert_url, expected_url) + class CreateHistoryModelTests(unittest.TestCase):