From 33a399e9d077151b5c96377e90e5c4f733324eeb Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Thu, 14 Jun 2018 11:25:26 +0100
Subject: [PATCH] handle the diff better

---
 .../funds/templates/funds/includes/actions.html     |  2 +-
 opentech/apply/funds/views.py                       | 13 ++++++++++---
 .../src/sass/apply/components/_revisions.scss       |  8 ++++++++
 opentech/static_src/src/sass/apply/main.scss        |  1 +
 4 files changed, 20 insertions(+), 4 deletions(-)
 create mode 100644 opentech/static_src/src/sass/apply/components/_revisions.scss

diff --git a/opentech/apply/funds/templates/funds/includes/actions.html b/opentech/apply/funds/templates/funds/includes/actions.html
index 6172da03c..4898fe36f 100644
--- a/opentech/apply/funds/templates/funds/includes/actions.html
+++ b/opentech/apply/funds/templates/funds/includes/actions.html
@@ -12,5 +12,5 @@
         <a data-fancybox data-src="#assign-lead" class="button button--half-width button--white" href="#">Lead</a>
     </div>
 
-    <a class="button button--white button--full-width button--bottom-space" href="#">Placeholder</a>
+    <a class="button button--white button--full-width button--bottom-space" href="{% url 'apply:revisions:list' submission_pk=object.id %}">Revisions</a>
 </div>
diff --git a/opentech/apply/funds/views.py b/opentech/apply/funds/views.py
index 7eb7feb73..2cd33849d 100644
--- a/opentech/apply/funds/views.py
+++ b/opentech/apply/funds/views.py
@@ -6,6 +6,7 @@ from django.http import HttpResponseRedirect
 from django.shortcuts import get_object_or_404
 from django.urls import reverse_lazy
 from django.utils.decorators import method_decorator
+from django.utils.text import mark_safe
 from django.views.generic import ListView, TemplateView, UpdateView
 
 from django_filters.views import FilterView
@@ -266,8 +267,13 @@ class RevisionCompareView(TemplateView):
 
     def compare_answer(self, answer_a, answer_b):
         if not answer_a and not answer_b:
+            # This catches the case where both results are None and we cant compare
             return answer_b
-        diff = SequenceMatcher(answer_a, answer_b)
+        if isinstance(answer_a, dict) or isinstance(answer_b, dict):
+            # TODO: handle file dictionaries
+            return answer_b
+
+        diff = SequenceMatcher(None, answer_a, answer_b)
         output = []
         for opcode, a0, a1, b0, b1 in diff.get_opcodes():
             if opcode == 'equal':
@@ -277,10 +283,11 @@ class RevisionCompareView(TemplateView):
             elif opcode == 'delete':
                 output.append('<span class="deleted">' + diff.a[a0:a1] + "</span>")
             elif opcode == 'replace':
-                raise NotImplementedError("what to do with 'replace' opcode?")
+                output.append('<span class="deleted">' + diff.a[a0:a1] + "</span>")
+                output.append('<span class="added">' + diff.b[b0:b1] + '</span>')
             else:
                 raise RuntimeError("unexpected opcode")
-        return ''.join(output)
+        return mark_safe(''.join(output))
 
     def compare(self, from_data, to_data):
         diffed_form_data = {
diff --git a/opentech/static_src/src/sass/apply/components/_revisions.scss b/opentech/static_src/src/sass/apply/components/_revisions.scss
new file mode 100644
index 000000000..2bd6b1710
--- /dev/null
+++ b/opentech/static_src/src/sass/apply/components/_revisions.scss
@@ -0,0 +1,8 @@
+span {
+    &.deleted {
+        background-color: #EE9090;
+    }
+    &.added {
+        background-color: #90EE90;
+    }
+}
diff --git a/opentech/static_src/src/sass/apply/main.scss b/opentech/static_src/src/sass/apply/main.scss
index e53ac847e..3f122dd07 100755
--- a/opentech/static_src/src/sass/apply/main.scss
+++ b/opentech/static_src/src/sass/apply/main.scss
@@ -43,6 +43,7 @@
 @import 'components/table';
 @import 'components/traffic-light';
 @import 'components/wrapper';
+@import 'components/revisions';
 
 // Layout
 @import 'layout/header';
-- 
GitLab