From d005a9d0c728b22e56392be80c355655c772dcb2 Mon Sep 17 00:00:00 2001
From: Fredrik Jonsson <frjo@xdeb.org>
Date: Thu, 6 Dec 2018 14:39:48 +0100
Subject: [PATCH] Make submission shortcodes in the format #1234 expand to
 links to the submission in reviews and comments.

---
 .../activity/include/listing_base.html        |  4 +--
 .../funds/templatetags/submission_tags.py     | 27 +++++++++++++++++++
 .../templates/review/review_detail.html       |  6 ++---
 .../static_src/src/sass/apply/base/_base.scss |  4 +++
 4 files changed, 36 insertions(+), 5 deletions(-)
 create mode 100644 opentech/apply/funds/templatetags/submission_tags.py

diff --git a/opentech/apply/activity/templates/activity/include/listing_base.html b/opentech/apply/activity/templates/activity/include/listing_base.html
index e76d3ec02..968145549 100644
--- a/opentech/apply/activity/templates/activity/include/listing_base.html
+++ b/opentech/apply/activity/templates/activity/include/listing_base.html
@@ -1,4 +1,4 @@
-{% load activity_tags bleach_tags markdown_tags %}
+{% load activity_tags bleach_tags markdown_tags submission_tags %}
 <div class="feed__item feed__item--{{ activity.type }}">
     <div class="feed__pre-content">
         <p class="feed__label feed__label--{{ activity.type }}">{{ activity.type|capfirst }}</p>
@@ -19,7 +19,7 @@
                 updated <a href="{{ activity.submission.get_absolute_url }}">{{ activity.submission.title }}</a>
             {% endif %}
 
-            {{ activity.message|markdown|bleach }}
+            {{ activity.message|markdown|bleach|submission_links }}
 
             {% 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
new file mode 100644
index 000000000..df63308d5
--- /dev/null
+++ b/opentech/apply/funds/templatetags/submission_tags.py
@@ -0,0 +1,27 @@
+import re
+
+from django import template
+from django.utils.safestring import mark_safe
+
+from opentech.apply.funds.models import ApplicationSubmission
+
+register = template.Library()
+
+
+@register.filter
+def submission_links(value):
+    regex = re.compile('[^\w]\#(\d+)[^\w]')
+    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 links:
+        for sid, link in links.items():
+            value = value.replace(sid, link)
+
+    return mark_safe(value)
diff --git a/opentech/apply/review/templates/review/review_detail.html b/opentech/apply/review/templates/review/review_detail.html
index 707b79a8e..3280e7d1c 100644
--- a/opentech/apply/review/templates/review/review_detail.html
+++ b/opentech/apply/review/templates/review/review_detail.html
@@ -1,5 +1,5 @@
 {% extends "base-apply.html" %}
-{% load bleach_tags %}
+{% load bleach_tags submission_tags %}
 
 {% block content %}
 <div class="admin-bar">
@@ -29,8 +29,8 @@
 </div>
 
 <div class="rich-text rich-text--answers">
-    {{ object.get_comments_display }}
+    {{ object.get_comments_display|submission_links }}
 
-    {{ object.output_answers }}
+    {{ object.output_answers|submission_links }}
 </div>
 {% endblock %}
diff --git a/opentech/static_src/src/sass/apply/base/_base.scss b/opentech/static_src/src/sass/apply/base/_base.scss
index 265736153..64fac05a4 100644
--- a/opentech/static_src/src/sass/apply/base/_base.scss
+++ b/opentech/static_src/src/sass/apply/base/_base.scss
@@ -114,3 +114,7 @@ ol {
 .light-grey-bg {
     background-color: $color--light-grey;
 }
+
+.mid-grey-text {
+    color: $color--mid-dark-grey;
+}
-- 
GitLab