Skip to content

Commit

Permalink
Closes #722: Enabled custom fields for device types
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Dec 16, 2016
1 parent 712567c commit b56e37a
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 16 deletions.
9 changes: 5 additions & 4 deletions netbox/dcim/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,14 @@ class Meta(ManufacturerSerializer.Meta):
# Device types
#

class DeviceTypeSerializer(serializers.ModelSerializer):
class DeviceTypeSerializer(CustomFieldSerializer, serializers.ModelSerializer):
manufacturer = ManufacturerNestedSerializer()
subdevice_role = serializers.SerializerMethodField()

class Meta:
model = DeviceType
fields = ['id', 'manufacturer', 'model', 'slug', 'part_number', 'u_height', 'is_full_depth',
'is_console_server', 'is_pdu', 'is_network_device', 'subdevice_role']
'is_console_server', 'is_pdu', 'is_network_device', 'subdevice_role', 'custom_fields']

def get_subdevice_role(self, obj):
return {
Expand Down Expand Up @@ -197,8 +197,9 @@ class DeviceTypeDetailSerializer(DeviceTypeSerializer):

class Meta(DeviceTypeSerializer.Meta):
fields = ['id', 'manufacturer', 'model', 'slug', 'part_number', 'u_height', 'is_full_depth',
'is_console_server', 'is_pdu', 'is_network_device', 'console_port_templates', 'cs_port_templates',
'power_port_templates', 'power_outlet_templates', 'interface_templates']
'is_console_server', 'is_pdu', 'is_network_device', 'subdevice_role', 'custom_fields',
'console_port_templates', 'cs_port_templates', 'power_port_templates', 'power_outlet_templates',
'interface_templates']


#
Expand Down
8 changes: 4 additions & 4 deletions netbox/dcim/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,20 @@ class ManufacturerDetailView(generics.RetrieveAPIView):
# Device Types
#

class DeviceTypeListView(generics.ListAPIView):
class DeviceTypeListView(CustomFieldModelAPIView, generics.ListAPIView):
"""
List device types (filterable)
"""
queryset = DeviceType.objects.select_related('manufacturer')
queryset = DeviceType.objects.select_related('manufacturer').prefetch_related('custom_field_values__field')
serializer_class = serializers.DeviceTypeSerializer
filter_class = filters.DeviceTypeFilter


class DeviceTypeDetailView(generics.RetrieveAPIView):
class DeviceTypeDetailView(CustomFieldModelAPIView, generics.RetrieveAPIView):
"""
Retrieve a single device type
"""
queryset = DeviceType.objects.select_related('manufacturer')
queryset = DeviceType.objects.select_related('manufacturer').prefetch_related('custom_field_values__field')
serializer_class = serializers.DeviceTypeDetailSerializer


Expand Down
2 changes: 1 addition & 1 deletion netbox/dcim/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def search(self, queryset, value):
)


class DeviceTypeFilter(django_filters.FilterSet):
class DeviceTypeFilter(CustomFieldFilterSet, django_filters.FilterSet):
manufacturer_id = django_filters.ModelMultipleChoiceFilter(
name='manufacturer',
queryset=Manufacturer.objects.all(),
Expand Down
7 changes: 4 additions & 3 deletions netbox/dcim/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ class Meta:
# Device types
#

class DeviceTypeForm(forms.ModelForm, BootstrapMixin):
class DeviceTypeForm(BootstrapMixin, CustomFieldForm):
slug = SlugField(slug_source='model')

class Meta:
Expand All @@ -263,7 +263,7 @@ class Meta:
'is_pdu', 'is_network_device', 'subdevice_role']


class DeviceTypeBulkEditForm(BulkEditForm, BootstrapMixin):
class DeviceTypeBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm):
pk = forms.ModelMultipleChoiceField(queryset=DeviceType.objects.all(), widget=forms.MultipleHiddenInput)
manufacturer = forms.ModelChoiceField(queryset=Manufacturer.objects.all(), required=False)
u_height = forms.IntegerField(min_value=1, required=False)
Expand All @@ -272,7 +272,8 @@ class Meta:
nullable_fields = []


class DeviceTypeFilterForm(forms.Form, BootstrapMixin):
class DeviceTypeFilterForm(BootstrapMixin, CustomFieldFilterForm):
model = DeviceType
manufacturer = FilterChoiceField(queryset=Manufacturer.objects.annotate(filter_count=Count('device_types')),
to_field_name='slug')

Expand Down
3 changes: 2 additions & 1 deletion netbox/dcim/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ def get_absolute_url(self):
return "{}?manufacturer={}".format(reverse('dcim:devicetype_list'), self.slug)


class DeviceType(models.Model):
class DeviceType(models.Model, CustomFieldModel):
"""
A DeviceType represents a particular make (Manufacturer) and model of device. It specifies rack height and depth, as
well as high-level functional role(s).
Expand Down Expand Up @@ -553,6 +553,7 @@ class DeviceType(models.Model):
choices=SUBDEVICE_ROLE_CHOICES,
help_text="Parent devices house child devices in device bays. Select "
"\"None\" if this device type is neither a parent nor a child.")
custom_field_values = GenericRelation(CustomFieldValue, content_type_field='obj_type', object_id_field='obj_id')

class Meta:
ordering = ['manufacturer', 'model']
Expand Down
1 change: 1 addition & 0 deletions netbox/dcim/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ class DeviceTypeEditView(PermissionRequiredMixin, ObjectEditView):
permission_required = 'dcim.change_devicetype'
model = DeviceType
form_class = forms.DeviceTypeForm
template_name = 'dcim/devicetype_edit.html'
obj_list_url = 'dcim:devicetype_list'


Expand Down
5 changes: 3 additions & 2 deletions netbox/extras/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def get_custom_fields_for_model(content_type, filterable_only=False, bulk_edit=F

# Date
elif cf.type == CF_TYPE_DATE:
field = forms.DateField(required=cf.required, initial=cf.default)
field = forms.DateField(required=cf.required, initial=cf.default, help_text="Date format: YYYY-MM-DD")

# Select
elif cf.type == CF_TYPE_SELECT:
Expand All @@ -63,7 +63,8 @@ def get_custom_fields_for_model(content_type, filterable_only=False, bulk_edit=F

field.model = cf
field.label = cf.label if cf.label else cf.name.replace('_', ' ').capitalize()
field.help_text = cf.description
if cf.description:
field.help_text = cf.description

field_dict[field_name] = field

Expand Down
2 changes: 1 addition & 1 deletion netbox/extras/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


CUSTOMFIELD_MODELS = (
'site', 'rack', 'device', # DCIM
'site', 'rack', 'devicetype', 'device', # DCIM
'aggregate', 'prefix', 'ipaddress', 'vlan', 'vrf', # IPAM
'provider', 'circuit', # Circuits
'tenant', # Tenants
Expand Down
3 changes: 3 additions & 0 deletions netbox/templates/dcim/devicetype.html
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ <h1>{{ devicetype.manufacturer }} {{ devicetype.model }}</h1>
</tr>
</table>
</div>
{% with devicetype.get_custom_fields as custom_fields %}
{% include 'inc/custom_fields_panel.html' %}
{% endwith %}
{% include 'dcim/inc/devicetype_component_table.html' with table=consoleport_table title='Console Ports' add_url='dcim:devicetype_add_consoleport' delete_url='dcim:devicetype_delete_consoleport' %}
{% include 'dcim/inc/devicetype_component_table.html' with table=powerport_table title='Power Ports' add_url='dcim:devicetype_add_powerport' delete_url='dcim:devicetype_delete_powerport' %}
{% include 'dcim/inc/devicetype_component_table.html' with table=mgmt_interface_table title='Management Interfaces' add_url='dcim:devicetype_add_interface' add_url_extra='?mgmt_only=1' edit_url='dcim:devicetype_bulkedit_interface' delete_url='dcim:devicetype_delete_interface' %}
Expand Down
28 changes: 28 additions & 0 deletions netbox/templates/dcim/devicetype_edit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{% extends 'utilities/obj_edit.html' %}
{% load form_helpers %}

{% block form %}
<div class="panel panel-default">
<div class="panel-heading"><strong>Device Type</strong></div>
<div class="panel-body">
{% render_field form.manufacturer %}
{% render_field form.model %}
{% render_field form.slug %}
{% render_field form.part_number %}
{% render_field form.u_height %}
{% render_field form.is_full_depth %}
{% render_field form.is_console_server %}
{% render_field form.is_pdu %}
{% render_field form.is_network_device %}
{% render_field form.subdevice_role %}
</div>
</div>
{% if form.custom_fields %}
<div class="panel panel-default">
<div class="panel-heading"><strong>Custom Fields</strong></div>
<div class="panel-body">
{% render_custom_fields form %}
</div>
</div>
{% endif %}
{% endblock %}

0 comments on commit b56e37a

Please sign in to comment.