diff --git a/opentech/apply/activity/templates/activity/include/listing_base.html b/opentech/apply/activity/templates/activity/include/listing_base.html index e76d3ec0297c39e7bd63338ad06a7d5e1bea5d19..23056f2fcd17b5d104676122e098172af5611f71 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|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 new file mode 100644 index 0000000000000000000000000000000000000000..a55bc257ba5cf4565a0a21d51fe20a5ceee0393e --- /dev/null +++ b/opentech/apply/funds/templatetags/submission_tags.py @@ -0,0 +1,24 @@ +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): + # 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 = {} + 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 = re.sub(f'(?<!\w){sid}(?!\w)', link, value) + + return mark_safe(value) diff --git a/opentech/apply/funds/tests/test_tags.py b/opentech/apply/funds/tests/test_tags.py new file mode 100644 index 0000000000000000000000000000000000000000..ccbd84b93e06e44c3ea854da2fdad49ef6ff074e --- /dev/null +++ b/opentech/apply/funds/tests/test_tags.py @@ -0,0 +1,20 @@ +from django.template import Context, Template +from django.test import override_settings, TestCase + +from opentech.apply.funds.tests.factories import ApplicationSubmissionFactory + + +@override_settings(ROOT_URLCONF='opentech.apply.urls') +class TestTemplateTags(TestCase): + def test_markdown_tags(self): + template = Template('{% load markdown_tags %}{{ content|markdown|safe }}') + context = Context({'content': 'Lorem ipsum **dolor** sit amet.'}) + output = template.render(context) + self.assertEqual(output, '<p>Lorem ipsum <strong>dolor</strong> sit amet.</p>\n') + + def test_submission_tags(self): + submission = ApplicationSubmissionFactory() + template = Template('{% load submission_tags %}{{ content|submission_links|safe }}') + context = Context({'content': f'Lorem ipsum dolor #{submission.id} sit amet.'}) + output = template.render(context) + self.assertEqual(output, f'Lorem ipsum dolor <a href="{submission.get_absolute_url()}">{submission.title} <span class="mid-grey-text">#{submission.id}</span></a> sit amet.') diff --git a/opentech/apply/review/templates/review/review_detail.html b/opentech/apply/review/templates/review/review_detail.html index 707b79a8efa6cb3e5cff1510da0f1829baef0e80..3280e7d1ce9b939020b1974f3421203db16682c2 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/settings/base.py b/opentech/settings/base.py index ba0565c37b55af5775515ffa66fca01cd55d4311..0fb1fac928d7d236080c582c1207a7f211cdb759 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 diff --git a/opentech/static_src/src/sass/apply/base/_base.scss b/opentech/static_src/src/sass/apply/base/_base.scss index 265736153de24ff77c32a9139992cb3708fe91a7..64fac05a4b13c45752c01e9ca281c1c86f718327 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; +}