From bba68cefebbf329d186cb71ce18cfddd4bc3b2f2 Mon Sep 17 00:00:00 2001
From: Fredrik Jonsson <frjo@xdeb.org>
Date: Fri, 7 Dec 2018 10:09:17 +0100
Subject: [PATCH] Load all submissions in one query for efficience. Use sub
 instead of replace so not replacing #1234 with #123 links. Catch short codes
 on end and start of line.

---
 .../templates/activity/include/listing_base.html  |  2 +-
 .../apply/funds/templatetags/submission_tags.py   | 15 ++++++---------
 opentech/settings/base.py                         |  6 +++---
 3 files changed, 10 insertions(+), 13 deletions(-)

diff --git a/opentech/apply/activity/templates/activity/include/listing_base.html b/opentech/apply/activity/templates/activity/include/listing_base.html
index 968145549..23056f2fc 100644
--- a/opentech/apply/activity/templates/activity/include/listing_base.html
+++ b/opentech/apply/activity/templates/activity/include/listing_base.html
@@ -19,7 +19,7 @@
                 updated <a href="{{ activity.submission.get_absolute_url }}">{{ activity.submission.title }}</a>
             {% endif %}
 
-            {{ activity.message|markdown|bleach|submission_links }}
+            {{ activity.message|submission_links|markdown|bleach }}
 
             {% if not submission_title and activity|user_can_see_related:request.user %}
                 {% with url=activity.related_object.get_absolute_url %}
diff --git a/opentech/apply/funds/templatetags/submission_tags.py b/opentech/apply/funds/templatetags/submission_tags.py
index df63308d5..a55bc257b 100644
--- a/opentech/apply/funds/templatetags/submission_tags.py
+++ b/opentech/apply/funds/templatetags/submission_tags.py
@@ -10,18 +10,15 @@ register = template.Library()
 
 @register.filter
 def submission_links(value):
-    regex = re.compile('[^\w]\#(\d+)[^\w]')
+    # Match tags in the format #123 that is not preceeded and/or followed by a word character.
+    matches = re.findall('(?<!\w)\#(\d+)(?!\w)', value)
     links = {}
-    for match in regex.finditer(value):
-        try:
-            submission = ApplicationSubmission.objects.get(id=match[1])
-        except ApplicationSubmission.DoesNotExist:
-            pass
-        else:
-            links[f'#{submission.id}'] = f'<a href="{submission.get_absolute_url()}">{submission.title} <span class="mid-grey-text">#{submission.id}</span></a>'
+    if matches:
+        for submission in ApplicationSubmission.objects.filter(id__in=matches):
+            links[f'\#{submission.id}'] = f'<a href="{submission.get_absolute_url()}">{submission.title} <span class="mid-grey-text">#{submission.id}</span></a>'
 
     if links:
         for sid, link in links.items():
-            value = value.replace(sid, link)
+            value = re.sub(f'(?<!\w){sid}(?!\w)', link, value)
 
     return mark_safe(value)
diff --git a/opentech/settings/base.py b/opentech/settings/base.py
index ba0565c37..0fb1fac92 100644
--- a/opentech/settings/base.py
+++ b/opentech/settings/base.py
@@ -419,11 +419,11 @@ SOCIAL_AUTH_PIPELINE = (
 )
 
 # Bleach Settings
-BLEACH_ALLOWED_TAGS = ['h2', 'h3', 'p', 'b', 'i', 'em', 'strong', 'a', 'ul', 'ol', 'li', 'br']
+BLEACH_ALLOWED_TAGS = ['h2', 'h3', 'p', 'b', 'i', 'em', 'strong', 'a', 'ul', 'ol', 'li', 'br', 'span']
 
-BLEACH_ALLOWED_ATTRIBUTES = ['href', 'title', 'style']
+BLEACH_ALLOWED_ATTRIBUTES = ['href', 'title', 'class']
 
-BLEACH_ALLOWED_STYLES = ['font-family', 'font-weight', 'text-decoration', 'font-variant']
+BLEACH_ALLOWED_STYLES = []
 
 BLEACH_STRIP_TAGS = True
 
-- 
GitLab