Browse files

Merge pull request #14 from pahaz/patch-2

add `patch_admin` function
Thx pahaz!
  • Loading branch information...
2 parents 9178e85 + 3ec764c commit 9235a966b09cfcfd40452e2e1bd869a2f42fc149 @jedie committed Mar 22, 2013
Showing with 50 additions and 1 deletion.
  1. +2 −1 AUTHORS
  2. +16 −0 README.creole
  3. +32 −0 reversion_compare/helpers.py
View
3 AUTHORS
@@ -1,4 +1,3 @@
-
PRIMARY AUTHORS are and/or have been (alphabetic order):
* Diemer, Jens
@@ -14,4 +13,6 @@ CONTRIBUTORS are and/or have been (alphabetic order):
Github: <https://github.com/mlavin>
* Mihelac, Bojan
Github: <https://github.com/bmihelac>
+* Pahaz, Blinov
+ Github: <https://github.com/pahaz>
View
16 README.creole
@@ -57,6 +57,17 @@ class ExampleModelAdmin(CompareVersionAdmin):
admin.site.register(ExampleModel, ExampleModelAdmin)
}}}
+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)
+}}}
+
+
=== customize ===
It's possible to change the look for every field or for a entire field type.
@@ -76,6 +87,11 @@ class YourAdmin(CompareVersionAdmin):
return "%r <-> %r" % (obj_compare.value1, obj_compare.value2)
}}}
+and example using **patch_admin** with custom version admin class:
+{{{
+patch_admin(User, AdminClass=YourAdmin)
+}}}
+
== screenshots ==
View
32 reversion_compare/helpers.py
@@ -19,6 +19,10 @@
from django.utils.safestring import mark_safe
from django.utils.encoding import force_unicode
+from django.contrib import admin
+from django.contrib.admin.sites import NotRegistered
+
+
try:
# http://code.google.com/p/google-diff-match-patch/
from diff_match_patch import diff_match_patch
@@ -168,6 +172,34 @@ def compare_queryset(first, second):
result.append(item)
return result
+def patch_admin(model, admin_site=None, AdminClass=None):
+ """
+ Enables version control with full admin integration for a model that has
+ already been registered with the django admin site.
+
+ This is excellent for adding version control to existing Django contrib
+ applications.
+ """
+ admin_site = admin_site or admin.site
+ try:
+ ModelAdmin = admin_site._registry[model].__class__
+ except KeyError:
+ raise NotRegistered("The model {model} has not been registered with the admin site.".format(
+ model = model,
+ ))
+ # Unregister existing admin class.
+ admin_site.unregister(model)
+ # Register patched admin class.
+ if not AdminClass:
+ from reversion_compare.admin import CompareVersionAdmin
+ class PatchedModelAdmin(CompareVersionAdmin, ModelAdmin):
+ pass
+ else:
+ class PatchedModelAdmin(AdminClass, ModelAdmin):
+ pass
+
+ admin_site.register(model, PatchedModelAdmin)
+
if __name__ == "__main__":
import doctest

0 comments on commit 9235a96

Please sign in to comment.