Fix for #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
This commit is contained in:
parent
ec56ec02e9
commit
d6f28962ff
|
@ -2,6 +2,7 @@
|
|||
|
||||
from django.db import models
|
||||
from ietf.utils import FKAsOneToOne
|
||||
from django.test import TestCase
|
||||
|
||||
class Acronym(models.Model):
|
||||
acronym_id = models.AutoField(primary_key=True)
|
||||
|
@ -905,3 +906,12 @@ class DocumentWrapper(object):
|
|||
primary_flag = 1
|
||||
def __init__(self, document):
|
||||
self.document = document
|
||||
|
||||
class Test(TestCase):
|
||||
def testDoctest(self):
|
||||
# doctests in models.py will be automatically tested when running
|
||||
# django's 'test' command, but for other modules we need to make a
|
||||
# bit of extra effort to have doctests run.
|
||||
import doctest
|
||||
import templatetags.ietf_filters
|
||||
doctest.testmod(templatetags.ietf_filters)
|
||||
|
|
|
@ -25,24 +25,62 @@ def expand_comma(value):
|
|||
def parse_email_list(value):
|
||||
"""
|
||||
Parse a list of comma-seperated email addresses into
|
||||
a list of mailto: links."""
|
||||
addrs = re.split(", ?", value)
|
||||
ret = []
|
||||
for addr in addrs:
|
||||
(name, email) = emailutils.parseaddr(addr)
|
||||
if not(name):
|
||||
name = email
|
||||
ret.append('<a href="mailto:%s">%s</a>' % ( fix_ampersands(email), escape(name) ))
|
||||
return ", ".join(ret)
|
||||
a list of mailto: links.
|
||||
|
||||
Splitting a string of email addresses should return a list:
|
||||
|
||||
>>> parse_email_list('joe@example.org, fred@example.com')
|
||||
'<a href="mailto:joe@example.org">joe@example.org</a>, <a href="mailto:fred@example.com">fred@example.com</a>'
|
||||
|
||||
Parsing a non-string should return the input value, rather than fail:
|
||||
|
||||
>>> parse_email_list(['joe@example.org', 'fred@example.com'])
|
||||
['joe@example.org', 'fred@example.com']
|
||||
|
||||
Null input values should pass through silently:
|
||||
|
||||
>>> parse_email_list('')
|
||||
''
|
||||
|
||||
>>> parse_email_list(None)
|
||||
|
||||
|
||||
"""
|
||||
if value and type(value) == type(""): # testing for 'value' being true isn't necessary; it's a fast-out route
|
||||
addrs = re.split(", ?", value)
|
||||
ret = []
|
||||
for addr in addrs:
|
||||
(name, email) = emailutils.parseaddr(addr)
|
||||
if not(name):
|
||||
name = email
|
||||
ret.append('<a href="mailto:%s">%s</a>' % ( fix_ampersands(email), escape(name) ))
|
||||
return ", ".join(ret)
|
||||
else:
|
||||
return value
|
||||
|
||||
# there's an "ahref -> a href" in GEN_UTIL
|
||||
# but let's wait until we understand what that's for.
|
||||
@register.filter(name='make_one_per_line')
|
||||
def make_one_per_line(value):
|
||||
"""
|
||||
Turn a comma-separated list into a carraige-return-seperated list."""
|
||||
return re.sub(", ?", "\n", value)
|
||||
Turn a comma-separated list into a carraige-return-seperated list.
|
||||
|
||||
>>> make_one_per_line("a, b, c")
|
||||
'a\\nb\\nc'
|
||||
|
||||
Pass through non-strings:
|
||||
|
||||
>>> make_one_per_line([1, 2])
|
||||
[1, 2]
|
||||
|
||||
>>> make_one_per_line(None)
|
||||
|
||||
"""
|
||||
if value and type(value) == type(""):
|
||||
return re.sub(", ?", "\n", value)
|
||||
else:
|
||||
return value
|
||||
|
||||
@register.filter(name='link_if_url')
|
||||
def link_if_url(value):
|
||||
"""
|
||||
|
@ -174,3 +212,10 @@ def inpast(date):
|
|||
if date:
|
||||
return date < datetime.datetime.now()
|
||||
return True
|
||||
|
||||
def _test():
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
|
||||
if __name__ == "__main__":
|
||||
_test()
|
||||
|
|
|
@ -5,3 +5,6 @@
|
|||
200 /liaison/help/from_ietf/ https://datatracker.ietf.org/public/liaison_guide_from_ietf.cgi
|
||||
200 /liaison/help/fields/ https://datatracker.ietf.org/public/liaison_field_help.cgi
|
||||
200 /liaison/help/
|
||||
|
||||
# Test case for ticket #182:
|
||||
200 /liaison/337/
|
||||
|
|
Loading…
Reference in a new issue