Skip to content

Commit 5ea4c37

Browse files
committed
Add templatefilter joinandor
This filter takes a list of a,b,c,d and turns it into "a, b, c and d" or "a, b, c or d" depending on parameter given.
1 parent 74d9a03 commit 5ea4c37

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

pgweb/core/templatetags/pgfilters.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,16 @@ def release_notes_pg_minor_version(minor_version, major_version):
8080
if str(major_version) in ['0', '1']:
8181
return str(minor_version)[2:4]
8282
return minor_version
83+
84+
85+
@register.filter()
86+
def joinandor(value, andor):
87+
# Value is a list of objects. Join them on comma, add "and" or "or" before the last.
88+
if len(value) == 1:
89+
return str(value)
90+
91+
if not isinstance(value, list):
92+
# Must have a list to index from the end
93+
value = list(value)
94+
95+
return ", ".join([str(x) for x in value[:-1]]) + ' ' + andor + ' ' + str(value[-1])

0 commit comments

Comments
 (0)