Files
Inventory/inventory/management/commands/seed_initial_data.py
2026-09-29 08:43:05 +02:00

56 lines
2.8 KiB
Python

from decimal import Decimal
from django.core.management.base import BaseCommand
from django.db import transaction
from inventory.models import ChoiceValue, Employee, NumberSequence
CHOICES = {
ChoiceValue.Kind.STATUS: ["Eingang", "Entsorgt", "Fertig", "Defekt", "Reserviert", "Ausgegeben", "Intern"],
ChoiceValue.Kind.RECIPIENT: ["Landkreis", "Jugendamt", "Bürgergeld", "Rentner", "Geflüchtete", "NGO", "Stadt", "Sonstige", "Verein", "Schüler/Studenten"],
ChoiceValue.Kind.LOCATION: ["R", "L"],
ChoiceValue.Kind.DEVICE_TYPE: ["Laptop", "AiO", "Desktop", "Bildschirm", "Drucker"],
}
NUMERIC_CHOICES = {
ChoiceValue.Kind.RAM: [2, 4, 6, 8, 12, 16, 24, 32, 64, 128],
ChoiceValue.Kind.SSD: [0, 64, 120, 128, 240, 250, 256, 480, 500, 512, 960, 1000, 2000, 4000],
ChoiceValue.Kind.HDD: [0, 64, 120, 128, 240, 250, 256, 480, 500, 512, 960, 1000, 2000, 4000],
ChoiceValue.Kind.DISPLAY: [10.1, 11.6, 12.5, 13.3, 14, 15, 15.6, 16, 17, 17.3],
}
EMPLOYEES = {
"AC": "Ahmad Chaja", "A_H": "Ahmad Hakim", "HA": "Aaron Holmer", "JH": "Johannes Hundshammer",
"TS": "Thomas Stemberger", "YN": "Yurii Naumenko", "VL": "Viktor Lysenko", "SB": "Stefan Bauer",
"KA": "Kanani", "RS": "Ruth Stepper", "SK": "Name aktuell unbekannt", "RG": "Rainer Gerhardt",
"HN": "Heinrich", "KK": "Karsten Kopplin", "LL": "Lucas Lenninger", "AL": "Arseni Litvin", "GW": "Gregor Witczak",
}
STATUS_COLORS = {
"Eingang": "#2563eb", "Entsorgt": "#64748b", "Fertig": "#15803d",
"Defekt": "#dc2626", "Reserviert": "#b45309", "Ausgegeben": "#7c3aed", "Intern": "#0891b2",
}
class Command(BaseCommand):
help = "Legt die initialen Auswahlwerte und Mitarbeiter idempotent an."
@transaction.atomic
def handle(self, *args, **options):
count = 0
for kind, labels in CHOICES.items():
for order, label in enumerate(labels):
defaults = {"active": True, "sort_order": order}
if kind == ChoiceValue.Kind.STATUS:
defaults["color"] = STATUS_COLORS.get(label, "#2563eb")
_, created = ChoiceValue.objects.get_or_create(kind=kind, label=label, defaults=defaults)
count += int(created)
for kind, values in NUMERIC_CHOICES.items():
for order, value in enumerate(values):
label = f"{value:g}"
_, created = ChoiceValue.objects.get_or_create(kind=kind, label=label, defaults={"numeric_value": Decimal(str(value)), "active": True, "sort_order": order})
count += int(created)
for code, name in EMPLOYEES.items():
_, created = Employee.objects.get_or_create(code=code, defaults={"name": name, "active": True})
count += int(created)
NumberSequence.objects.get_or_create(name="device")
self.stdout.write(self.style.SUCCESS(f"Seed-Daten bereit ({count} neue Datensätze)."))