Skip to content
Snippets Groups Projects
Commit bba68cef authored by Fredrik Jonsson's avatar Fredrik Jonsson
Browse files

Load all submissions in one query for efficience. Use sub instead of replace...

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.
parent d005a9d0
No related branches found
No related tags found
No related merge requests found
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
updated <a href="{{ activity.submission.get_absolute_url }}">{{ activity.submission.title }}</a> updated <a href="{{ activity.submission.get_absolute_url }}">{{ activity.submission.title }}</a>
{% endif %} {% 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 %} {% if not submission_title and activity|user_can_see_related:request.user %}
{% with url=activity.related_object.get_absolute_url %} {% with url=activity.related_object.get_absolute_url %}
......
...@@ -10,18 +10,15 @@ register = template.Library() ...@@ -10,18 +10,15 @@ register = template.Library()
@register.filter @register.filter
def submission_links(value): 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 = {} links = {}
for match in regex.finditer(value): if matches:
try: for submission in ApplicationSubmission.objects.filter(id__in=matches):
submission = ApplicationSubmission.objects.get(id=match[1]) links[f'\#{submission.id}'] = f'<a href="{submission.get_absolute_url()}">{submission.title} <span class="mid-grey-text">#{submission.id}</span></a>'
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: if links:
for sid, link in links.items(): for sid, link in links.items():
value = value.replace(sid, link) value = re.sub(f'(?<!\w){sid}(?!\w)', link, value)
return mark_safe(value) return mark_safe(value)
...@@ -419,11 +419,11 @@ SOCIAL_AUTH_PIPELINE = ( ...@@ -419,11 +419,11 @@ SOCIAL_AUTH_PIPELINE = (
) )
# Bleach Settings # 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 BLEACH_STRIP_TAGS = True
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment