Skip to content

Commit

Permalink
Update CSV export fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
progala committed Oct 6, 2023
1 parent 1879069 commit 41e3d45
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion nautobot_device_lifecycle_mgmt/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
def create_devices():
"""Create devices for tests."""
device_platform, _ = Platform.objects.get_or_create(name="Cisco IOS", slug="cisco_ios")
manufacturer, _ = Manufacturer.objects.get_or_create(slug="cisco")
manufacturer, _ = Manufacturer.objects.get_or_create(name="Cisco", slug="cisco")
device_type, _ = DeviceType.objects.get_or_create(manufacturer=manufacturer, model="6509-E", slug="6509-e")
device_role, _ = DeviceRole.objects.get_or_create(name="Core Switch", slug="core-switch")
site, _ = Site.objects.get_or_create(name="Test 1", slug="test-1")
Expand Down
22 changes: 13 additions & 9 deletions nautobot_device_lifecycle_mgmt/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,8 @@ def test_contract_devices_export(self):
obj_perm.users.add(self.user)
obj_perm.object_types.add(ContentType.objects.get_for_model(ContractLCM))
obj_perm.object_types.add(ContentType.objects.get_for_model(Device))
# obj_perm.object_types.add(ContentType.objects.get_for_model(DeviceType))
# obj_perm.object_types.add(ContentType.objects.get_for_model(Manufacturer))

contract1 = ContractLCM.objects.filter(name="CiscoHW1").first()
response = self.client.get(f"{contract1.get_absolute_url()}?export_contract=devices")
Expand All @@ -594,9 +596,9 @@ def test_contract_devices_export(self):
response_body = response.content.decode(response.charset)
self.assertEqual(
response_body,
"Contract Name,Contract Type,Device Name,Device Serial,Device Site"
"\nCiscoHW1,Hardware,sw1,,Test 1"
"\nCiscoHW1,Hardware,sw2,,Test 1",
"Contract Name,Contract Type,Device Name,Device Serial,Device Manufacturer,Device Site"
"\nCiscoHW1,Hardware,sw1,,Cisco,Test 1"
"\nCiscoHW1,Hardware,sw2,,Cisco,Test 1",
)

contract2 = ContractLCM.objects.filter(name="AristaHW1").first()
Expand All @@ -606,7 +608,8 @@ def test_contract_devices_export(self):
response_body = response.content.decode(response.charset)
self.assertEqual(
response_body,
"Contract Name,Contract Type,Device Name,Device Serial,Device Site\nAristaHW1,Hardware,sw3,,Test 1",
"Contract Name,Contract Type,Device Name,Device Serial,Device Manufacturer,Device Site"
"\nAristaHW1,Hardware,sw3,,Cisco,Test 1",
)

def test_contract_inventoryitems_export(self):
Expand All @@ -617,6 +620,7 @@ def test_contract_inventoryitems_export(self):
obj_perm.users.add(self.user)
obj_perm.object_types.add(ContentType.objects.get_for_model(ContractLCM))
obj_perm.object_types.add(ContentType.objects.get_for_model(InventoryItem))
# obj_perm.object_types.add(ContentType.objects.get_for_model(Manufacturer))

contract1 = ContractLCM.objects.filter(name="CiscoHW1").first()
response = self.client.get(f"{contract1.get_absolute_url()}?export_contract=inventoryitems")
Expand All @@ -625,8 +629,8 @@ def test_contract_inventoryitems_export(self):
response_body = response.content.decode(response.charset)
self.assertEqual(
response_body,
"Contract Name,Contract Type,Item Name,Item Part ID,Item Serial,Item Parent Device,Item Site"
"\nCiscoHW1,Hardware,SUP2T Card,VS-S2T-10G,,sw1,Test 1",
"Contract Name,Contract Type,Item Name,Item Part ID,Item Serial,Item Manufacturer,Item Parent Device,Item Site"
"\nCiscoHW1,Hardware,SUP2T Card,VS-S2T-10G,,Cisco,sw1,Test 1",
)

contract2 = ContractLCM.objects.filter(name="AristaHW1").first()
Expand All @@ -636,7 +640,7 @@ def test_contract_inventoryitems_export(self):
response_body = response.content.decode(response.charset)
self.assertEqual(
response_body,
"Contract Name,Contract Type,Item Name,Item Part ID,Item Serial,Item Parent Device,Item Site"
"\nAristaHW1,Hardware,100GBASE-SR4 QSFP Transceiver,QSFP-100G-SR4-S,,sw2,Test 1"
"\nAristaHW1,Hardware,48x RJ-45 Line Card,WS-X6548-GE-TX,,sw3,Test 1",
"Contract Name,Contract Type,Item Name,Item Part ID,Item Serial,Item Manufacturer,Item Parent Device,Item Site"
"\nAristaHW1,Hardware,100GBASE-SR4 QSFP Transceiver,QSFP-100G-SR4-S,,Cisco,sw2,Test 1"
"\nAristaHW1,Hardware,48x RJ-45 Line Card,WS-X6548-GE-TX,,Cisco,sw3,Test 1",
)
18 changes: 15 additions & 3 deletions nautobot_device_lifecycle_mgmt/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,14 +929,25 @@ def export_csv(self, request, contract, object_type):
"""Export queryset of objects as comma-separated value (CSV)."""
csv_data = []
if object_type == "devices":
csv_data.append(",".join(["Contract Name", "Contract Type", "Device Name", "Device Serial", "Device Site"]))
csv_data.append(
",".join(
[
"Contract Name",
"Contract Type",
"Device Name",
"Device Serial",
"Device Manufacturer",
"Device Site",
]
)
)
qs = (
Device.objects.filter(
id__in=RelationshipAssociation.objects.filter(
relationship__slug="contractlcm-to-device", source_id=contract.pk
).values_list("destination_id", flat=True)
)
.values("name", "serial", "site__name")
.values("name", "serial", "device_type__manufacturer__name", "site__name")
.restrict(request.user, "view")
)
elif object_type == "inventoryitems":
Expand All @@ -948,6 +959,7 @@ def export_csv(self, request, contract, object_type):
"Item Name",
"Item Part ID",
"Item Serial",
"Item Manufacturer",
"Item Parent Device",
"Item Site",
]
Expand All @@ -959,7 +971,7 @@ def export_csv(self, request, contract, object_type):
relationship__slug="contractlcm-to-inventoryitem", source_id=contract.pk
).values_list("destination_id", flat=True)
)
.values("name", "part_id", "serial", "device__name", "device__site__name")
.values("name", "part_id", "serial", "manufacturer__name", "device__name", "device__site__name")
.restrict(request.user, "view")
)
else:
Expand Down

0 comments on commit 41e3d45

Please sign in to comment.