Initialize clean CSR inventory repository
This commit is contained in:
237
inventory/migrations/0009_split_device_specifications.py
Normal file
237
inventory/migrations/0009_split_device_specifications.py
Normal file
@@ -0,0 +1,237 @@
|
||||
# Generated by Django 5.2.17 on 2026-09-26 10:54
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
def copy_to_specifications(apps, schema_editor):
|
||||
Device = apps.get_model("inventory", "Device")
|
||||
Computer = apps.get_model("inventory", "ComputerSpecification")
|
||||
Monitor = apps.get_model("inventory", "MonitorSpecification")
|
||||
Printer = apps.get_model("inventory", "PrinterSpecification")
|
||||
for device in Device.objects.select_related("device_type").iterator(chunk_size=500):
|
||||
label = device.device_type.label.strip().casefold() if device.device_type else ""
|
||||
if label == "drucker":
|
||||
Printer.objects.create(device_id=device.pk, wifi=device.wifi, lan=device.lan)
|
||||
elif label == "bildschirm":
|
||||
Monitor.objects.create(device_id=device.pk, display_inches=device.display_inches)
|
||||
else:
|
||||
Computer.objects.create(
|
||||
device_id=device.pk, cpu=device.cpu, hwid=device.hwid,
|
||||
ram_gb=device.ram_gb, ssd_gb=device.ssd_gb, hdd_gb=device.hdd_gb,
|
||||
display_inches=device.display_inches, battery=device.battery,
|
||||
wifi=device.wifi, lan=device.lan,
|
||||
)
|
||||
|
||||
|
||||
def copy_back_to_devices(apps, schema_editor):
|
||||
Device = apps.get_model("inventory", "Device")
|
||||
Computer = apps.get_model("inventory", "ComputerSpecification")
|
||||
Monitor = apps.get_model("inventory", "MonitorSpecification")
|
||||
Printer = apps.get_model("inventory", "PrinterSpecification")
|
||||
for device in Device.objects.select_related("device_type").iterator(chunk_size=500):
|
||||
label = device.device_type.label.strip().casefold() if device.device_type else ""
|
||||
if label == "drucker":
|
||||
spec = Printer.objects.filter(device_id=device.pk).first()
|
||||
elif label == "bildschirm":
|
||||
spec = Monitor.objects.filter(device_id=device.pk).first()
|
||||
else:
|
||||
spec = Computer.objects.filter(device_id=device.pk).first()
|
||||
if spec:
|
||||
Device.objects.filter(pk=device.pk).update(**{
|
||||
name: getattr(spec, name) for name in (
|
||||
"cpu", "hwid", "ram_gb", "ssd_gb", "hdd_gb", "display_inches", "battery", "wifi", "lan",
|
||||
) if hasattr(spec, name)
|
||||
})
|
||||
|
||||
|
||||
def computer_search_index(apps, schema_editor):
|
||||
if schema_editor.connection.vendor == "postgresql":
|
||||
schema_editor.execute(
|
||||
"CREATE INDEX IF NOT EXISTS inventory_computer_cpu_trgm "
|
||||
"ON inventory_computerspecification USING gin (cpu gin_trgm_ops)"
|
||||
)
|
||||
|
||||
|
||||
def drop_computer_search_index(apps, schema_editor):
|
||||
if schema_editor.connection.vendor == "postgresql":
|
||||
schema_editor.execute("DROP INDEX IF EXISTS inventory_computer_cpu_trgm")
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("inventory", "0008_normalize_hardware_by_type"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name="ComputerSpecification",
|
||||
fields=[
|
||||
(
|
||||
"device",
|
||||
models.OneToOneField(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
primary_key=True,
|
||||
related_name="computer_specification",
|
||||
serialize=False,
|
||||
to="inventory.device",
|
||||
),
|
||||
),
|
||||
("cpu", models.CharField(blank=True, db_index=True, max_length=200)),
|
||||
("hwid", models.CharField(blank=True, db_index=True, max_length=255)),
|
||||
(
|
||||
"ram_gb",
|
||||
models.PositiveIntegerField(blank=True, db_index=True, null=True),
|
||||
),
|
||||
(
|
||||
"ssd_gb",
|
||||
models.PositiveIntegerField(blank=True, db_index=True, null=True),
|
||||
),
|
||||
(
|
||||
"hdd_gb",
|
||||
models.PositiveIntegerField(blank=True, db_index=True, null=True),
|
||||
),
|
||||
(
|
||||
"display_inches",
|
||||
models.DecimalField(
|
||||
blank=True, decimal_places=1, max_digits=4, null=True
|
||||
),
|
||||
),
|
||||
(
|
||||
"battery",
|
||||
models.CharField(
|
||||
choices=[
|
||||
("YES", "JA"),
|
||||
("NO", "NEIN"),
|
||||
("UNKNOWN", "UNBEKANNT"),
|
||||
],
|
||||
default="UNKNOWN",
|
||||
max_length=10,
|
||||
),
|
||||
),
|
||||
(
|
||||
"wifi",
|
||||
models.CharField(
|
||||
choices=[
|
||||
("YES", "JA"),
|
||||
("NO", "NEIN"),
|
||||
("UNKNOWN", "UNBEKANNT"),
|
||||
],
|
||||
default="UNKNOWN",
|
||||
max_length=10,
|
||||
),
|
||||
),
|
||||
(
|
||||
"lan",
|
||||
models.CharField(
|
||||
choices=[
|
||||
("YES", "JA"),
|
||||
("NO", "NEIN"),
|
||||
("UNKNOWN", "UNBEKANNT"),
|
||||
],
|
||||
default="UNKNOWN",
|
||||
max_length=10,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name="MonitorSpecification",
|
||||
fields=[
|
||||
(
|
||||
"device",
|
||||
models.OneToOneField(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
primary_key=True,
|
||||
related_name="monitor_specification",
|
||||
serialize=False,
|
||||
to="inventory.device",
|
||||
),
|
||||
),
|
||||
(
|
||||
"display_inches",
|
||||
models.DecimalField(
|
||||
blank=True, decimal_places=1, max_digits=4, null=True
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name="PrinterSpecification",
|
||||
fields=[
|
||||
(
|
||||
"device",
|
||||
models.OneToOneField(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
primary_key=True,
|
||||
related_name="printer_specification",
|
||||
serialize=False,
|
||||
to="inventory.device",
|
||||
),
|
||||
),
|
||||
(
|
||||
"wifi",
|
||||
models.CharField(
|
||||
choices=[
|
||||
("YES", "JA"),
|
||||
("NO", "NEIN"),
|
||||
("UNKNOWN", "UNBEKANNT"),
|
||||
],
|
||||
default="UNKNOWN",
|
||||
max_length=10,
|
||||
),
|
||||
),
|
||||
(
|
||||
"lan",
|
||||
models.CharField(
|
||||
choices=[
|
||||
("YES", "JA"),
|
||||
("NO", "NEIN"),
|
||||
("UNKNOWN", "UNBEKANNT"),
|
||||
],
|
||||
default="UNKNOWN",
|
||||
max_length=10,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
migrations.RunPython(copy_to_specifications, copy_back_to_devices),
|
||||
migrations.RemoveField(
|
||||
model_name="device",
|
||||
name="battery",
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name="device",
|
||||
name="cpu",
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name="device",
|
||||
name="display_inches",
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name="device",
|
||||
name="hdd_gb",
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name="device",
|
||||
name="hwid",
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name="device",
|
||||
name="lan",
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name="device",
|
||||
name="ram_gb",
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name="device",
|
||||
name="ssd_gb",
|
||||
),
|
||||
migrations.RemoveField(
|
||||
model_name="device",
|
||||
name="wifi",
|
||||
),
|
||||
migrations.RunPython(computer_search_index, drop_computer_search_index),
|
||||
]
|
||||
Reference in New Issue
Block a user