1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

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

@ -47,8 +47,9 @@ class ProviderCSVForm(forms.ModelForm):
fields = ['name', 'slug', 'asn', 'account', 'portal_url', 'comments'] fields = ['name', 'slug', 'asn', 'account', 'portal_url', 'comments']
help_texts = { help_texts = {
'name': 'Provider name', 'name': 'Provider name',
'asn': 'Autonomous system number', 'asn': '32-bit autonomous system number',
'comments': 'Free-form comments' 'portal_url': 'Portal URL',
'comments': 'Free-form comments',
} }
@ -116,7 +117,7 @@ class CircuitCSVForm(forms.ModelForm):
type = forms.ModelChoiceField( type = forms.ModelChoiceField(
queryset=CircuitType.objects.all(), queryset=CircuitType.objects.all(),
to_field_name='name', to_field_name='name',
help_text='Name of assigned tenant', help_text='Name of circuit type',
error_messages={ error_messages={
'invalid_choice': 'Invalid circuit type.' 'invalid_choice': 'Invalid circuit type.'
} }
@ -125,7 +126,7 @@ class CircuitCSVForm(forms.ModelForm):
queryset=Tenant.objects.all(), queryset=Tenant.objects.all(),
required=False, required=False,
to_field_name='name', to_field_name='name',
help_text='Name of circuit type', help_text='Name of assigned tenant',
error_messages={ error_messages={
'invalid_choice': 'Tenant not found.' 'invalid_choice': 'Tenant not found.'
} }

View File

@ -146,6 +146,10 @@ class SiteCSVForm(forms.ModelForm):
'name', 'slug', 'region', 'tenant', 'facility', 'asn', 'physical_address', 'shipping_address', 'name', 'slug', 'region', 'tenant', 'facility', 'asn', 'physical_address', 'shipping_address',
'contact_name', 'contact_phone', 'contact_email', 'comments', 'contact_name', 'contact_phone', 'contact_email', 'comments',
] ]
help_texts = {
'slug': 'URL-friendly slug',
'asn': '32-bit autonomous system number',
}
class SiteBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm): class SiteBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm):
@ -271,13 +275,15 @@ class RackCSVForm(forms.ModelForm):
'invalid_choice': 'Role not found.', 'invalid_choice': 'Role not found.',
} }
) )
type = forms.CharField(required=False)
class Meta: class Meta:
model = Rack model = Rack
fields = [ fields = [
'site', 'group', 'name', 'facility_id', 'tenant', 'role', 'type', 'width', 'u_height', 'desc_units', 'site', 'group', 'name', 'facility_id', 'tenant', 'role', 'type', 'width', 'u_height', 'desc_units',
] ]
help_texts = {
'type': 'Rack type',
}
def clean_group(self): def clean_group(self):

View File

@ -42,7 +42,16 @@
<tr> <tr>
<td><code>{{ name }}</code></td> <td><code>{{ name }}</code></td>
<td>{% if field.required %}<i class="glyphicon glyphicon-ok" title="Required"></i>{% endif %}</td> <td>{% if field.required %}<i class="glyphicon glyphicon-ok" title="Required"></i>{% endif %}</td>
<td>{{ field.help_text|default:field.label }}</td> <td>
{{ field.help_text|default:field.label }}
{% if field.choices %}
<br /><small class="text-muted">Examples: {{ field.choices|example_choices }}</small>
{% elif field|widget_type == 'dateinput' %}
<br /><small class="text-muted">Format: YYYY-MM-DD</small>
{% elif field|widget_type == 'checkboxinput' %}
<br /><small class="text-muted">Specify "true" or "false"</small>
{% endif %}
</td>
</tr> </tr>
{% endfor %} {% endfor %}
</table> </table>

View File

@ -235,7 +235,7 @@ class CSVDataField(forms.CharField):
if not self.initial: if not self.initial:
self.initial = ','.join(required_fields) + '\n' self.initial = ','.join(required_fields) + '\n'
if not self.help_text: 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 ' \ 'commas to separate values. Multi-line data and values containing commas may be wrapped ' \
'in double quotes.' 'in double quotes.'

View File

@ -40,7 +40,9 @@ def widget_type(field):
""" """
Return the widget type 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() return field.field.widget.__class__.__name__.lower()
except AttributeError: else:
return None return None

View File

@ -1,3 +1,5 @@
from __future__ import unicode_literals
from markdown import markdown from markdown import markdown
from django import template from django import template
@ -60,6 +62,22 @@ def bettertitle(value):
return ' '.join([w[0].upper() + w[1:] for w in value.split()]) 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 # Tags
# #