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

Form cleanup and fixed child device import

This commit is contained in:
Jeremy Stretch
2017-06-07 15:30:28 -04:00
parent 0a6e4f31d5
commit 293be752ca
3 changed files with 27 additions and 21 deletions

View File

@ -117,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 circuit type', help_text='Type of circuit',
error_messages={ error_messages={
'invalid_choice': 'Invalid circuit type.' 'invalid_choice': 'Invalid circuit type.'
} }

View File

@ -19,13 +19,12 @@ from utilities.forms import (
) )
from .formfields import MACAddressFormField from .formfields import MACAddressFormField
from .models import ( from .models import (
DeviceBay, DeviceBayTemplate, CONNECTION_STATUS_CHOICES, CONNECTION_STATUS_PLANNED, CONNECTION_STATUS_CONNECTED, DeviceBay, DeviceBayTemplate, CONNECTION_STATUS_CHOICES, CONNECTION_STATUS_CONNECTED, ConsolePort,
ConsolePort, ConsolePortTemplate, ConsoleServerPort, ConsoleServerPortTemplate, Device, DeviceRole, DeviceType, ConsolePortTemplate, ConsoleServerPort, ConsoleServerPortTemplate, Device, DeviceRole, DeviceType, Interface,
Interface, IFACE_FF_CHOICES, IFACE_FF_LAG, IFACE_ORDERING_CHOICES, InterfaceConnection, InterfaceTemplate, IFACE_FF_CHOICES, IFACE_FF_LAG, IFACE_ORDERING_CHOICES, InterfaceConnection, InterfaceTemplate, Manufacturer,
Manufacturer, InventoryItem, Platform, PowerOutlet, PowerOutletTemplate, PowerPort, PowerPortTemplate, InventoryItem, Platform, PowerOutlet, PowerOutletTemplate, PowerPort, PowerPortTemplate, RACK_FACE_CHOICES,
RACK_FACE_CHOICES, RACK_TYPE_CHOICES, RACK_WIDTH_CHOICES, Rack, RackGroup, RackReservation, RackRole, RACK_TYPE_CHOICES, RACK_WIDTH_CHOICES, Rack, RackGroup, RackReservation, RackRole, RACK_WIDTH_19IN, RACK_WIDTH_23IN,
RACK_WIDTH_19IN, RACK_WIDTH_23IN, Region, Site, STATUS_CHOICES, SUBDEVICE_ROLE_CHILD, SUBDEVICE_ROLE_PARENT, Region, Site, STATUS_CHOICES, SUBDEVICE_ROLE_CHILD, SUBDEVICE_ROLE_PARENT, VIRTUAL_IFACE_TYPES,
VIRTUAL_IFACE_TYPES,
) )
@ -265,8 +264,12 @@ class RackCSVForm(forms.ModelForm):
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', 'name', 'facility_id', 'tenant', 'role', 'type', 'width', 'u_height', 'desc_units',
] ]
help_texts = {
'name': 'Rack name',
'u_height': 'Height in rack units',
}
def clean(self): def clean(self):
@ -705,13 +708,13 @@ class BaseDeviceCSVForm(forms.ModelForm):
manufacturer = forms.ModelChoiceField( manufacturer = forms.ModelChoiceField(
queryset=Manufacturer.objects.all(), queryset=Manufacturer.objects.all(),
to_field_name='name', to_field_name='name',
help_text='Manufacturer name', help_text='Device type manufacturer',
error_messages={ error_messages={
'invalid_choice': 'Invalid manufacturer.', 'invalid_choice': 'Invalid manufacturer.',
} }
) )
model_name = forms.CharField( model_name = forms.CharField(
help_text='Model name' help_text='Device type model name'
) )
platform = forms.ModelChoiceField( platform = forms.ModelChoiceField(
queryset=Platform.objects.all(), queryset=Platform.objects.all(),
@ -757,7 +760,7 @@ class DeviceCSVForm(BaseDeviceCSVForm):
) )
rack_group = forms.CharField( rack_group = forms.CharField(
required=False, required=False,
help_text='Name of parent rack\'s group' help_text='Parent rack\'s group (if any)'
) )
rack_name = forms.CharField( rack_name = forms.CharField(
required=False, required=False,
@ -774,6 +777,9 @@ class DeviceCSVForm(BaseDeviceCSVForm):
'name', 'device_role', 'tenant', 'manufacturer', 'model_name', 'platform', 'serial', 'asset_tag', 'status', 'name', 'device_role', 'tenant', 'manufacturer', 'model_name', 'platform', 'serial', 'asset_tag', 'status',
'site', 'rack_group', 'rack_name', 'position', 'face', 'site', 'rack_group', 'rack_name', 'position', 'face',
] ]
help_texts = {
'name': 'Device name',
}
def clean(self): def clean(self):
@ -826,10 +832,11 @@ class ChildDeviceCSVForm(BaseDeviceCSVForm):
if parent and device_bay_name: if parent and device_bay_name:
try: try:
self.instance.parent_bay = DeviceBay.objects.get(device=parent, name=device_bay_name) self.instance.parent_bay = DeviceBay.objects.get(device=parent, name=device_bay_name)
# Inherit site and rack from parent device
self.instance.site = parent.site
self.instance.rack = parent.rack
except DeviceBay.DoesNotExist: except DeviceBay.DoesNotExist:
self.add_error( raise forms.ValidationError("Parent device/bay ({} {}) not found".format(parent, device_bay_name))
'device_bay_name', "Parent device/bay ({} {}) not found".format(parent, device_bay_name)
)
class DeviceBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm): class DeviceBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm):

View File

@ -877,18 +877,17 @@ class ChildDeviceBulkImportView(PermissionRequiredMixin, BulkImportView):
template_name = 'dcim/device_import_child.html' template_name = 'dcim/device_import_child.html'
default_return_url = 'dcim:device_list' default_return_url = 'dcim:device_list'
def save_obj(self, obj): def _save_obj(self, obj_form):
# Inherit site and rack from parent device obj = obj_form.save()
obj.site = obj.parent_bay.device.site
obj.rack = obj.parent_bay.device.rack
obj.save()
# Save the reverse relation # Save the reverse relation to the parent device bay
device_bay = obj.parent_bay device_bay = obj.parent_bay
device_bay.installed_device = obj device_bay.installed_device = obj
device_bay.save() device_bay.save()
return obj
class DeviceBulkEditView(PermissionRequiredMixin, BulkEditView): class DeviceBulkEditView(PermissionRequiredMixin, BulkEditView):
permission_required = 'dcim.change_device' permission_required = 'dcim.change_device'