Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added support for ForeignKey and FileField to moderation

  • Loading branch information...
commit 4f35a32be723475dc9670c9f73055814f4e51c83 1 parent 724e7ef
marcin-koziol authored
Showing with 46 additions and 7 deletions.
  1. +46 −7 src/moderation/diff.py
View
53 src/moderation/diff.py
@@ -3,8 +3,9 @@
import re
import difflib
-from django.db.models import fields
+from django.db.models import fields, ForeignKey, FileField
from django.utils.html import escape
+from django.core.urlresolvers import reverse, NoReverseMatch
class BaseChange(object):
@@ -22,7 +23,43 @@ def render_diff(self, template, context):
return render_to_string(template, context)
+class ForeignKeyChange(BaseChange):
+ @property
+ def diff(self):
+ rel_to = self.field.rel.to
+ pk1, pk2 = self.change
+ value1, value2 = rel_to.objects.get(pk=self.change[0]), rel_to.objects.get(pk=self.change[1])
+
+ try:
+ url1 = reverse( 'admin:%s_%s_change' % (rel_to._meta.app_label, rel_to._meta.object_name.lower()),args=[pk1] )
+ except NoReverseMatch:
+ url1 = None
+ if value1 == value2:
+ if url1:
+ return '<a href="%(url)s">%(value)s</a>' % {'url': url1, 'value':value1}
+ return value1
+
+ try:
+ url2 = reverse( 'admin:%s_%s_change' % (rel_to._meta.app_label, rel_to._meta.object_name.lower()),args=[pk2] )
+ except NoReverseMatch:
+ url2 = None
+
+ if url1:
+ return ('<a href="%(url)s">%(value)s</a>' % {'url': url1, 'value':value1})+ ' &gt; ' +('<a href="%(url)s">%(value)s</a>' % {'url': url2, 'value':value2})
+ return value1+' &gt; '+value2
+
+class FileChange(BaseChange):
+
+ @property
+ def diff(self):
+ value1, value2 = (unicode(self.change[0]), unicode(self.change[1]))
+ url1, url2 = (self.change[0].url, self.change[1].url)
+ change = (value1, value2)
+ if value1 == value2:
+ return '<a href="%(url)s">%(value)s</a>' % {'url': url1, 'value':value1}
+ return ('<a href="%(url)s">%(value)s</a>' % {'url': url1, 'value':value1})+' &gt; '+('<a href="%(url)s">%(value)s</a>' % {'url': url2, 'value':value2})
+
class TextChange(BaseChange):
@property
@@ -36,8 +73,8 @@ def diff(self):
{'diff_operations': get_diff_operations(*self.change)})
-class ImageChange(BaseChange):
+class ImageChange(BaseChange):
@property
def diff(self):
left_image, right_image = self.change
@@ -112,11 +149,13 @@ def get_change_for_type(verbose_name, change, field):
u"New %(verbose_name)s" % {'verbose_name': verbose_name},
field,
change)
+ elif isinstance(field, ForeignKey):
+ value1, value2 = change
+ change = ForeignKeyChange(verbose_name, field, (unicode(value1), unicode(value2)))
+ elif isinstance(field, FileField):
+ value1, value2 = change
+ change = FileChange(verbose_name, field, (value1, value2))
else:
value1, value2 = change
- change = TextChange(verbose_name,
- field,
- (unicode(value1), unicode(value2)),
- )
-
+ change = TextChange(verbose_name, field, (unicode(value1), unicode(value2)))
return change
Please sign in to comment.
Something went wrong with that request. Please try again.