diff --git a/opentech/apply/funds/differ.py b/opentech/apply/funds/differ.py index f99cb7f7b73c3b2c6594ac9c9c865494fad61fe0..7573d9a830fc078c3de01dd5cc1cae62710d5391 100644 --- a/opentech/apply/funds/differ.py +++ b/opentech/apply/funds/differ.py @@ -1,8 +1,7 @@ +from bleach.sanitizer import Cleaner from bs4 import BeautifulSoup from difflib import SequenceMatcher -import bleach - from django.utils.html import format_html from django.utils.text import mark_safe @@ -28,17 +27,18 @@ def compare(answer_a, answer_b, should_bleach=True): return answer_b if should_bleach: + cleaner = Cleaner(tags=['h4'], attributes={}, strip=True) if isinstance(answer_a, str): - answer_a = bleach.clean(answer_a, tags=['section', 'h4', 'p', 'br'], attributes={}, strip=True) + answer_a = cleaner.clean(answer_a) else: answer_a = str(answer_a) if isinstance(answer_b, str): - answer_b = bleach.clean(answer_b, tags=['section', 'h4', 'p', 'br'], attributes={}, strip=True) + answer_b = cleaner.clean(answer_b) else: answer_b = str(answer_b) - diff = SequenceMatcher(lambda x: '\n\r' in x, answer_a, answer_b) + diff = SequenceMatcher(None, answer_a, answer_b) output = [] added = [] deleted = [] diff --git a/opentech/apply/funds/views.py b/opentech/apply/funds/views.py index 1d480247ecf9f5ca5f1856208acdea47d484ad65..0b05d75483ffeb19edfad866c72088a80c657f07 100644 --- a/opentech/apply/funds/views.py +++ b/opentech/apply/funds/views.py @@ -801,7 +801,7 @@ class RevisionCompareView(DetailView): # Compare all the required fields diffed_required = [ - compare(*fields, should_bleach=False) + compare(*fields) for fields in zip(from_required, to_required) ] for field, diff in zip(self.object.named_blocks, diffed_required): @@ -809,7 +809,7 @@ class RevisionCompareView(DetailView): # Compare all the answers diffed_text_fields_answers = [ - compare(*fields, should_bleach=True) + compare(*fields) for fields in zip(from_rendered_text_fields, to_rendered_text_fields) ]