Skip to content

Commit 0ed9c88

Browse files
Tim Daviesedugomez
authored andcommitted
Made a single tag filter join_with
1 parent e7f9dbc commit 0ed9c88

File tree

2 files changed

+18
-46
lines changed

2 files changed

+18
-46
lines changed

prefix_finder/frontend/templates/results.html

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ <h4><a href="{% url 'list' result.code %}" data-toggle="tooltip" data-placement=
2020
This is a <strong>{{ result.listType }}</strong> list.
2121
{% if result.listType = "primary" %}
2222
Organisations are generally entered into this list when they are registered
23-
{% if result.structure %} as a {{ result.structure_titles|join_or:True }} {% endif %}
23+
{% if result.structure %} as a {{ result.structure_titles|join_with:'or'|lower }} {% endif %}
2424
{% if result.subnationalCoverage %}
25-
in {{ result.subnationalCoverage_titles|join_or }}.
25+
in {{ result.subnationalCoverage_titles|join_with:'or' }}.
2626
{% else %}
27-
in {{ result.coverage_titles|join_or }}.
27+
in {{ result.coverage_titles|join_with:'or' }}.
2828
{% endif %}
2929
{% endif %}
3030
{% if result.listType = "secondary" %}
31-
{% if result.structure %}{{ result.structure_titles|join_or }} organisations{% else %}{% if result.sector %}{{ result.sector_titles|join_or }} organisations{% else %} Organisations{% endif %}{% endif %} may be entered onto this offical list as a result of a transaction with the list owner.
31+
{% if result.structure %}{{ result.structure_titles|join_with:'or' }} organisations{% else %}{% if result.sector %}{{ result.sector_titles|join_with:'or' }} organisations{% else %} Organisations{% endif %}{% endif %} may be entered onto this offical list as a result of a transaction with the list owner.
3232
{% endif %}
3333
{% if result.listType = "third_party" %}
3434
This list is maintained by a third-party who may or may not have had interactions or transactions with the organiastions listed.
@@ -38,13 +38,13 @@ <h4><a href="{% url 'list' result.code %}" data-toggle="tooltip" data-placement=
3838
{% endif %}
3939
</p>
4040
{% if result.coverage %}<p>
41-
This list covers the following countries: {{ result.coverage_titles|join_and }}
41+
This list covers the following countries: {{ result.coverage_titles|join_with:'and' }}.
4242
</p>{% endif %}
4343
{% if result.structure %}<p>
44-
It includes organisations of the following types: {{ result.structure_titles|join_or }}
44+
It includes organisations of the following types: {{ result.structure_titles|join_with:'or' }}.
4545
</p>{% endif %}
4646
{% if result.sector %}<p>
47-
It includes organisations working in the following sectors: {{ result.sector_titles|join_or }}
47+
It includes organisations working in the following sectors: {{ result.sector_titles|join_with:'or' }}
4848
</p>{% endif %}
4949
<a class="pull-right" href="{% url 'list' result.code %}"><button class="btn" style="margin-top:25px;">More info</button></a>
5050
<hr/>
@@ -60,4 +60,4 @@ <h4><a href="{% url 'list' result.code %}" data-toggle="tooltip" data-placement=
6060
</div>
6161
{% if forloop.counter|divisibleby:2 %}</div><div class="row">{% endif %}
6262
{% endfor %}
63-
</div>
63+
</div>

prefix_finder/frontend/templatetags/results.py

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -80,49 +80,21 @@ def tidy_results(results, length=None):
8080

8181

8282
@register.filter
83-
def join_or(value,lowercase=False):
83+
def join_with(value, conj):
8484
"""Given a list of strings, format them with commas and spaces, but
85-
with 'and' at the end.
85+
with 'and' or 'or' at the end.
8686
87-
>>> join_and(['apples', 'oranges', 'pears'])
87+
>>> join_with(['apples', 'oranges', 'pears'], 'and')
8888
"apples, oranges, and pears"
89-
9089
"""
91-
# convert numbers to strings
92-
if(value):
93-
if lowercase:
94-
value = [str(item).lower() for item in value]
95-
else:
96-
value = [str(item) for item in value]
97-
98-
if len(value) == 1:
99-
return value[0]
100-
101-
# join all but the last element
102-
all_but_last = ", ".join(value[:-1])
103-
return "%s or %s" % (all_but_last, value[-1])
104-
else:
90+
if not value:
10591
return ""
92+
if len(value) == 1:
93+
return value[0]
10694

107-
@register.filter
108-
def join_and(value):
109-
"""Given a list of strings, format them with commas and spaces, but
110-
with 'and' at the end.
111-
112-
>>> join_and(['apples', 'oranges', 'pears'])
113-
"apples, oranges, and pears"
114-
115-
"""
11695
# convert numbers to strings
117-
if(value):
118-
119-
value = [str(item) for item in value]
120-
121-
if len(value) == 1:
122-
return value[0]
96+
value = [str(item) for item in value]
12397

124-
# join all but the last element
125-
all_but_last = ", ".join(value[:-1])
126-
return "%s and %s" % (all_but_last, value[-1])
127-
else:
128-
return ""
98+
# join all but the last element
99+
all_but_last = ", ".join(value[:-1])
100+
return "%s %s %s" % (all_but_last, conj, value[-1])

0 commit comments

Comments
 (0)