from django.core.management import call_command, get_commands from filament.bambu_catalog import BAMBU_LAB_PRODUCT_COUNT, BAMBU_LAB_VARIANT_COUNT from filament.forms import VariantSelectionForm from filament.management.commands.seed_catalog import ( ANYCUBIC_BOOTSTRAP_KEY, BAMBU_LAB_BOOTSTRAP_KEY, BAMBU_PETG_BOOTSTRAP_KEY, ESUN_BOOTSTRAP_KEY, JAYO_BOOTSTRAP_KEY, SUNLU_BOOTSTRAP_KEY, ) from filament.models import ( CatalogBootstrap, Color, InventoryItem, Manufacturer, OrderItem, ProductType, Variant, ) from .base import CatalogTestCase class CatalogTests(CatalogTestCase): def setUp(self): self.client.force_login(self.alice) def test_lists_show_product_images_and_placeholder_for_missing_photo(self): image_url = "https://cdn.shopify.com/example.png" self.spool.image_url = image_url self.spool.manufacturer_color_code = "11200" self.spool.save(update_fields=["image_url", "manufacturer_color_code"]) for model in (InventoryItem, OrderItem): model.objects.create(owner=self.alice, variant=self.spool, quantity=1) model.objects.create(owner=self.alice, variant=self.refill, quantity=1) for url in ("/bestand/", "/bestellung/"): response = self.client.get(url) self.assertContains( response, 'class="variant-thumbnail"', count=1 if url == "/bestand/" else 2 ) self.assertContains(response, f'src="{image_url}"', count=1) self.assertContains(response, 'referrerpolicy="no-referrer"', count=1) self.assertContains(response, 'Artikelnr.') self.assertContains(response, '11200', count=1) self.assertContains(response, '–', count=1) self.assertIn("https://cdn.shopify.com", response["Content-Security-Policy"]) self.assertNotIn( "https://cdn.shopify.com", self.client.get("/katalog/")["Content-Security-Policy"] ) def test_shared_catalog_displays_optional_variant_data(self): self.spool.manufacturer_color_code = "11200" self.spool.net_weight_g = 1000 self.spool.diameter_mm = "1.75" self.spool.save() response = self.client.get("/katalog/") self.assertEqual(response.status_code, 200) for text in ( "Bambu Lab", "PLA Matte", "Scharlachrot", "11200", "1000", "1,75", "Mit Spule", "Refill (ohne Spule)", ): self.assertContains(response, text) def test_dropdowns_only_offer_existing_active_combinations(self): other_maker = Manufacturer.objects.create(name="Anderer Hersteller") other_product = ProductType.objects.create(manufacturer=other_maker, name="PLA Basic") other_color = Color.objects.create(name="Blau") Variant.objects.create(product_type=other_product, color=other_color, spool_type="refill") form = VariantSelectionForm(selection=self.selection()) self.assertEqual(list(form.fields["product_type"].queryset), [self.product]) self.assertEqual(set(form.fields["color"].queryset), {self.red, self.black}) self.assertEqual( form.fields["spool_type"].choices, [("spool", "Filament auf Spule"), ("refill", "Refill (ohne Spule)")], ) invalid = self.selection() | {"product_type": other_product.pk, "color": other_color.pk} self.assertFalse(VariantSelectionForm(invalid).is_valid()) response = self.client.post("/bestand/hinzufuegen/", invalid) self.assertEqual(response.status_code, 200) self.assertFalse(InventoryItem.objects.exists()) def test_material_filter_only_shows_matching_subtypes_without_brand(self): basic = ProductType.objects.create(manufacturer=self.maker, name="Bambu Lab · PLA Basic") abs_cf = ProductType.objects.create(manufacturer=self.maker, name="ABS-CF") Variant.objects.create(product_type=basic, color=self.black, spool_type="refill") Variant.objects.create(product_type=abs_cf, color=self.black, spool_type="spool") for url in ("/bestand/hinzufuegen/", "/bestellung/hinzufuegen/"): response = self.client.post( url, {"manufacturer": self.maker.pk, "material": "PLA", "action": "filter"} ) form = response.context["form"] self.assertEqual( list(form.fields["material"].choices)[1:], [("ABS", "ABS"), ("PLA", "PLA")] ) self.assertEqual( {label for value, label in form.fields["product_type"].choices if value}, {"Basic", "Matte"}, ) self.assertFalse(form.fields["color"].queryset.exists()) response = self.client.post(url, self.selection() | {"material": "ABS"}) self.assertTrue(response.context["form"].errors) def test_filter_button_works_without_javascript_or_full_selection(self): response = self.client.post( "/bestand/hinzufuegen/", {"manufacturer": self.maker.pk, "action": "filter"} ) self.assertEqual(response.status_code, 200) self.assertFalse(response.context["form"].errors) self.assertContains(response, "formnovalidate") self.assertFalse(InventoryItem.objects.exists()) def test_spool_choices_have_no_placeholder_and_keep_the_existing_selection(self): for url in ("/bestand/hinzufuegen/", "/bestellung/hinzufuegen/"): for variant in (self.spool, self.refill, self.black_spool): with self.subTest(url=url, variant=variant): response = self.client.post(url, self.selection(variant) | {"action": "filter"}) self.assertNotContains(response, "Spulenausführung wählen") self.assertContains(response, "Filament auf Spule") field = response.context["form"]["spool_type"] self.assertEqual(field.value(), variant.spool_type) self.assertEqual( "refill" in dict(field.field.choices), variant.color_id == self.red.pk ) data = self.selection(self.black_spool) | {"spool_type": "", "action": "filter"} self.assertEqual( self.client.post(url, data).context["form"]["spool_type"].value(), "spool" ) def test_article_number_is_optional_when_using_dropdowns(self): for path, model in (("bestand", InventoryItem), ("bestellung", OrderItem)): with self.subTest(path=path): url = f"/{path}/hinzufuegen/" response = self.client.get(url) article_input = response.context["lookup"]["article_number"].as_widget() self.assertIn('name="article_number"', article_input) self.assertNotIn("required", article_input) self.assertRedirects( self.client.post(url, self.selection() | {"article_number": ""}), f"/{path}/" ) self.assertEqual(model.objects.get(variant=self.spool).quantity, 1) response = self.client.post(url, {"action": "lookup", "article_number": ""}) self.assertContains(response, "Bitte gib eine Artikelnummer ein.") def test_article_lookup_prefills_both_forms_and_preserves_quantity(self): self.refill.manufacturer_color_code = "PLA-RD-01" self.refill.save(update_fields=["manufacturer_color_code"]) for path, model in (("bestand", InventoryItem), ("bestellung", OrderItem)): with self.subTest(path=path): url = f"/{path}/hinzufuegen/" response = self.client.post( url, {"action": "lookup", "article_number": " pla-rd-01 ", "quantity": 4} ) self.assertContains(response, "Variante suchen") self.assertEqual(response.context["form"].initial, self.selection(self.refill, "4")) self.assertFalse(model.objects.exists()) self.assertRedirects( self.client.post(url, response.context["form"].initial), f"/{path}/" ) self.assertEqual(model.objects.get(variant=self.refill).quantity, 4) def test_article_lookup_requires_a_choice_for_duplicate_active_codes(self): Variant.objects.filter(pk__in=[self.spool.pk, self.refill.pk]).update( manufacturer_color_code="10100" ) self.black_spool.manufacturer_color_code = "10100" self.black_spool.is_active = False self.black_spool.save() url = "/bestand/hinzufuegen/" lookup = {"article_number": "10100", "quantity": 2} response = self.client.post(url, lookup | {"action": "lookup"}) self.assertContains(response, "Mehrere Varianten gefunden") self.assertEqual(set(response.context["matches"]), {self.spool, self.refill}) self.assertNotIn("spool_type", response.context["form"].initial) invalid = self.client.post( url, lookup | {"action": "pick", "article_variant": self.black_spool.pk} ) self.assertContains(invalid, "Bitte wähle eine der gefundenen Varianten.") picked = self.client.post( url, lookup | {"action": "pick", "article_variant": self.spool.pk} ) self.assertEqual(picked.context["form"].initial, self.selection(self.spool, "2")) self.assertFalse(InventoryItem.objects.exists()) def test_article_lookup_ignores_partial_and_unknown_codes(self): self.spool.manufacturer_color_code = "40101" self.spool.save(update_fields=["manufacturer_color_code"]) url = "/bestellung/hinzufuegen/" response = self.client.post( url, self.selection() | {"action": "lookup", "article_number": "4010"} ) self.assertContains(response, "Zu dieser Artikelnummer gibt es keinen aktiven Eintrag.") self.assertEqual(response.context["form"]["color"].value(), str(self.red.pk)) self.assertFalse(OrderItem.objects.exists()) def test_color_preview_is_available_in_both_flows_and_remains_optional(self): self.spool.swatch = "#DE4343" self.spool.save(update_fields=["swatch"]) for url in ("/bestand/hinzufuegen/", "/bestellung/hinzufuegen/"): with self.subTest(url=url): response = self.client.post( url, { "manufacturer": self.maker.pk, "material": "PLA", "product_type": self.product.pk, "action": "filter", }, ) self.assertContains(response, 'data-color-select=""') self.assertContains(response, 'data-swatch="#DE4343"', count=1) self.assertContains(response, "Kohleschwarz") self.assertFalse(response.context["form"].errors) def test_inactive_variants_stay_visible_in_existing_positions_but_cannot_be_added(self): stock = InventoryItem.objects.create(owner=self.alice, variant=self.spool, quantity=2) OrderItem.objects.create(owner=self.bob, variant=self.spool, quantity=1) self.spool.is_active = False self.spool.save() for url in ("/bestand/", "/bestellung/"): self.assertContains(self.client.get(url), "Deaktiviert") for url in ("/bestand/hinzufuegen/", "/bestellung/hinzufuegen/"): self.assertEqual(self.client.post(url, self.selection()).status_code, 200) stock.refresh_from_db() self.assertEqual(stock.quantity, 2) self.assertEqual(OrderItem.objects.count(), 1) self.assertEqual( self.client.post(f"/bestand/{stock.pk}/bearbeiten/", {"quantity": 1}).status_code, 302 ) self.assertNotIn( "spool", dict(VariantSelectionForm(selection=self.selection()).fields["spool_type"].choices), ) def test_bambu_catalog_is_complete_one_time_bootstrap(self): self.assertIn("seed_catalog", get_commands()) call_command("seed_catalog", verbosity=0) self.assertEqual( ProductType.objects.filter(manufacturer__name="Bambu Lab").count(), BAMBU_LAB_PRODUCT_COUNT + 2, ) bambu_variants = Variant.objects.filter(product_type__manufacturer__name="Bambu Lab") self.assertEqual(bambu_variants.count(), BAMBU_LAB_VARIANT_COUNT + 42) self.assertFalse( bambu_variants.exclude(pk__in=(self.spool.pk, self.refill.pk, self.black_spool.pk)) .filter(image_url="") .exists() ) self.assertTrue( Variant.objects.get( product_type__name="PLA Basic", manufacturer_color_code="10100", spool_type="spool" ).image_url.endswith("/pla_baisc_white.jpg") ) for product, color, code, spool_type, weight in ( ("PLA Basic", "Jade White", "10100", "refill", 1000), ("PLA Translucent", "Teal", "13612", "spool", 1000), ("PAHT-CF (500 g)", "Black", "70100", "spool", 500), ("PAHT-CF (1 kg)", "Black", "70100", "spool", 1000), ("Support for PLA/PETG", "Nature", "65102", "spool", 500), ): self.assertTrue( Variant.objects.filter( product_type__name=product, color__name=color, manufacturer_color_code=code, spool_type=spool_type, net_weight_g=weight, diameter_mm="1.75", ).exists(), (product, color, spool_type), ) self.assertFalse(ProductType.objects.filter(name__icontains="bundle").exists()) self.assertEqual( Variant.objects.get( product_type__name="PLA Silk Dual Color", color__name="Velvet Eclipse (Black-Red)" ).swatch, "#000000,#A34342", ) response = self.client.post( "/bestand/hinzufuegen/", {"action": "lookup", "article_number": "40101"} ) self.assertEqual( {(variant.color.name, variant.spool_type) for variant in response.context["matches"]}, {("ABS Black", "spool"), ("ABS Black", "refill")}, ) count = Variant.objects.count() inactive = Variant.objects.filter(manufacturer_color_code="11100").first() removed = Variant.objects.get( product_type__name="PLA Translucent", manufacturer_color_code="13612", spool_type="spool", ) inactive.is_active = False inactive.save() removed.delete() call_command("seed_catalog", verbosity=0) self.assertEqual(Variant.objects.count(), count - 1) inactive.refresh_from_db() self.assertFalse(inactive.is_active) self.assertFalse( bambu_variants.filter( product_type__name="PLA Translucent", manufacturer_color_code="13612" ).exists() ) self.assertTrue(CatalogBootstrap.objects.filter(key=BAMBU_LAB_BOOTSTRAP_KEY).exists()) def test_sunlu_imports_separately_into_existing_installations_and_preserves_edits(self): CatalogBootstrap.objects.create(key=BAMBU_LAB_BOOTSTRAP_KEY) call_command("seed_catalog", verbosity=0) sunlu = Manufacturer.objects.get(name="SUNLU") variants = Variant.objects.filter(product_type__manufacturer=sunlu) self.assertEqual(variants.count(), 561) self.assertEqual(ProductType.objects.filter(manufacturer=sunlu).count(), 67) self.assertEqual( Variant.objects.filter(product_type__manufacturer=self.maker) .exclude(product_type__name__in=("PETG Basic", "PETG Matte")) .count(), 3, ) self.assertTrue(CatalogBootstrap.objects.filter(key=SUNLU_BOOTSTRAP_KEY).exists()) pla = variants.filter(product_type__name="PLA", color__name="Schwarz") self.assertEqual(set(pla.values_list("spool_type", flat=True)), {"spool", "refill"}) spool = pla.get(spool_type="spool") self.assertEqual(spool.swatch, "#000000") self.assertTrue(spool.image_url.startswith("https://cdn.shopify.com/")) self.assertTrue( variants.filter(product_type__name="PETG (5 kg)", net_weight_g=5000).exists() ) self.assertTrue( variants.filter(product_type__name="PA6-CF20 (500 g)", net_weight_g=500).exists() ) for url in ("/bestand/hinzufuegen/", "/bestellung/hinzufuegen/"): self.assertRedirects( self.client.post(url, self.selection(spool)), url.removesuffix("hinzufuegen/"), ) refill = pla.get(spool_type="refill") refill.is_active = False refill.save(update_fields=["is_active"]) extra = variants.get(product_type__name="PETG-CF10") extra.delete() call_command("seed_catalog", verbosity=0) self.assertEqual(variants.count(), 560) self.assertFalse(variants.filter(product_type__name="PETG-CF10").exists()) refill.refresh_from_db() self.assertFalse(refill.is_active) def test_esun_imports_once_into_existing_installations(self): CatalogBootstrap.objects.create(key=BAMBU_LAB_BOOTSTRAP_KEY) CatalogBootstrap.objects.create(key=SUNLU_BOOTSTRAP_KEY) call_command("seed_catalog", verbosity=0) esun = Manufacturer.objects.get(name="eSUN") variants = Variant.objects.filter(product_type__manufacturer=esun) self.assertEqual(variants.count(), 438) self.assertEqual(ProductType.objects.filter(manufacturer=esun).count(), 50) self.assertTrue(CatalogBootstrap.objects.filter(key=ESUN_BOOTSTRAP_KEY).exists()) pla_black = variants.filter(product_type__name="PLA Plus", color__name="Black") self.assertEqual(set(pla_black.values_list("spool_type", flat=True)), {"spool", "refill"}) refill = pla_black.get(spool_type="refill") self.assertEqual(refill.swatch, "#272729") self.assertEqual(refill.net_weight_g, 1000) self.assertTrue(refill.image_url.startswith("https://ueeshop.ly200-cdn.com/")) refill.full_clean() for path in ("bestand", "bestellung"): self.assertRedirects( self.client.post(f"/{path}/hinzufuegen/", self.selection(refill)), f"/{path}/" ) response = self.client.get(f"/{path}/") self.assertContains(response, f'src="{refill.image_url}"') self.assertIn( "https://ueeshop.ly200-cdn.com/u_file/UPBB/UPBB609/", response["Content-Security-Policy"], ) refill.is_active = False refill.manufacturer_color_code = "CUSTOM-001" refill.save(update_fields=["is_active", "manufacturer_color_code"]) pla_black.get(spool_type="spool").delete() call_command("seed_catalog", verbosity=0) refill.refresh_from_db() self.assertFalse(refill.is_active) self.assertEqual(refill.manufacturer_color_code, "CUSTOM-001") self.assertEqual(variants.count(), 437) self.assertEqual(pla_black.count(), 1) def test_bambu_petg_expands_an_existing_catalog_once(self): CatalogBootstrap.objects.bulk_create( [ CatalogBootstrap(key=key) for key in (BAMBU_LAB_BOOTSTRAP_KEY, SUNLU_BOOTSTRAP_KEY, ESUN_BOOTSTRAP_KEY) ] ) product = ProductType.objects.create(manufacturer=self.maker, name="PETG Basic") white, _ = Color.objects.get_or_create(name="White") edited = Variant.objects.create( product_type=product, color=white, spool_type="spool", manufacturer_color_code="CUSTOM-WHITE", is_active=False, ) call_command("seed_catalog", verbosity=0) variants = Variant.objects.filter(product_type__manufacturer=self.maker).exclude( product_type=self.product ) self.assertEqual(variants.filter(product_type__name="PETG Basic").count(), 26) self.assertEqual(variants.filter(product_type__name="PETG Matte").count(), 16) self.assertTrue(CatalogBootstrap.objects.filter(key=BAMBU_PETG_BOOTSTRAP_KEY).exists()) self.assertFalse(variants.filter(product_type__name="PETG HF").exists()) edited.refresh_from_db() self.assertFalse(edited.is_active) self.assertEqual(edited.manufacturer_color_code, "CUSTOM-WHITE") refill = variants.get(manufacturer_color_code="30106", spool_type="refill") self.assertEqual(refill.swatch, "#FFFFFF") self.assertEqual(refill.net_weight_g, 1000) self.assertTrue(refill.image_url.startswith("https://store.bblcdn.eu/")) form = VariantSelectionForm(selection=self.selection(refill)) self.assertEqual( set(form.fields["product_type"].queryset.values_list("name", flat=True)), {"PETG Basic", "PETG Matte"}, ) refill.delete() call_command("seed_catalog", verbosity=0) self.assertFalse(variants.filter(manufacturer_color_code="30106").exists()) def test_jayo_and_anycubic_import_once_for_existing_installations(self): CatalogBootstrap.objects.bulk_create( CatalogBootstrap(key=key) for key in ( BAMBU_LAB_BOOTSTRAP_KEY, BAMBU_PETG_BOOTSTRAP_KEY, SUNLU_BOOTSTRAP_KEY, ESUN_BOOTSTRAP_KEY, ) ) call_command("seed_catalog", verbosity=0) for brand, key, count in ( ("JAYO", JAYO_BOOTSTRAP_KEY, 574), ("Anycubic", ANYCUBIC_BOOTSTRAP_KEY, 266), ): self.assertTrue(CatalogBootstrap.objects.filter(key=key).exists()) self.assertEqual( Variant.objects.filter(product_type__manufacturer__name=brand).count(), count ) jayo = Variant.objects.filter(product_type__manufacturer__name="JAYO", color__name="Black") normal = jayo.get(product_type__name="PLA", spool_type="spool") reusable = jayo.get(product_type__name="PLA (Mehrwegspule)", spool_type="spool") refill = jayo.get(product_type__name="PLA", spool_type="refill") self.assertEqual(reusable.net_weight_g, 1100) self.assertEqual(refill.manufacturer_color_code, "DLZ-EU-JY-PLA-BK-1.1kg-N") self.assertEqual(jayo.get(product_type__name="PLA (5 kg)").net_weight_g, 5000) self.assertTrue(refill.image_url.startswith("https://cdn.shopify.com/")) anycubic = Variant.objects.filter( product_type__manufacturer__name="Anycubic", product_type__name="PETG", color__name="Schwarz", ) spool = anycubic.get(spool_type="spool") self.assertEqual(spool.swatch, "#212721") self.assertEqual(spool.net_weight_g, 1000) self.assertEqual(anycubic.get(spool_type="refill").manufacturer_color_code, "AHPEBK-A102") for url, variant in ( ("/bestand/hinzufuegen/", refill), ("/bestellung/hinzufuegen/", spool), ): response = self.client.post( url, {"action": "lookup", "article_number": variant.manufacturer_color_code} ) self.assertEqual(response.context["form"].initial, self.selection(variant)) self.assertRedirects( self.client.post(url, response.context["form"].initial), url.removesuffix("hinzufuegen/"), ) normal.delete() spool.is_active = False spool.save(update_fields=["is_active"]) call_command("seed_catalog", verbosity=0) self.assertFalse(jayo.filter(product_type__name="PLA", spool_type="spool").exists()) spool.refresh_from_db() self.assertFalse(spool.is_active) self.assertEqual(Variant.objects.filter(product_type__manufacturer=self.maker).count(), 3)