256 lines
8.7 KiB
Python
256 lines
8.7 KiB
Python
from django.conf import settings
|
||
from django.core.validators import MaxValueValidator, MinValueValidator, RegexValidator
|
||
from django.db import models
|
||
|
||
|
||
class Manufacturer(models.Model):
|
||
name = models.CharField("Hersteller", max_length=100, unique=True)
|
||
|
||
class Meta:
|
||
ordering = ["name"]
|
||
verbose_name = "Hersteller"
|
||
verbose_name_plural = "Hersteller"
|
||
|
||
def __str__(self):
|
||
return self.name
|
||
|
||
|
||
class ProductType(models.Model):
|
||
manufacturer = models.ForeignKey(
|
||
Manufacturer, on_delete=models.PROTECT, verbose_name="Hersteller"
|
||
)
|
||
name = models.CharField("Produktart", max_length=100)
|
||
|
||
class Meta:
|
||
ordering = ["manufacturer__name", "name"]
|
||
constraints = [
|
||
models.UniqueConstraint(
|
||
fields=["manufacturer", "name"], name="unique_product_per_manufacturer"
|
||
)
|
||
]
|
||
verbose_name = "Produktart"
|
||
verbose_name_plural = "Produktarten"
|
||
|
||
def __str__(self):
|
||
return f"{self.manufacturer} · {self.name}"
|
||
|
||
@property
|
||
def material_and_subtype(self):
|
||
name = self.name.strip()
|
||
brand = self.manufacturer.name.strip() + " "
|
||
if name.casefold().startswith(brand.casefold()):
|
||
name = name[len(brand) :].lstrip(" ·-")
|
||
material, _, subtype = name.partition(" ")
|
||
if "-" in material:
|
||
material, suffix = material.split("-", 1)
|
||
subtype = " ".join(part for part in (suffix, subtype) if part)
|
||
return material or name, subtype.strip() or material or name
|
||
|
||
|
||
class Color(models.Model):
|
||
name = models.CharField("Farbe", max_length=100, unique=True)
|
||
|
||
class Meta:
|
||
ordering = ["name"]
|
||
verbose_name = "Farbe"
|
||
verbose_name_plural = "Farben"
|
||
|
||
def __str__(self):
|
||
return self.name
|
||
|
||
|
||
class Variant(models.Model):
|
||
class SpoolType(models.TextChoices):
|
||
SPOOL = "spool", "Mit Spule"
|
||
REFILL = "refill", "Refill (ohne Spule)"
|
||
|
||
product_type = models.ForeignKey(
|
||
ProductType, on_delete=models.PROTECT, related_name="variants", verbose_name="Produktart"
|
||
)
|
||
color = models.ForeignKey(
|
||
Color, on_delete=models.PROTECT, related_name="variants", verbose_name="Farbe"
|
||
)
|
||
spool_type = models.CharField("Spulenausführung", max_length=10, choices=SpoolType.choices)
|
||
manufacturer_color_code = models.CharField("Hersteller-Farbcode", max_length=40, blank=True)
|
||
swatch = models.CharField(
|
||
"Farbvorschau",
|
||
max_length=15,
|
||
blank=True,
|
||
validators=[
|
||
RegexValidator(
|
||
r"\A#[0-9A-Fa-f]{6}(,#[0-9A-Fa-f]{6})?\Z",
|
||
"Bitte einen oder zwei Hexwerte eingeben (z. B. #DE4343 oder #000000,#A34342).",
|
||
)
|
||
],
|
||
help_text="Ein Hexwert oder zwei durch ein Komma getrennte Hexwerte.",
|
||
)
|
||
image_url = models.URLField(
|
||
"Produktbild",
|
||
max_length=500,
|
||
blank=True,
|
||
validators=[
|
||
RegexValidator(
|
||
r"\Ahttps://(?:cdn\.shopify\.com|store\.bblcdn\.eu|store\.bblcdn\.com)/",
|
||
"Bitte eine HTTPS-Bildadresse von einer unterstützten CDN-Domain eingeben.",
|
||
)
|
||
],
|
||
help_text="Bildadresse von cdn.shopify.com, store.bblcdn.eu oder store.bblcdn.com.",
|
||
)
|
||
net_weight_g = models.PositiveIntegerField(
|
||
"Nettogewicht (g)", null=True, blank=True, validators=[MinValueValidator(1)]
|
||
)
|
||
diameter_mm = models.DecimalField(
|
||
"Durchmesser (mm)",
|
||
max_digits=4,
|
||
decimal_places=2,
|
||
null=True,
|
||
blank=True,
|
||
validators=[MinValueValidator(0.01)],
|
||
)
|
||
is_active = models.BooleanField("Aktiv", default=True)
|
||
|
||
class Meta:
|
||
ordering = [
|
||
"product_type__manufacturer__name",
|
||
"product_type__name",
|
||
"color__name",
|
||
"spool_type",
|
||
]
|
||
constraints = [
|
||
models.UniqueConstraint(
|
||
fields=["product_type", "color", "spool_type"], name="unique_catalog_variant"
|
||
),
|
||
models.CheckConstraint(
|
||
condition=models.Q(spool_type__in=["spool", "refill"]), name="valid_spool_type"
|
||
),
|
||
models.CheckConstraint(
|
||
condition=models.Q(net_weight_g__isnull=True) | models.Q(net_weight_g__gt=0),
|
||
name="positive_net_weight",
|
||
),
|
||
models.CheckConstraint(
|
||
condition=models.Q(diameter_mm__isnull=True) | models.Q(diameter_mm__gt=0),
|
||
name="positive_diameter",
|
||
),
|
||
]
|
||
verbose_name = "Filamentvariante"
|
||
verbose_name_plural = "Filamentvarianten"
|
||
|
||
@property
|
||
def manufacturer(self):
|
||
return self.product_type.manufacturer
|
||
|
||
def __str__(self):
|
||
return f"{self.product_type} · {self.color} · {self.get_spool_type_display()}"
|
||
|
||
|
||
class QuantityPosition(models.Model):
|
||
owner = models.ForeignKey(
|
||
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name="Person"
|
||
)
|
||
variant = models.ForeignKey(Variant, on_delete=models.PROTECT, verbose_name="Filamentvariante")
|
||
quantity = models.PositiveIntegerField(
|
||
"Stückzahl", validators=[MinValueValidator(1), MaxValueValidator(9999)]
|
||
)
|
||
|
||
class Meta:
|
||
abstract = True
|
||
ordering = [
|
||
"variant__product_type__manufacturer__name",
|
||
"variant__product_type__name",
|
||
"variant__color__name",
|
||
"variant__spool_type",
|
||
]
|
||
constraints = [
|
||
models.UniqueConstraint(fields=["owner", "variant"], name="%(class)s_owner_variant"),
|
||
models.CheckConstraint(
|
||
condition=models.Q(quantity__gte=1, quantity__lte=9999),
|
||
name="%(class)s_quantity_range",
|
||
),
|
||
]
|
||
|
||
def __str__(self):
|
||
return f"{self.quantity} × {self.variant}"
|
||
|
||
|
||
class InventoryItem(QuantityPosition):
|
||
"""Aggregate per owner and exact catalog variant."""
|
||
|
||
|
||
class OrderItem(QuantityPosition):
|
||
class Status(models.TextChoices):
|
||
OPEN = "open", "Offen"
|
||
UNAVAILABLE = "unavailable", "Aktuell nicht verfügbar"
|
||
ORDERED = "ordered", "Bestellt"
|
||
ARRIVED = "arrived", "Eingetroffen"
|
||
HANDED_OVER = "handed_over", "Übergeben"
|
||
|
||
PENDING_STATUSES = (Status.OPEN, Status.UNAVAILABLE)
|
||
status = models.CharField("Bestellstatus", max_length=16, choices=Status, default=Status.OPEN)
|
||
ordered_at = models.DateTimeField("Bestellt am", null=True, blank=True)
|
||
handed_over_at = models.DateTimeField("Übergeben am", null=True, blank=True)
|
||
|
||
class Meta(QuantityPosition.Meta):
|
||
constraints = [
|
||
models.UniqueConstraint(
|
||
fields=["owner", "variant"],
|
||
condition=models.Q(status__in=["open", "unavailable"]),
|
||
name="orderitem_pending_owner_variant",
|
||
),
|
||
models.CheckConstraint(
|
||
condition=models.Q(quantity__gte=1, quantity__lte=9999),
|
||
name="orderitem_quantity_range",
|
||
),
|
||
models.CheckConstraint(
|
||
condition=models.Q(
|
||
status__in=["open", "unavailable", "ordered", "arrived", "handed_over"]
|
||
),
|
||
name="orderitem_valid_status",
|
||
),
|
||
]
|
||
|
||
@property
|
||
def is_pending(self):
|
||
return self.status in self.PENDING_STATUSES
|
||
|
||
|
||
class Printer(models.Model):
|
||
owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
|
||
manufacturer = models.CharField("Hersteller", max_length=100)
|
||
model = models.CharField("Modell", max_length=100)
|
||
name = models.CharField("Name (optional)", max_length=100, blank=True)
|
||
|
||
class Meta:
|
||
ordering = ["manufacturer", "model", "name"]
|
||
|
||
def __str__(self):
|
||
base = f"{self.manufacturer} {self.model}"
|
||
return f"{base} · {self.name}" if self.name else base
|
||
|
||
|
||
class SharingPreferences(models.Model):
|
||
owner = models.OneToOneField(
|
||
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name="sharing"
|
||
)
|
||
share_inventory = models.BooleanField("Bestand mit anderen teilen", default=False)
|
||
share_printers = models.BooleanField("Drucker mit anderen teilen", default=False)
|
||
|
||
|
||
class CatalogBootstrap(models.Model):
|
||
"""Internal marker: bundled starter catalogs are imported only once."""
|
||
|
||
key = models.CharField(max_length=100, unique=True)
|
||
applied_at = models.DateTimeField(auto_now_add=True)
|
||
|
||
|
||
class Feedback(models.Model):
|
||
sender = models.ForeignKey(
|
||
settings.AUTH_USER_MODEL, on_delete=models.CASCADE, verbose_name="Absender"
|
||
)
|
||
message = models.TextField("Nachricht", max_length=2000)
|
||
created_at = models.DateTimeField("Gesendet am", auto_now_add=True)
|
||
|
||
class Meta:
|
||
ordering = ["-created_at", "-pk"]
|
||
verbose_name = "Feedback"
|
||
verbose_name_plural = "Feedback"
|