From 2ed0a4bcf91e59139086e9907a1b9eb58f28ddb9 Mon Sep 17 00:00:00 2001 From: David Taylor Date: Wed, 16 Mar 2016 10:08:26 +0000 Subject: [PATCH] Updated changes view to use diff where appropriate. --- RIGS/templates/RIGS/version_changes.html | 43 ++++++------------- .../RIGS/version_changes_change.html | 27 ++++++++++++ RIGS/versioning.py | 20 +++++++++ 3 files changed, 60 insertions(+), 30 deletions(-) create mode 100644 RIGS/templates/RIGS/version_changes_change.html diff --git a/RIGS/templates/RIGS/version_changes.html b/RIGS/templates/RIGS/version_changes.html index 98abaf15..9fd57020 100644 --- a/RIGS/templates/RIGS/version_changes.html +++ b/RIGS/templates/RIGS/version_changes.html @@ -1,40 +1,23 @@ {% for change in version.field_changes %} - + {% endfor %} {% for itemChange in version.item_changes %} - + {% endspaceless %}'>item '{% if itemChange.new %}{{ itemChange.new.name }}{% else %}{{ itemChange.old.name }}{% endif %}' {% endfor %} \ No newline at end of file diff --git a/RIGS/templates/RIGS/version_changes_change.html b/RIGS/templates/RIGS/version_changes_change.html new file mode 100644 index 00000000..19411a36 --- /dev/null +++ b/RIGS/templates/RIGS/version_changes_change.html @@ -0,0 +1,27 @@ +{# pass in variable "change" to this template #} + +{% if change.linebreaks and change.new and change.old %} + {% for diff in change.diff %} + {% if diff.type == "insert" %} + {{ diff.text|linebreaksbr }} + {% elif diff.type == "delete" %} + {{diff.text|linebreaksbr}} + {% else %} + {{diff.text|linebreaksbr}} + {% endif %} + {% endfor %} +{% else %} + {% if change.old %} + + {{change.old}} + + {% endif %} + {% if change.new and change.old %} +
+ {% endif %} + {% if change.new %} + + {{change.new}} + + {% endif %} +{% endif %} \ No newline at end of file diff --git a/RIGS/versioning.py b/RIGS/versioning.py index fa54e9ff..38c92936 100644 --- a/RIGS/versioning.py +++ b/RIGS/versioning.py @@ -17,6 +17,7 @@ from reversion.models import Version from django.contrib.contenttypes.models import ContentType # Used to lookup the content_type from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger from django.db.models import ForeignKey, IntegerField, EmailField, TextField +from diff_match_patch import diff_match_patch from RIGS import models, forms import datetime @@ -64,6 +65,25 @@ def model_compare(oldObj, newObj, excluded_keys=[]): return True return False + @property + def diff(self): + oldText = str(self.display_value(self._old)) or "" + newText = str(self.display_value(self._new)) or "" + dmp = diff_match_patch() + diffs = dmp.diff_main(oldText, newText) + dmp.diff_cleanupSemantic(diffs) + + outputDiffs = [] + + for (op, data) in diffs: + if op == dmp.DIFF_INSERT: + outputDiffs.append({'type':'insert', 'text':data}) + elif op == dmp.DIFF_DELETE: + outputDiffs.append({'type':'delete', 'text':data}) + elif op == dmp.DIFF_EQUAL: + outputDiffs.append({'type':'equal', 'text':data}) + return outputDiffs + changes = [] for thisField in theFields: