54 lines
2.7 KiB
Python
54 lines
2.7 KiB
Python
import random
|
|
from datetime import timedelta
|
|
|
|
from django.core.management.base import BaseCommand, CommandError
|
|
from django.db import transaction
|
|
from django.db.models import Max
|
|
from django.utils import timezone
|
|
|
|
from inventory.models import ChoiceValue, Device, Employee, NumberSequence
|
|
from inventory.device_types import bulk_create_specifications, clear_inapplicable
|
|
|
|
|
|
MODELS = ["Lenovo ThinkPad T480", "Dell Latitude 7490", "HP EliteBook 840 G5", "Lenovo ThinkCentre M720", "Dell OptiPlex 7060", "HP ProDesk 600 G4", "Lenovo ThinkPad P50"]
|
|
CPUS = ["Intel Core i5-8250U", "Intel Core i5-8350U", "Intel Core i7-8650U", "Intel Core i5-6500", "Intel Core i7-6700", "AMD Ryzen 5 3500U"]
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = "Erzeugt realistische Testgeräte, z. B. generate_test_devices 5000."
|
|
|
|
def add_arguments(self, parser):
|
|
parser.add_argument("count", type=int)
|
|
|
|
@transaction.atomic
|
|
def handle(self, *args, **options):
|
|
count = options["count"]
|
|
if count < 1 or count > 100000:
|
|
raise CommandError("Anzahl muss zwischen 1 und 100000 liegen.")
|
|
statuses = list(ChoiceValue.objects.filter(kind=ChoiceValue.Kind.STATUS, active=True))
|
|
types = list(ChoiceValue.objects.filter(kind=ChoiceValue.Kind.DEVICE_TYPE, active=True))
|
|
employees = list(Employee.objects.filter(active=True))
|
|
if not statuses:
|
|
raise CommandError("Bitte zuerst seed_initial_data ausführen.")
|
|
start = (Device.objects.aggregate(value=Max("number"))["value"] or 0) + 1
|
|
today = timezone.localdate()
|
|
devices = []
|
|
for offset in range(count):
|
|
model = random.choice(MODELS)
|
|
device = Device(
|
|
number=start + offset, shelf=f"{random.choice('ABCDEFGH')}{random.randint(1, 30)}",
|
|
status=random.choice(statuses), device_type=random.choice(types) if types else None,
|
|
date=today - timedelta(days=random.randint(0, 1500)), model=model, cpu=random.choice(CPUS),
|
|
ram_gb=random.choice([4, 8, 8, 16, 32]), ssd_gb=random.choice([0, 128, 256, 512, 1000]),
|
|
hdd_gb=random.choice([0, 0, 500, 1000, 2000]), employee=random.choice(employees) if employees else None,
|
|
wifi=random.choice(Device.TriState.values), lan=random.choice(Device.TriState.values),
|
|
)
|
|
clear_inapplicable(device)
|
|
device.custom_values = {}
|
|
devices.append(device)
|
|
for index in range(0, count, 1000):
|
|
saved = Device.objects.bulk_create(devices[index:index + 1000], batch_size=1000)
|
|
bulk_create_specifications(saved)
|
|
NumberSequence.ensure_at_least(start + count - 1)
|
|
self.stdout.write(self.style.SUCCESS(f"{count} Testgeräte angelegt."))
|