diff --git a/filament/static/filament/app.css b/filament/static/filament/app.css index 3781018..300ff0c 100644 --- a/filament/static/filament/app.css +++ b/filament/static/filament/app.css @@ -348,6 +348,32 @@ p { gap: 12px; border-bottom: 1px solid var(--line); } +.stock-panel-heading { + flex-wrap: wrap; +} +.stock-search { + display: flex; + flex-wrap: wrap; + align-items: center; + gap: 8px; + max-width: 100%; +} +.stock-search input { + width: min(13rem, 100%); + min-width: 0; + flex: 1 1 10rem; + min-height: 44px; + padding: 8px 12px; + border: 1px solid #7c979e; + border-radius: 10px; + background: var(--surface-solid); + color: var(--ink); + font-size: 13px; + font-weight: 500; +} +.stock-search a { + font-size: 13px; +} .table-wrap { /* Keep absolute screen-reader labels inside the horizontally scrolling table. */ position: relative; @@ -442,6 +468,20 @@ td.quantity { letter-spacing: normal; text-transform: none; } +.stock-sort-link { + display: inline-flex; + align-items: center; + gap: 0.4rem; + color: inherit; + text-decoration: none; +} +.stock-sort-link:hover, +.stock-sort-link:focus-visible { + text-decoration: underline; +} +.inventory-table th[aria-sort] .stock-sort-link { + color: var(--green); +} .inventory-table .stock-group + .stock-group tr:first-child > * { border-top-width: 2px; } diff --git a/filament/tests/test_catalog.py b/filament/tests/test_catalog.py index 19f1d53..13263dc 100644 --- a/filament/tests/test_catalog.py +++ b/filament/tests/test_catalog.py @@ -44,7 +44,8 @@ class CatalogTests(CatalogTestCase): ) self.assertContains(response, f'src="{image_url}"', count=1) self.assertContains(response, 'referrerpolicy="no-referrer"', count=1) - self.assertContains(response, 'Artikelnr.') + self.assertContains(response, 'title="Hersteller-Farbcode"') + 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"]) diff --git a/filament/tests/test_web_inventory.py b/filament/tests/test_web_inventory.py index 8c926ad..1991641 100644 --- a/filament/tests/test_web_inventory.py +++ b/filament/tests/test_web_inventory.py @@ -1,4 +1,4 @@ -from filament.models import InventoryItem +from filament.models import Color, InventoryItem, Manufacturer, ProductType, Variant from .base import CatalogTestCase @@ -16,3 +16,85 @@ class InventoryWebTests(CatalogTestCase): self.assertContains(response, "Scharlachrot") self.client.force_login(self.bob) self.assertNotContains(self.client.get("/bestand/"), "Scharlachrot") + + def test_inventory_search_matches_each_field_and_preserves_sorting(self): + self.client.force_login(self.alice) + asa = ProductType.objects.create(manufacturer=self.maker, name="ASA Basic") + asa_spool = Variant.objects.create( + product_type=asa, color=self.red, spool_type="spool", manufacturer_color_code="40101" + ) + asa_refill = Variant.objects.create( + product_type=asa, color=self.red, spool_type="refill", manufacturer_color_code="40102" + ) + InventoryItem.objects.create(owner=self.alice, variant=asa_spool, quantity=2) + InventoryItem.objects.create(owner=self.alice, variant=asa_refill, quantity=5) + InventoryItem.objects.create(owner=self.alice, variant=self.black_spool, quantity=1) + InventoryItem.objects.create(owner=self.bob, variant=asa_spool, quantity=999) + for term, expected in ( + (" bAmBu LaB ", [asa_refill, asa_spool, self.black_spool]), + ("ASA", [asa_refill, asa_spool]), + ("scharlachROT", [asa_refill, asa_spool]), + ("40101", [asa_spool]), + ("ASA 40102", [asa_refill]), + ): + with self.subTest(term=term): + response = self.client.get( + "/bestand/", {"q": term, "sort": "quantity", "direction": "desc"} + ) + self.assertEqual([item.variant for item in response.context["items"]], expected) + self.assertEqual(response.context["total"], 8) + self.assertContains(response, 'name="sort" value="quantity"') + self.assertContains(response, 'name="direction" value="desc"') + self.assertContains(response, f"{len(expected)} von 3 Positionen") + + filtered = self.client.get("/bestand/", {"q": "40101"}) + self.assertContains(filtered, 'rowspan="1"') + self.assertNotContains(filtered, 'class="article-number">40102') + self.assertContains(filtered, 'href="?sort=quantity&direction=asc&q=40101"') + + empty = self.client.get("/bestand/", {"q": "PETG", "sort": "article", "direction": "asc"}) + self.assertContains(empty, "Keine passenden Filamente") + self.assertContains(empty, 'href="/bestand/?sort=article&direction=asc"') + self.assertEqual(empty.context["items"], []) + + def test_inventory_sorts_every_data_column_in_both_directions(self): + maker = Manufacturer.objects.create(name="Zulu") + product = ProductType.objects.create(manufacturer=maker, name="ABS Basic") + color = Color.objects.create(name="Aquablau") + other = Variant.objects.create(product_type=product, color=color, spool_type="spool") + self.spool.manufacturer_color_code = "A10" + self.spool.save(update_fields=["manufacturer_color_code"]) + self.refill.manufacturer_color_code = "A2" + self.refill.save(update_fields=["manufacturer_color_code"]) + mounted, refill, third = [ + InventoryItem.objects.create(owner=self.alice, variant=variant, quantity=quantity) + for variant, quantity in ((self.spool, 12), (self.refill, 2), (other, 4)) + ] + InventoryItem.objects.create(owner=self.bob, variant=self.black_spool, quantity=999) + self.client.force_login(self.alice) + for column, ascending, descending in ( + ("manufacturer", [mounted, refill, third], [third, mounted, refill]), + ("product", [third, mounted, refill], [mounted, refill, third]), + ("color", [third, mounted, refill], [mounted, refill, third]), + ("spool", [mounted, third, refill], [refill, mounted, third]), + ("article", [refill, mounted, third], [mounted, refill, third]), + ("quantity", [refill, third, mounted], [mounted, third, refill]), + ): + with self.subTest(column=column): + url = "/bestand/" + response = self.client.get(url, {"sort": column, "direction": "asc"}) + self.assertContains(response, 'aria-sort="ascending"', count=1) + next_url = f"{url}?sort={column}&direction=desc" + self.assertContains(response, f'href="?sort={column}&direction=desc"') + self.assertEqual(response.context["items"], ascending) + self.assertEqual( + [item for group in response.context["groups"] for item in group], ascending + ) + self.assertEqual(response.context["total"], 18) + response = self.client.get(next_url) + self.assertContains(response, 'aria-sort="descending"', count=1) + self.assertEqual(response.context["items"], descending) + self.assertEqual( + self.client.get("/bestand/", {"sort": "anything"}).context["groups"], + [[mounted, refill], [third]], + ) diff --git a/filament/views.py b/filament/views.py index 8769bef..84f971f 100644 --- a/filament/views.py +++ b/filament/views.py @@ -1,3 +1,4 @@ +import re from itertools import groupby from django.contrib import messages @@ -26,6 +27,15 @@ from .forms import ( from .models import InventoryItem, OrderItem, Printer, SharingPreferences, Variant from .services import add_quantity, consume_spool, update_order_status +STOCK_COLUMNS = ( + ("manufacturer", "Hersteller", ""), + ("product", "Produktart", ""), + ("color", "Farbe", ""), + ("spool", "Spulenausführung", ""), + ("article", "Artikelnr.", "Hersteller-Farbcode"), + ("quantity", "Stück", ""), +) + def health(request): with connection.cursor() as cursor: @@ -211,18 +221,74 @@ def inventory_consume(request, pk): @login_required def inventory(request): items = list(_stock_items().filter(owner=request.user)) + total = sum(item.quantity for item in items) + search = request.GET.get("q", "").strip() + terms = search.casefold().split() + visible_items = [item for item in items if _stock_matches(item, terms)] if terms else items + sort = request.GET.get("sort") + descending = request.GET.get("direction") == "desc" + sort_keys = { + "manufacturer": lambda item: item.variant.manufacturer.name.casefold(), + "product": lambda item: item.variant.product_type.name.casefold(), + "color": lambda item: item.variant.color.name.casefold(), + "spool": lambda item: item.get_spool_type_display().casefold(), + "article": _stock_article_sort_key, + "quantity": lambda item: item.quantity, + } + sort = sort if sort in sort_keys else "" + if sort: + visible_items.sort(key=sort_keys[sort], reverse=descending) + if sort == "article": + visible_items.sort(key=lambda item: not item.variant.manufacturer_color_code.strip()) + # These columns can put rows of the same product and color in different places. + groups = ( + [[item] for item in visible_items] + if sort in {"spool", "article", "quantity"} + else _group_stock(visible_items) + ) + columns = [ + { + "key": key, + "label": label, + "title": title, + "direction": ("descending" if descending else "ascending") if sort == key else "", + "next_direction": "desc" if sort == key and not descending else "asc", + } + for key, label, title in STOCK_COLUMNS + ] return render( request, "filament/inventory.html", { - "items": items, - "groups": _group_stock(items), - "total": sum(item.quantity for item in items), + "items": visible_items, + "stock_count": len(items), + "groups": groups, + "columns": columns, + "search": search, + "sort": sort, + "sort_direction": "desc" if descending else "asc", + "total": total, "sharing": getattr(request.user, "sharing", None), }, ) +def _stock_matches(item, terms): + variant = item.variant + fields = ( + variant.manufacturer.name.casefold(), + variant.product_type.name.casefold(), + variant.color.name.casefold(), + variant.manufacturer_color_code.casefold(), + ) + return all(any(term in field for field in fields) for term in terms) + + +def _stock_article_sort_key(item): + parts = re.split(r"([0-9]+)", item.variant.manufacturer_color_code.strip().casefold()) + return tuple(int(part) if index % 2 else part for index, part in enumerate(parts)) + + def _stock_items(): return InventoryItem.objects.select_related( "variant__product_type__manufacturer", "variant__color" @@ -231,6 +297,7 @@ def _stock_items(): "variant__product_type__name", "variant__color__name", "-spool_type", + "pk", ) diff --git a/templates/filament/inventory.html b/templates/filament/inventory.html index 0122151..e894dfe 100644 --- a/templates/filament/inventory.html +++ b/templates/filament/inventory.html @@ -5,7 +5,7 @@ Rollen im Bestand{{ total }}dein persönlicher Vorrat
- Filamentvarianten{{ items|length }}Farben und Ausführungen + Filamentvarianten{{ stock_count }}Farben und Ausführungen
Sichtbarkeit @@ -17,17 +17,31 @@
-
+

Dein Vorrat

- {{ items|length }} Position{{ items|length|pluralize:"en" }} + + {% if search %}{{ items|length }} von {{ stock_count }} Positionen{% else %}{{ stock_count }} Position{{ stock_count|pluralize:"en" }}{% endif %}
{% if groups %} - {% include "filament/stock_table.html" with groups=groups editable=True only %} + {% include "filament/stock_table.html" with groups=groups columns=columns editable=True search=search only %}

Spule verbraucht

{% include "filament/consume_options.html" %}
+ {% elif search %} +

Keine passenden Filamente

Für „{{ search }}“ gibt es keine Treffer in deinem Vorrat.

{% else %}

Platz für dein erstes Filament

Wähle eine Variante aus dem gemeinsamen Katalog und lege deinen Vorrat an.

Filament auswählen
{% endif %}

Gut zu wissen: Fügst du dieselbe Variante erneut hinzu, zählen wir die Stückzahl automatisch hoch.

{% endblock %} diff --git a/templates/filament/stock_table.html b/templates/filament/stock_table.html index eea3dd2..8d5e1dd 100644 --- a/templates/filament/stock_table.html +++ b/templates/filament/stock_table.html @@ -1,5 +1,20 @@
- {% if editable %}{% endif %} + + {% if columns %} + {% for column in columns %} + + {% endfor %} + {% else %} + + {% endif %} + {% if editable %}{% endif %} + {% for group in groups %} {% for item in group %}
HerstellerProduktartFarbeSpulenausführungArtikelnr.StückAktionen
+ + {{ column.label }} + + {% if column.next_direction == "asc" %}aufsteigend{% else %}absteigend{% endif %} sortieren + + HerstellerProduktartFarbeSpulenausführungArtikelnr.StückAktionen