Skip to content

Commit d6f2896

Browse files
committed
Fix for adamlaska#182.
* Added pass-through of non-string values for parse_email_list() and make_one_per_line(). * Added doctests for the filter changes * Added testurl for the failing /liaison/337/ - Legacy-Id: 787
1 parent ec56ec0 commit d6f2896

3 files changed

Lines changed: 69 additions & 11 deletions

File tree

ietf/idtracker/models.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from django.db import models
44
from ietf.utils import FKAsOneToOne
5+
from django.test import TestCase
56

67
class Acronym(models.Model):
78
acronym_id = models.AutoField(primary_key=True)
@@ -905,3 +906,12 @@ class DocumentWrapper(object):
905906
primary_flag = 1
906907
def __init__(self, document):
907908
self.document = document
909+
910+
class Test(TestCase):
911+
def testDoctest(self):
912+
# doctests in models.py will be automatically tested when running
913+
# django's 'test' command, but for other modules we need to make a
914+
# bit of extra effort to have doctests run.
915+
import doctest
916+
import templatetags.ietf_filters
917+
doctest.testmod(templatetags.ietf_filters)

ietf/idtracker/templatetags/ietf_filters.py

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,62 @@ def expand_comma(value):
2525
def parse_email_list(value):
2626
"""
2727
Parse a list of comma-seperated email addresses into
28-
a list of mailto: links."""
29-
addrs = re.split(", ?", value)
30-
ret = []
31-
for addr in addrs:
32-
(name, email) = emailutils.parseaddr(addr)
33-
if not(name):
34-
name = email
35-
ret.append('<a href="mailto:%s">%s</a>' % ( fix_ampersands(email), escape(name) ))
36-
return ", ".join(ret)
28+
a list of mailto: links.
3729
30+
Splitting a string of email addresses should return a list:
31+
32+
>>> parse_email_list('joe@example.org, fred@example.com')
33+
'<a href="mailto:joe@example.org">joe@example.org</a>, <a href="mailto:fred@example.com">fred@example.com</a>'
34+
35+
Parsing a non-string should return the input value, rather than fail:
36+
37+
>>> parse_email_list(['joe@example.org', 'fred@example.com'])
38+
['joe@example.org', 'fred@example.com']
39+
40+
Null input values should pass through silently:
41+
42+
>>> parse_email_list('')
43+
''
44+
45+
>>> parse_email_list(None)
46+
47+
48+
"""
49+
if value and type(value) == type(""): # testing for 'value' being true isn't necessary; it's a fast-out route
50+
addrs = re.split(", ?", value)
51+
ret = []
52+
for addr in addrs:
53+
(name, email) = emailutils.parseaddr(addr)
54+
if not(name):
55+
name = email
56+
ret.append('<a href="mailto:%s">%s</a>' % ( fix_ampersands(email), escape(name) ))
57+
return ", ".join(ret)
58+
else:
59+
return value
60+
3861
# there's an "ahref -> a href" in GEN_UTIL
3962
# but let's wait until we understand what that's for.
4063
@register.filter(name='make_one_per_line')
4164
def make_one_per_line(value):
4265
"""
43-
Turn a comma-separated list into a carraige-return-seperated list."""
44-
return re.sub(", ?", "\n", value)
66+
Turn a comma-separated list into a carraige-return-seperated list.
4567
68+
>>> make_one_per_line("a, b, c")
69+
'a\\nb\\nc'
70+
71+
Pass through non-strings:
72+
73+
>>> make_one_per_line([1, 2])
74+
[1, 2]
75+
76+
>>> make_one_per_line(None)
77+
78+
"""
79+
if value and type(value) == type(""):
80+
return re.sub(", ?", "\n", value)
81+
else:
82+
return value
83+
4684
@register.filter(name='link_if_url')
4785
def link_if_url(value):
4886
"""
@@ -174,3 +212,10 @@ def inpast(date):
174212
if date:
175213
return date < datetime.datetime.now()
176214
return True
215+
216+
def _test():
217+
import doctest
218+
doctest.testmod()
219+
220+
if __name__ == "__main__":
221+
_test()

ietf/liaisons/testurl.list

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@
55
200 /liaison/help/from_ietf/ https://datatracker.ietf.org/public/liaison_guide_from_ietf.cgi
66
200 /liaison/help/fields/ https://datatracker.ietf.org/public/liaison_field_help.cgi
77
200 /liaison/help/
8+
9+
# Test case for ticket #182:
10+
200 /liaison/337/

0 commit comments

Comments
 (0)