Browse Source

Added dynamic examples for CSV form fields

Jeremy Stretch 8 years ago
parent
commit
d122f9f700

+ 5 - 4
netbox/circuits/forms.py

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

+ 7 - 1
netbox/dcim/forms.py

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

+ 10 - 1
netbox/templates/utilities/obj_import.html

@@ -42,7 +42,16 @@
                     <tr>
                         <td><code>{{ name }}</code></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>
                 {% endfor %}
             </table>

+ 1 - 1
netbox/utilities/forms.py

@@ -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.'
 

+ 4 - 2
netbox/utilities/templatetags/form_helpers.py

@@ -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

+ 18 - 0
netbox/utilities/templatetags/helpers.py

@@ -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
 #