From 67184119a7726e82d725efa388a14669229e09bf Mon Sep 17 00:00:00 2001 From: Fredrik Jonsson <frjo@xdeb.org> Date: Tue, 9 Jul 2019 12:41:19 +0200 Subject: [PATCH] Strip all but h4 and use Cleaner class. --- opentech/apply/funds/differ.py | 10 +++++----- opentech/apply/funds/views.py | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/opentech/apply/funds/differ.py b/opentech/apply/funds/differ.py index f99cb7f7b..7573d9a83 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 1d480247e..0b05d7548 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) ] -- GitLab