Skip to content

Add compare view to django-reversion for comparing two versions of a reversion model.

License

Notifications You must be signed in to change notification settings

ivelum/django-reversion-compare

 
 

Repository files navigation

django-reversion-compare

django-reversion-compare is an extension to django-reversion that provides a history compare view to compare two versions of a model which is under reversion.

Comparing model versions is not a easy task. Maybe there are different view how this should looks like. This project will gives you a generic way to see whats has been changed.

Many parts are customizable by overwrite methods or subclassing, see above.

Build Status on github github.com/jedie/django-reversion-compare/actions
Build Status on travis-ci.org travis-ci.org/jedie/django-reversion-compare
Coverage Status on coveralls.io coveralls.io/r/jedie/django-reversion-compare
Coverage Status on codecov.io codecov.io/gh/jedie/django-reversion-compare
Requirements Status on requires.io requires.io/github/jedie/django-reversion-compare/requirements/

Installation

Just use:

pip install django-reversion-compare

Setup

Add reversion_compare to INSTALLED_APPS in your settings.py, e.g.:

INSTALLED_APPS = (
    'django...',
    ...
    'reversion', # https://github.com/etianen/django-reversion
    'reversion_compare', # https://github.com/jedie/django-reversion-compare
    ...
)

# Add reversion models to admin interface:
ADD_REVERSION_ADMIN=True
# optional settings:
REVERSION_COMPARE_FOREIGN_OBJECTS_AS_ID=False
REVERSION_COMPARE_IGNORE_NOT_REGISTERED=False

Usage

Inherit from CompareVersionAdmin instead of VersionAdmin to get the comparison feature.

admin.py e.g.:

from django.contrib import admin
from reversion_compare.admin import CompareVersionAdmin

from my_app.models import ExampleModel

@admin.register(ExampleModel)
class ExampleModelAdmin(CompareVersionAdmin):
    pass

If you're using an existing third party app, then you can add patch django-reversion-compare into its admin class by using the reversion_compare.helpers.patch_admin() method. For example, to add version control to the built-in User model:

from reversion_compare.helpers import patch_admin

patch_admin(User)

e.g.: Add django-cms Page model:

from cms.models.pagemodel import Page
from reversion_compare.helpers import patch_admin


# Patch django-cms Page Model to add reversion-compare functionality:
patch_admin(Page)

Customize

It's possible to change the look for every field or for a entire field type. You must only define a methods to your admin class with this name scheme:

  • "compare_%s" % field_name
  • "compare_%s" % field.get_internal_type()

If there is no method with this name scheme, the fallback_compare() method will be used.

An example for specifying a compare method for a model field by name:

class YourAdmin(CompareVersionAdmin):
    def compare_foo_bar(self, obj_compare):
        """ compare the foo_bar model field """
        return "%r <-> %r" % (obj_compare.value1, obj_compare.value2)

and example using patch_admin with custom version admin class:

patch_admin(User, AdminClass=YourAdmin)

Class Based View

Beyond the Admin views, you can also create a Class Based View for displaying and comparing version differences. This is a single class-based-view that either displays the list of versions to select for an object or displays both the versions and their differences (if the versions to be compared have been selected). This class can be used just like a normal DetailView:

Inherit from it in your class and add a model (or queryset), for example:

from reversion_compare.views import HistoryCompareDetailView

class SimpleModelHistoryCompareView(HistoryCompareDetailView):
    model = SimpleModel

Then, assign that CBV to a url, for example:

url(r'^test_view/(?P<pk>\d+)$', views.SimpleModelHistoryCompareView.as_view() ),

Last step, you need to create a template to display both the version select form and the changes part (if the form is submitted). An example template is the following:

<style type="text/css">
/* minimal style for the diffs */
pre.highlight {
    max-width: 900px;
    white-space: pre-line;
}
del, ins {
    color: #000;
    text-decoration: none;
}
del { background-color: #ffe6e6; }
ins { background-color: #e6ffe6; }
sup.follow { color: #5555ff; }
</style>

{% include "reversion-compare/action_list_partial.html"  %}
{% if request.GET.version_id1 %}
    {% include "reversion-compare/compare_partial.html"  %}
    {% include "reversion-compare/compare_links_partial.html"  %}
{% endif %}

Beyond the styling, you should include:

  • reversion-compare/action_list_partial.html partial template to display the version select form
  • reversion-compare/compare_partial.html partial template to display the actual version
  • reversion-compare/compare_links_partial.html to include previous/next comparison links

compare_partial.html and compare_links_partial.html will show the compare-related information so it's better to display them only when the select-versions-tocompare-form has been submitted. If you want more control on the appearence of your templates you can check the above partials to understand how the availabble context variables are used and override them completely.

Screenshots

Here some screenshots of django-reversion-compare:


How to select the versions to compare:

django-reversion-compare_v0_1_0-01.png


from v0.1.0: DateTimeField compare (last update), TextField compare (content) with small changes and a ForeignKey compare (child model instance was added):

django-reversion-compare_v0_1_0-02.png


from v0.1.0: Same as above, but the are more lines changed in TextField and the ForeignKey relation was removed:

django-reversion-compare_v0_1_0-03.png


Example screenshot from v0.3.0: a many-to-many field compare (friends, hobbies):

django-reversion-compare_v0_3_0-01.png

  • In the first line, the m2m object has been changed.
  • line 2: A m2m object was deleted
  • line 3: A m2m object was removed from this entry (but not deleted)
  • line 4: This m2m object has not changed

create developer environment

e.g.:

# Clone project (Use your fork SSH url!):
~$ git clone https://github.com/jedie/django-reversion-compare.git
~$ cd django-reversion-compare
~/django-reversion-compare$ make install
~/django-reversion-compare$ make
help                 List all commands
install-poetry       install or update poetry
install              install reversion_compare via poetry
lint                 Run code formatters and linter
fix-code-style       Fix code formatting
tox-listenvs         List all tox test environments
tox                  Run pytest via tox with all environments
tox-py36             Run pytest via tox with *python v3.6*
tox-py37             Run pytest via tox with *python v3.7*
tox-py38             Run pytest via tox with *python v3.8*
pytest               Run pytest
update-rst-readme    update README.rst from README.reversion_compare
publish              Release new version to PyPi
run-test-server      Start Django dev server with the test project

Helpful for writing and debugging unittests is to run a local test server with the same data. e.g.:

~/django-reversion-compare$ make run-test-server

migration will be run.

Call manage commands from test project, e.g.:

~/django-reversion-compare$ poetry shell
django-reversion-compare-foobar-py3.6) ~/django-reversion-compare$ ./reversion_compare_tests/manage.py --help
...

Backwards-incompatible changes

v0.12.0

Google "diff-match-patch" is now mandatory and not optional.

Version compatibility

Reversion-Compare django-reversion Django Python
>=v0.10.0 v3.0 v2.2, v3.0 v3.6, v3.7, v3.8, pypy3
>=v0.9.0 v2.0 v2.2, v3.0 v3.6, v3.7, v3.8, pypy3
>=v0.8.6 v2.0 v1.11, v2.0 v3.5, v3.6, v3.7, pypy3
>=v0.8.4 v2.0 v1.8, v1.11, v2.0 v3.5, v3.6, pypy3
>=v0.8.3 v2.0 v1.8, v1.11 v3.5, v3.6, pypy3
v0.8.x v2.0 v1.8, v1.10, v1.11 v2.7, v3.4, v3.5, v3.6 (only with Django 1.11)
>=v0.7.2 v2.0 v1.8, v1.9, v1.10 v2.7, v3.4, v3.5
v0.7.x v2.0 v1.8, v1.9 v2.7, v3.4, v3.5
v0.6.x v1.9, v1.10 v1.8, v1.9 v2.7, v3.4, v3.5
>=v0.5.2 v1.9 v1.7, v1.8 v2.7, v3.4
>=v0.4 v1.8 v1.7 v2.7, v3.4
<v0.4 v1.6 v1.4 v2.7

These are the unittests variants. See also: /.travis.yml Maybe other versions are compatible, too.

Changelog

Links

Github https://github.com/jedie/django-reversion-compare
Python Packages https://pypi.org/project/django-reversion-compare/

Donation

About

Add compare view to django-reversion for comparing two versions of a reversion model.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 92.5%
  • HTML 5.9%
  • Makefile 1.6%