Fixes #1358: Correct VRF example values in IP/prefix import forms

This commit is contained in:
Jeremy Stretch 2017-07-18 10:39:09 -04:00
parent 0655834938
commit 106627da04
2 changed files with 12 additions and 8 deletions

View File

@ -44,7 +44,7 @@
<td>
{{ field.help_text|default:field.label }}
{% if field.choices %}
<br /><small class="text-muted">Choices: {{ field.choices|example_choices }}</small>
<br /><small class="text-muted">Choices: {{ field|example_choices }}</small>
{% elif field|widget_type == 'dateinput' %}
<br /><small class="text-muted">Format: YYYY-MM-DD</small>
{% elif field|widget_type == 'checkboxinput' %}

View File

@ -63,19 +63,23 @@ def bettertitle(value):
@register.filter()
def example_choices(value, arg=3):
def example_choices(field, arg=3):
"""
Returns a number (default: 3) of example choices for a ChoiceFiled (useful for CSV import forms).
"""
choices = []
for id, label in value:
if len(choices) == arg:
choices.append('etc.')
examples = []
if hasattr(field, 'queryset'):
choices = [(obj.pk, getattr(obj, field.to_field_name)) for obj in field.queryset[:arg+1]]
else:
choices = field.choices
for id, label in choices:
if len(examples) == arg:
examples.append('etc.')
break
if not id:
continue
choices.append(label)
return ', '.join(choices) or 'None'
examples.append(label)
return ', '.join(examples) or 'None'
#