160 lines
6.2 KiB
Python
160 lines
6.2 KiB
Python
from decimal import Decimal
|
|
|
|
from django.core.management.base import BaseCommand
|
|
from django.db import transaction
|
|
|
|
from filament.anycubic_catalog import ANYCUBIC_CATALOG
|
|
from filament.bambu_catalog import BAMBU_LAB_CATALOG, BAMBU_LAB_VARIANT_COUNT
|
|
from filament.bambu_images_0006 import bambu_image_url
|
|
from filament.bambu_petg_catalog import BAMBU_PETG_CATALOG
|
|
from filament.bambu_pla_pure_catalog import BAMBU_PLA_PURE_CATALOG
|
|
from filament.bambu_swatches_0005 import BAMBU_SWATCHES
|
|
from filament.esun_catalog import ESUN_CATALOG
|
|
from filament.geeetech_catalog import GEEETECH_CATALOG
|
|
from filament.generic_catalog import GENERIC_CATALOG
|
|
from filament.jayo_catalog import JAYO_CATALOG
|
|
from filament.models import CatalogBootstrap, Color, Manufacturer, ProductType, Variant
|
|
from filament.sunlu_catalog import SUNLU_CATALOG, SUNLU_IMAGE_BASE
|
|
|
|
BAMBU_LAB_BOOTSTRAP_KEY = "bambu-lab-eu-initial-catalog"
|
|
BAMBU_PETG_BOOTSTRAP_KEY = "bambu-lab-eu-petg-basic-matte"
|
|
BAMBU_PLA_PURE_BOOTSTRAP_KEY = "bambu-lab-eu-pla-pure"
|
|
SUNLU_BOOTSTRAP_KEY = "sunlu-de-initial-catalog"
|
|
ESUN_BOOTSTRAP_KEY = "esun-eu-initial-catalog"
|
|
JAYO_BOOTSTRAP_KEY = "jayo-eu-initial-catalog"
|
|
ANYCUBIC_BOOTSTRAP_KEY = "anycubic-de-initial-catalog"
|
|
GEEETECH_BOOTSTRAP_KEY = "geeetech-eu-initial-catalog"
|
|
GENERIC_BOOTSTRAP_KEY = "generic-initial-catalog"
|
|
|
|
|
|
class Command(BaseCommand):
|
|
help = "Importiert die mitgelieferten Herstellerkataloge einmalig pro Datenbank."
|
|
|
|
def handle(self, *args, **options):
|
|
self.seed_bambu(options["verbosity"])
|
|
self.seed_catalog(
|
|
options["verbosity"], BAMBU_PETG_BOOTSTRAP_KEY, "Bambu Lab", BAMBU_PETG_CATALOG
|
|
)
|
|
self.seed_catalog(
|
|
options["verbosity"], BAMBU_PLA_PURE_BOOTSTRAP_KEY, "Bambu Lab", BAMBU_PLA_PURE_CATALOG
|
|
)
|
|
self.seed_catalog(
|
|
options["verbosity"], SUNLU_BOOTSTRAP_KEY, "SUNLU", SUNLU_CATALOG, sunlu_image_url
|
|
)
|
|
self.seed_catalog(options["verbosity"], ESUN_BOOTSTRAP_KEY, "eSUN", ESUN_CATALOG)
|
|
self.seed_catalog(
|
|
options["verbosity"], JAYO_BOOTSTRAP_KEY, "JAYO", JAYO_CATALOG, shopify_image_url
|
|
)
|
|
self.seed_catalog(
|
|
options["verbosity"],
|
|
ANYCUBIC_BOOTSTRAP_KEY,
|
|
"Anycubic",
|
|
ANYCUBIC_CATALOG,
|
|
shopify_image_url,
|
|
)
|
|
self.seed_catalog(
|
|
options["verbosity"],
|
|
GEEETECH_BOOTSTRAP_KEY,
|
|
"Geeetech",
|
|
GEEETECH_CATALOG,
|
|
shopify_image_url,
|
|
)
|
|
self.seed_catalog(
|
|
options["verbosity"],
|
|
GENERIC_BOOTSTRAP_KEY,
|
|
"Sonstige",
|
|
GENERIC_CATALOG,
|
|
diameter_mm=None,
|
|
)
|
|
|
|
@transaction.atomic
|
|
def seed_bambu(self, verbosity):
|
|
_, should_import = CatalogBootstrap.objects.get_or_create(key=BAMBU_LAB_BOOTSTRAP_KEY)
|
|
if not should_import:
|
|
if verbosity:
|
|
self.stdout.write(
|
|
"Bambu-Lab-Erstkatalog wurde bereits importiert; Benutzerdaten bleiben unverändert."
|
|
)
|
|
return
|
|
|
|
manufacturer, _ = Manufacturer.objects.get_or_create(name="Bambu Lab")
|
|
created = 0
|
|
for product_name, catalog_variants in BAMBU_LAB_CATALOG:
|
|
product, _ = ProductType.objects.get_or_create(
|
|
manufacturer=manufacturer, name=product_name
|
|
)
|
|
for color_name, code, net_weight_g, spool_types in catalog_variants:
|
|
color, _ = Color.objects.get_or_create(name=color_name)
|
|
for spool_type in spool_types:
|
|
_, new = Variant.objects.get_or_create(
|
|
product_type=product,
|
|
color=color,
|
|
spool_type=spool_type,
|
|
defaults={
|
|
"manufacturer_color_code": code,
|
|
"swatch": BAMBU_SWATCHES.get(product_name, {}).get(
|
|
code or color_name, ""
|
|
),
|
|
"image_url": bambu_image_url(
|
|
product_name, code or color_name, spool_type
|
|
),
|
|
"net_weight_g": net_weight_g,
|
|
"diameter_mm": Decimal("1.75"),
|
|
},
|
|
)
|
|
created += new
|
|
if verbosity:
|
|
self.stdout.write(
|
|
self.style.SUCCESS(
|
|
f"Bambu-Lab-Erstkatalog importiert: {created} Varianten ergänzt; "
|
|
f"{BAMBU_LAB_VARIANT_COUNT} Varianten im Datenstand."
|
|
)
|
|
)
|
|
|
|
@transaction.atomic
|
|
def seed_catalog(
|
|
self,
|
|
verbosity,
|
|
key,
|
|
manufacturer_name,
|
|
catalog,
|
|
image_url=None,
|
|
*,
|
|
diameter_mm=Decimal("1.75"),
|
|
):
|
|
_, should_import = CatalogBootstrap.objects.get_or_create(key=key)
|
|
if not should_import:
|
|
return
|
|
|
|
manufacturer, _ = Manufacturer.objects.get_or_create(name=manufacturer_name)
|
|
created = 0
|
|
for name, catalog_variants in catalog.items():
|
|
product, _ = ProductType.objects.get_or_create(manufacturer=manufacturer, name=name)
|
|
for color_name, spool_type, sku, weight, swatch, image_path in catalog_variants:
|
|
color, _ = Color.objects.get_or_create(name=color_name)
|
|
_, new = Variant.objects.get_or_create(
|
|
product_type=product,
|
|
color=color,
|
|
spool_type=spool_type,
|
|
defaults={
|
|
"manufacturer_color_code": sku,
|
|
"net_weight_g": weight,
|
|
"diameter_mm": diameter_mm,
|
|
"swatch": swatch,
|
|
"image_url": image_url(image_path) if image_url else image_path,
|
|
},
|
|
)
|
|
created += new
|
|
if verbosity:
|
|
self.stdout.write(
|
|
self.style.SUCCESS(f"{manufacturer_name}-Katalog: {created} Varianten ergänzt.")
|
|
)
|
|
|
|
|
|
def sunlu_image_url(path):
|
|
return SUNLU_IMAGE_BASE + path + "?width=144" if path else ""
|
|
|
|
|
|
def shopify_image_url(path):
|
|
return "https://cdn.shopify.com/" + path if path else ""
|