Added dynamic examples for CSV form fields

This commit is contained in:
Jeremy Stretch
2017-06-06 17:27:26 -04:00
parent 1ddd7415cb
commit d122f9f700
6 changed files with 45 additions and 9 deletions

View File

@@ -235,7 +235,7 @@ class CSVDataField(forms.CharField):
if not self.initial:
self.initial = ','.join(required_fields) + '\n'
if not self.help_text:
self.help_text = 'Enter the list of column headers followed by one line per record to be imported. Use ' \
self.help_text = 'Enter the list of column headers followed by one line per record to be imported, using ' \
'commas to separate values. Multi-line data and values containing commas may be wrapped ' \
'in double quotes.'

View File

@@ -40,7 +40,9 @@ def widget_type(field):
"""
Return the widget type
"""
try:
if hasattr(field, 'widget'):
return field.widget.__class__.__name__.lower()
elif hasattr(field, 'field'):
return field.field.widget.__class__.__name__.lower()
except AttributeError:
else:
return None

View File

@@ -1,3 +1,5 @@
from __future__ import unicode_literals
from markdown import markdown
from django import template
@@ -60,6 +62,22 @@ def bettertitle(value):
return ' '.join([w[0].upper() + w[1:] for w in value.split()])
@register.filter()
def example_choices(value, 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.')
break
if not id:
continue
choices.append(label)
return ', '.join(choices) or 'None found'
#
# Tags
#