Skip to content

Commit

Permalink
Removed humanized_simple_join tag, improved humanized_join, and polis…
Browse files Browse the repository at this point in the history
…hed tests
  • Loading branch information
Tim Baxter committed Jan 15, 2015
1 parent a1b85ee commit d999eeb
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 34 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Tango Shared Core Change Log

### 0.14.0
* Removed humanized_simple_join tag, improved humanized_join, and polished tests.

### 0.13.0
* Removed internal xmltramp copy in favor of xmltramp2.

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name='tango-shared-core',
version='0.13.0',
version='0.14.0',
author=u'Tim Baxter',
author_email='mail.baxter@gmail.com',
description='Tango shared/core functionality.',
Expand Down
29 changes: 1 addition & 28 deletions tango_shared/templatetags/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,34 +31,6 @@ def fixbreaks(value):
return value.replace('\n', ' \n')


@register.filter
def humanized_simple_join(value):
"""
Given a list of strings, format them with commas and spaces,
but with 'and' at the end.
Unlike humanize_join, this does not wrap each object in a link.
>>> humanized_simple_join(['apples', 'oranges', 'pears'])
"apples, oranges, and pears"
In a template, if mylist = ['apples', 'oranges', 'pears']
then {{ mylist|humanized_simple_join }}
will output "apples, oranges, and pears"
"""
# make everything a string to avoid errors
value = [six.u(item) for item in value]

if len(value) == 1: # one item in list
return value[0]
if len(value) == 2: # two items in list
return "%s and %s" % (value[0], value[1])

# join all but the last element
all_but_last = ", ".join(value[:-1])
return "%s, and %s" % (all_but_last, value[-1])


@register.filter
def humanized_join(value, add_links=False):
Expand All @@ -83,6 +55,7 @@ def humanized_join(value, add_links=False):
# If it's not a list, just return it
if not type(value) is list:
return value

if add_links:
try:
value = ['<a href="%s">%s</a>' % (item.get_absolute_url(), six.u(item)) for item in value]
Expand Down
10 changes: 5 additions & 5 deletions tango_shared/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,20 @@ class TemplateTagsTests(TestCase):
def setUp(self):
self.test_list = ['apples', 'oranges']

def test_humanized_join_with_one_items(self):
def test_humanized_join(self):
t = Template('{% load formatting %}{{ mylist|humanized_join }}')

# with one item
c = Context({"mylist": 'foo'})
output = t.render(c)
self.assertEqual(output, 'foo')

def test_humanized_join_with_two_items(self):
t = Template('{% load formatting %}{{ mylist|humanized_join }}')
# with two items
c = Context({"mylist": self.test_list})
output = t.render(c)
self.assertEqual(output, 'apples and oranges')

def test_humanized_join_with_more_items(self):
t = Template('{% load formatting %}{{ mylist|humanized_join }}')
# with three
self.test_list.append('pears')
c = Context({"mylist": self.test_list})
output = t.render(c)
Expand Down

0 comments on commit d999eeb

Please sign in to comment.