Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

passing tests of reversion

  • Loading branch information...
commit d6ef8fa3be3c6e246b467b8a91c971adeae23d20 1 parent cd48164
@gugu authored
Showing with 16 additions and 14 deletions.
  1. +2 −3 MANIFEST.in
  2. +14 −11 src/reversion_email/models.py
View
5 MANIFEST.in
@@ -1,4 +1,3 @@
-include src/reversion/templates/reversion/*.html
-include src/reversion/locale/*/LC_MESSAGES/django.*
+include src/reversion_email/templates/reversion_email/*.html
include LICENSE
-include README.markdown
+include README.markdown
View
25 src/reversion_email/models.py
@@ -1,6 +1,5 @@
"""Database models used by django-reversion-email."""
-from reversion.models import Version
from django.conf import settings
from django.db.models import signals
from django.core.mail import EmailMultiAlternatives
@@ -9,29 +8,32 @@
from django.template import Context
from django.core import urlresolvers
from django.contrib.sites.models import Site
+import difflib
def send_diff_to_email(sender, instance, **kwargs):
- from reversion.helpers import generate_patch_html, generate_patch
versions = sender.objects.filter(content_type=instance.content_type, object_id=instance.object_id).order_by('-id')
html_template = get_template('reversion_email/email.html')
text_template = get_template('reversion_email/email.txt')
patch = []
object_meta = instance.content_type.model_class()._meta
+ html_diff = difflib.HtmlDiff()
+ text_diff = difflib.Differ()
+
for field in object_meta.fields:
- if versions.count() > 1:
+ if versions.count() > 1 and (versions[1].object is not None) and (versions[0].object is not None):
patch.append({
'field' : field.name,
- 'html' : generate_patch_html(versions[1],versions[0],field.name),
- 'text' : generate_patch(versions[1],versions[0],field.name)
+ 'html' : html_diff.make_file([unicode(getattr(versions[1].object, field.name))],[unicode(getattr(versions[0].object,field.name))]),
+ 'text' : '\n'.join(text_diff.compare([unicode(getattr(versions[1].object, field.name))],[unicode(getattr(versions[0].object,field.name))])),
})
- else:
+ elif versions[0].object is not None or versions[1].object is not None:
patch.append({
'field' : field.name,
- 'html' : getattr(versions[0].object, field.name),
- 'text' : getattr(versions[0].object, field.name),
+ 'html' : getattr(versions[0].object or versions[1].object, field.name),
+ 'text' : getattr(versions[0].object or versions[1].object, field.name),
})
current_site = Site.objects.get_current()
@@ -43,8 +45,8 @@ def send_diff_to_email(sender, instance, **kwargs):
'site' : current_site,
})
try:
- if len(versions)>1:
- context['admin_recover_url'] = urlresolvers.reverse('admin:%s_%s_revision' % url_info, args=[versions[1].object.pk, instance.pk])
+ if len(versions)>1 and (versions[0].object is not None or versions[1].object is not None):
+ context['admin_recover_url'] = urlresolvers.reverse('admin:%s_%s_revision' % url_info, args=[(versions[0].object or versions[1].object).pk, instance.pk])
except NoReverseMatch:
pass
text_content = text_template.render(context)
@@ -63,4 +65,5 @@ def send_diff_to_email(sender, instance, **kwargs):
email.send()
if hasattr(settings,'MODERATORS'):
- signals.post_save.connect(send_diff_to_email, sender = Version)
+ import reversion.models
+ signals.post_save.connect(send_diff_to_email, sender = reversion.models.Version)
Please sign in to comment.
Something went wrong with that request. Please try again.