Skip to content

Commit 92df44f

Browse files
author
Bill Filler
authored
Merge pull request #1100 from edx/bfiller/linkify-submission
WL-1452 | add support for linkifying submissions
2 parents 7cb152b + c223fa0 commit 92df44f

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

openassessment/templates/openassessmentblock/oa_submission_answer.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{% spaceless %}
22
{% load i18n %}
3+
{% load oa_extras %}
34
<ol class="submission__answer__display__content">
45
{% for part in answer.parts %}
56
<li class="submission__answer__part">
@@ -19,7 +20,7 @@ <h5 class="submission__answer__part__text__title">{% trans "The question for thi
1920
<div class="submission__answer__part__text">
2021
<h5 class="submission__answer__part__text__title">{{ answer_text_label }}</h5>
2122
<div class="submission__answer__part__text__value">
22-
{{ part.text|linebreaks }}
23+
{{ part.text|escape|link_and_linebreak }}
2324
</div>
2425
</div>
2526
{% endif %}

openassessment/templatetags/__init__.py

Whitespace-only changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from django import template
2+
from django.template.defaultfilters import linebreaks, stringfilter
3+
from django.utils.safestring import mark_safe
4+
from django.utils.html import conditional_escape
5+
from bleach import callbacks
6+
7+
import bleach
8+
9+
register = template.Library()
10+
11+
12+
@register.filter()
13+
@stringfilter
14+
def link_and_linebreak(text):
15+
"""
16+
Converts URLs in text into clickable links with their target attribute set to `_blank`.
17+
It wraps givent tags into <p> tags and converts line breaks(\n) to <br> tags.
18+
Args:
19+
text: (str) Text having URLs to be converted
20+
Returns: (str) Text with URLs convert to links
21+
"""
22+
if text:
23+
escaped_text = conditional_escape(text)
24+
return mark_safe(linebreaks(bleach.linkify(escaped_text, callbacks=[callbacks.target_blank])))
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import ddt
2+
from django.template import Context, Template
3+
import unittest
4+
5+
6+
@ddt.ddt
7+
class OAExtrasTests(unittest.TestCase):
8+
9+
template = Template(
10+
"{% load oa_extras %}"
11+
"{{ text|link_and_linebreak }}"
12+
)
13+
14+
@ddt.data(
15+
("", ""),
16+
('check this https://dummy-url.com', 'https://dummy-url.com'),
17+
('Visit this URL http://dummy-url.com', 'http://dummy-url.com'),
18+
('dummy-text http://dummy-url.org', 'http://dummy-url.org'),
19+
('dummy-url.com dummy-text', 'dummy-url.com')
20+
)
21+
@ddt.unpack
22+
def test_link_and_linebreak(self, text, link_text):
23+
rendered_template = self.template.render(Context({'text': text}))
24+
self.assertIn(link_text, rendered_template)
25+
if text:
26+
self.assertRegexpMatches(
27+
rendered_template,
28+
r'<a.*target="_blank".*>{link_text}</a>'.format(link_text=link_text),
29+
)
30+
31+
@ddt.data(
32+
("hello <script></script>", "script"),
33+
("http://dummy-url.com <applet></applet>", "applet"),
34+
("<iframe></iframe>", "iframe"),
35+
("<link></link>", "link"),
36+
)
37+
@ddt.unpack
38+
def test_html_tags(self, text, tag):
39+
rendered_template = self.template.render(Context({'text': text}))
40+
escaped_tag = "&lt;{tag}&gt;".format(tag=tag)
41+
self.assertIn(escaped_tag, rendered_template)

requirements/base.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ git+https://github.com/edx/[email protected]#egg=XBlock==1.0.1
55
git+https://github.com/edx/[email protected]#egg=xblock-sdk==0.1.4
66

77
# Third Party Requirements
8+
bleach==1.4
9+
html5lib==0.999
810
boto>=2.39.0,<3.0.0
911
python-swiftclient>=3.1.0,<4.0.0
1012
defusedxml>=0.4.1,<1.0.0

0 commit comments

Comments
 (0)