mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
Fixes #3950: Cloned Device Form does not retain device type
This commit is contained in:
@@ -66,6 +66,14 @@
|
||||
"slug": "servertech"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "dcim.manufacturer",
|
||||
"pk": 4,
|
||||
"fields": {
|
||||
"name": "Dell",
|
||||
"slug": "dell"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "dcim.devicetype",
|
||||
"pk": 1,
|
||||
@@ -144,6 +152,19 @@
|
||||
"is_full_depth": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "dcim.devicetype",
|
||||
"pk": 7,
|
||||
"fields": {
|
||||
"created": "2016-06-23",
|
||||
"last_updated": "2016-06-23T03:19:56.521Z",
|
||||
"manufacturer": 4,
|
||||
"model": "PowerEdge R640",
|
||||
"slug": "poweredge-r640",
|
||||
"u_height": 1,
|
||||
"is_full_depth": false
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "dcim.consoleporttemplate",
|
||||
"pk": 1,
|
||||
@@ -1880,6 +1901,15 @@
|
||||
"color": "yellow"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "dcim.devicerole",
|
||||
"pk": 7,
|
||||
"fields": {
|
||||
"name": "Server",
|
||||
"slug": "server",
|
||||
"color": "grey"
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "dcim.platform",
|
||||
"pk": 1,
|
||||
@@ -2127,6 +2157,34 @@
|
||||
"comments": ""
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "dcim.device",
|
||||
"pk": 13,
|
||||
"fields": {
|
||||
"local_context_data": null,
|
||||
"created": "2016-06-23",
|
||||
"last_updated": "2016-06-23T03:19:56.521Z",
|
||||
"device_type": 7,
|
||||
"device_role": 6,
|
||||
"tenant": null,
|
||||
"platform": null,
|
||||
"name": "test1-server1",
|
||||
"serial": "",
|
||||
"asset_tag": null,
|
||||
"site": 1,
|
||||
"rack": 2,
|
||||
"position": null,
|
||||
"face": "",
|
||||
"status": true,
|
||||
"primary_ip4": null,
|
||||
"primary_ip6": null,
|
||||
"cluster": 4,
|
||||
"virtual_chassis": null,
|
||||
"vc_position": null,
|
||||
"vc_priority": null,
|
||||
"comments": ""
|
||||
}
|
||||
},
|
||||
{
|
||||
"model": "dcim.consoleport",
|
||||
"pk": 1,
|
||||
|
@@ -1636,13 +1636,22 @@ class DeviceForm(BootstrapMixin, TenancyForm, CustomFieldForm):
|
||||
instance = kwargs.get('instance')
|
||||
if 'initial' not in kwargs:
|
||||
kwargs['initial'] = {}
|
||||
|
||||
# Using hasattr() instead of "is not None" to avoid RelatedObjectDoesNotExist on required field
|
||||
if instance and hasattr(instance, 'device_type'):
|
||||
kwargs['initial']['manufacturer'] = instance.device_type.manufacturer
|
||||
if instance and instance.cluster is not None:
|
||||
kwargs['initial']['cluster_group'] = instance.cluster.group
|
||||
|
||||
if 'device_type' in kwargs['initial'] and 'manufacturer' not in kwargs['initial']:
|
||||
device_type_id = kwargs['initial']['device_type']
|
||||
manufacturer_id = DeviceType.objects.filter(pk=device_type_id).values_list('manufacturer__pk', flat=True).first()
|
||||
kwargs['initial']['manufacturer'] = manufacturer_id
|
||||
|
||||
if 'cluster' in kwargs['initial'] and 'cluster_group' not in kwargs['initial']:
|
||||
cluster_id = kwargs['initial']['cluster']
|
||||
cluster_group_id = Cluster.objects.filter(pk=cluster_id).values_list('group__pk', flat=True).first()
|
||||
kwargs['initial']['cluster_group'] = cluster_group_id
|
||||
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
if self.instance.pk:
|
||||
|
@@ -1409,7 +1409,7 @@ class Device(ChangeLoggedModel, ConfigContextModel, CustomFieldModel):
|
||||
'site', 'rack_group', 'rack_name', 'position', 'face', 'comments',
|
||||
]
|
||||
clone_fields = [
|
||||
'device_type', 'device_type__manufacturer', 'device_role', 'tenant', 'platform', 'site', 'rack', 'status', 'cluster', 'cluster__group',
|
||||
'device_type', 'device_role', 'tenant', 'platform', 'site', 'rack', 'status', 'cluster',
|
||||
]
|
||||
|
||||
STATUS_CLASS_MAP = {
|
||||
|
@@ -10,7 +10,7 @@ def get_id(model, slug):
|
||||
|
||||
class DeviceTestCase(TestCase):
|
||||
|
||||
fixtures = ['dcim', 'ipam']
|
||||
fixtures = ['dcim', 'ipam', 'virtualization']
|
||||
|
||||
def test_racked_device(self):
|
||||
test = DeviceForm(data={
|
||||
@@ -78,3 +78,15 @@ class DeviceTestCase(TestCase):
|
||||
})
|
||||
self.assertTrue(test.is_valid())
|
||||
self.assertTrue(test.save())
|
||||
|
||||
def test_cloned_cluster_device_initial_data(self):
|
||||
test = DeviceForm(initial={
|
||||
'device_type': get_id(DeviceType, 'poweredge-r640'),
|
||||
'device_role': get_id(DeviceRole, 'server'),
|
||||
'status': DeviceStatusChoices.STATUS_ACTIVE,
|
||||
'site': get_id(Site, 'test1'),
|
||||
"cluster": Cluster.objects.get(id=4).id,
|
||||
})
|
||||
self.assertEqual(test.initial['manufacturer'], get_id(Manufacturer, 'dell'))
|
||||
self.assertIn('cluster_group', test.initial)
|
||||
self.assertEqual(test.initial['cluster_group'], get_id(ClusterGroup, 'vm-host'))
|
||||
|
Reference in New Issue
Block a user