Skip to content

Commit

Permalink
feat: add simple view tag render_custom_inline_list
Browse files Browse the repository at this point in the history
Co-authored-by: @jwindeck
Signed-off-by: David Wallace <david.wallace@tu-darmstadt.de>
  • Loading branch information
MyPyDavid committed Apr 5, 2024
1 parent c945d7d commit bb7acb8
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions rdmo/views/templatetags/view_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,33 @@ def get_set(context, attribute, set_prefix='', project=None):
return get_sets(context, attribute, set_prefix=set_prefix, project=project)


@register.simple_tag
def render_custom_inline_list(values=None, sep=',', sep_last=', and'):
values = values or []

if not values:
return ''

if len(values) == 1:
return values[0]['value_and_unit']

if len(values) == 2:
if sep_last.startswith(', '): # for english sep_last
sep_last = sep_last.split(', ')[-1]
return f"{values[0]['value_and_unit']} {sep_last} {values[1]['value_and_unit']}"

_text = ''
for n,value in enumerate(values):
if n == len(values) - 1 and _text:
_text += f"{sep_last} "
_text += value['value_and_unit']
if n < len(values) - 2:
_text += f"{sep} "
else:
_text += ' '
return _text


@register.inclusion_tag('views/tags/value.html', takes_context=True)
def render_value(context, attribute, set_prefix='', set_index=0, index=0, project=None):
context['value'] = get_value(context, attribute, set_prefix=set_prefix, set_index=set_index,
Expand Down

0 comments on commit bb7acb8

Please sign in to comment.