|
| 1 | +from django.db import migrations |
| 2 | +from django.db.models import Count |
| 3 | + |
| 4 | +import utilities.fields |
| 5 | + |
| 6 | + |
| 7 | +def recalculate_devicetype_template_counts(apps, schema_editor): |
| 8 | + DeviceType = apps.get_model("dcim", "DeviceType") |
| 9 | + device_types = list(DeviceType.objects.all().annotate( |
| 10 | + _console_port_template_count=Count('consoleporttemplates', distinct=True), |
| 11 | + _console_server_port_template_count=Count('consoleserverporttemplates', distinct=True), |
| 12 | + _power_port_template_count=Count('powerporttemplates', distinct=True), |
| 13 | + _power_outlet_template_count=Count('poweroutlettemplates', distinct=True), |
| 14 | + _interface_template_count=Count('interfacetemplates', distinct=True), |
| 15 | + _front_port_template_count=Count('frontporttemplates', distinct=True), |
| 16 | + _rear_port_template_count=Count('rearporttemplates', distinct=True), |
| 17 | + _device_bay_template_count=Count('devicebaytemplates', distinct=True), |
| 18 | + _module_bay_template_count=Count('modulebaytemplates', distinct=True), |
| 19 | + _inventory_item_template_count=Count('inventoryitemtemplates', distinct=True), |
| 20 | + )) |
| 21 | + |
| 22 | + for devicetype in device_types: |
| 23 | + devicetype.console_port_template_count = devicetype._console_port_template_count |
| 24 | + devicetype.console_server_port_template_count = devicetype._console_server_port_template_count |
| 25 | + devicetype.power_port_template_count = devicetype._power_port_template_count |
| 26 | + devicetype.power_outlet_template_count = devicetype._power_outlet_template_count |
| 27 | + devicetype.interface_template_count = devicetype._interface_template_count |
| 28 | + devicetype.front_port_template_count = devicetype._front_port_template_count |
| 29 | + devicetype.rear_port_template_count = devicetype._rear_port_template_count |
| 30 | + devicetype.device_bay_template_count = devicetype._device_bay_template_count |
| 31 | + devicetype.module_bay_template_count = devicetype._module_bay_template_count |
| 32 | + devicetype.inventory_item_template_count = devicetype._inventory_item_template_count |
| 33 | + |
| 34 | + DeviceType.objects.bulk_update(device_types, [ |
| 35 | + 'console_port_template_count', |
| 36 | + 'console_server_port_template_count', |
| 37 | + 'power_port_template_count', |
| 38 | + 'power_outlet_template_count', |
| 39 | + 'interface_template_count', |
| 40 | + 'front_port_template_count', |
| 41 | + 'rear_port_template_count', |
| 42 | + 'device_bay_template_count', |
| 43 | + 'module_bay_template_count', |
| 44 | + 'inventory_item_template_count', |
| 45 | + ]) |
| 46 | + |
| 47 | + |
| 48 | +class Migration(migrations.Migration): |
| 49 | + dependencies = [ |
| 50 | + ('dcim', '0176_device_component_counters'), |
| 51 | + ] |
| 52 | + |
| 53 | + operations = [ |
| 54 | + migrations.AddField( |
| 55 | + model_name='devicetype', |
| 56 | + name='console_port_template_count', |
| 57 | + field=utilities.fields.CounterCacheField(default=0, to_field='device_type', to_model='dcim.ConsolePortTemplate'), |
| 58 | + ), |
| 59 | + migrations.AddField( |
| 60 | + model_name='devicetype', |
| 61 | + name='console_server_port_template_count', |
| 62 | + field=utilities.fields.CounterCacheField(default=0, to_field='device_type', to_model='dcim.ConsoleServerPortTemplate'), |
| 63 | + ), |
| 64 | + migrations.AddField( |
| 65 | + model_name='devicetype', |
| 66 | + name='power_port_template_count', |
| 67 | + field=utilities.fields.CounterCacheField(default=0, to_field='device_type', to_model='dcim.PowerPortTemplate'), |
| 68 | + ), |
| 69 | + migrations.AddField( |
| 70 | + model_name='devicetype', |
| 71 | + name='power_outlet_template_count', |
| 72 | + field=utilities.fields.CounterCacheField(default=0, to_field='device_type', to_model='dcim.PowerOutletTemplate'), |
| 73 | + ), |
| 74 | + migrations.AddField( |
| 75 | + model_name='devicetype', |
| 76 | + name='interface_template_count', |
| 77 | + field=utilities.fields.CounterCacheField(default=0, to_field='device_type', to_model='dcim.InterfaceTemplate'), |
| 78 | + ), |
| 79 | + migrations.AddField( |
| 80 | + model_name='devicetype', |
| 81 | + name='front_port_template_count', |
| 82 | + field=utilities.fields.CounterCacheField(default=0, to_field='device_type', to_model='dcim.FrontPortTemplate'), |
| 83 | + ), |
| 84 | + migrations.AddField( |
| 85 | + model_name='devicetype', |
| 86 | + name='rear_port_template_count', |
| 87 | + field=utilities.fields.CounterCacheField(default=0, to_field='device_type', to_model='dcim.RearPortTemplate'), |
| 88 | + ), |
| 89 | + migrations.AddField( |
| 90 | + model_name='devicetype', |
| 91 | + name='device_bay_template_count', |
| 92 | + field=utilities.fields.CounterCacheField(default=0, to_field='device_type', to_model='dcim.DeviceBayTemplate'), |
| 93 | + ), |
| 94 | + migrations.AddField( |
| 95 | + model_name='devicetype', |
| 96 | + name='module_bay_template_count', |
| 97 | + field=utilities.fields.CounterCacheField(default=0, to_field='device_type', to_model='dcim.ModuleBayTemplate'), |
| 98 | + ), |
| 99 | + migrations.AddField( |
| 100 | + model_name='devicetype', |
| 101 | + name='inventory_item_template_count', |
| 102 | + field=utilities.fields.CounterCacheField(default=0, to_field='device_type', to_model='dcim.InventoryItemTemplate'), |
| 103 | + ), |
| 104 | + migrations.RunPython( |
| 105 | + recalculate_devicetype_template_counts, |
| 106 | + reverse_code=migrations.RunPython.noop |
| 107 | + ), |
| 108 | + ] |
0 commit comments