Skip to content

Commit

Permalink
add_device_type_components.py: update the cached component counts on …
Browse files Browse the repository at this point in the history
…the Device
  • Loading branch information
candlerb committed Jan 30, 2024
1 parent 647bcf6 commit 91f40a5
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions scripts/add_device_type_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
PowerOutlet, Interface, RearPort, FrontPort,
DeviceBay, ModuleBay)
from extras.scripts import Script, ObjectVar, MultiObjectVar
from django.db.models import F


class AddDeviceTypeComponents(Script):
class Meta:
name = "Add Device Type Components"
description = "Add missing components to selected devices"
scheduling_enabled = False

manufacturer = ObjectVar(
model=Manufacturer,
Expand Down Expand Up @@ -41,17 +43,17 @@ def run(self, data, commit):
# components per the DeviceType definition"
# Note that ordering is important: e.g. PowerPort before
# PowerOutlet, RearPort before FrontPort
for klass, item, templateitem in [
(ConsolePort, 'consoleports', 'consoleporttemplates'),
for klass, item, templateitem, counter_name in [
(ConsolePort, 'consoleports', 'consoleporttemplates', 'console_port_count'),
(ConsoleServerPort, 'consoleserverports',
'consoleserverporttemplates'),
(PowerPort, 'powerports', 'powerporttemplates'),
(PowerOutlet, 'poweroutlets', 'poweroutlettemplates'),
(Interface, 'interfaces', 'interfacetemplates'),
(RearPort, 'rearports', 'rearporttemplates'),
(FrontPort, 'frontports', 'frontporttemplates'),
(DeviceBay, 'devicebays', 'devicebaytemplates'),
(ModuleBay, 'modulebays', 'modulebaytemplates'),
'consoleserverporttemplates', 'console_server_port_count'),
(PowerPort, 'powerports', 'powerporttemplates', 'power_port_count'),
(PowerOutlet, 'poweroutlets', 'poweroutlettemplates', 'power_outlet_count'),
(Interface, 'interfaces', 'interfacetemplates', 'interface_count'),
(RearPort, 'rearports', 'rearporttemplates', 'rear_port_count'),
(FrontPort, 'frontports', 'frontporttemplates', 'front_port_count'),
(DeviceBay, 'devicebays', 'devicebaytemplates', 'device_bay_count'),
(ModuleBay, 'modulebays', 'modulebaytemplates', 'module_bay_count'),
]:
names = {i.name for i in getattr(device, item).all()}
templates = getattr(dt, templateitem).all()
Expand All @@ -64,6 +66,10 @@ def run(self, data, commit):
for i in items:
i.full_clean()
klass.objects.bulk_create(items)
# post_save hook is not called, so we have to update counters ourselves
Device.objects.filter(pk=device.pk).update(
**{counter_name: F(counter_name) + len(items)}
)
self.log_success("%s (%d): created %d %s" % (device.name,
device.id,
len(items),
Expand Down

0 comments on commit 91f40a5

Please sign in to comment.