Add search and column sorting to personal inventory
This commit is contained in:
1 parent
351b09d2f8
commit
b69e1d37bd
6 files changed
+229
-10
No files matched your search
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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, '<th title="Hersteller-Farbcode">Artikelnr.</th>')
|
||||
self.assertContains(response, 'title="Hersteller-Farbcode"')
|
||||
self.assertContains(response, "Artikelnr.")
|
||||
self.assertContains(response, '<td class="article-number">11200</td>', count=1)
|
||||
self.assertContains(response, '<td class="article-number">–</td>', count=1)
|
||||
self.assertIn("https://cdn.shopify.com", response["Content-Security-Policy"])
|
||||
|
||||
@@ -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]],
|
||||
)
|
||||
+70
-3
@@ -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",
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<span>Rollen im Bestand</span><strong>{{ total }}</strong><small>dein persönlicher Vorrat</small>
|
||||
</section>
|
||||
<section class="stat">
|
||||
<span>Filamentvarianten</span><strong>{{ items|length }}</strong><small>Farben und Ausführungen</small>
|
||||
<span>Filamentvarianten</span><strong>{{ stock_count }}</strong><small>Farben und Ausführungen</small>
|
||||
</section>
|
||||
<section class="stat privacy">
|
||||
<span>Sichtbarkeit</span>
|
||||
@@ -17,17 +17,31 @@
|
||||
</section>
|
||||
</div>
|
||||
<section class="panel">
|
||||
<div class="panel-heading">
|
||||
<div class="panel-heading stock-panel-heading">
|
||||
<h2>Dein Vorrat</h2>
|
||||
<span class="badge">{{ items|length }} Position{{ items|length|pluralize:"en" }}</span>
|
||||
<form class="stock-search" role="search" method="get" action="{% url 'inventory' %}">
|
||||
{% if sort %}
|
||||
<input type="hidden" name="sort" value="{{ sort }}">
|
||||
<input type="hidden" name="direction" value="{{ sort_direction }}">
|
||||
{% endif %}
|
||||
<label class="sr-only" for="stock-search">Vorrat nach Hersteller, Produktart, Farbe oder Artikelnummer durchsuchen</label>
|
||||
<input id="stock-search" type="search" name="q" value="{{ search }}" placeholder="z. B. ASA oder 40101" autocomplete="off">
|
||||
<button class="button secondary" type="submit">Suchen</button>
|
||||
{% if search %}
|
||||
<a href="{% url 'inventory' %}{% if sort %}?sort={{ sort }}&direction={{ sort_direction }}{% endif %}">Suche zurücksetzen</a>
|
||||
{% endif %}
|
||||
</form>
|
||||
<span class="badge">{% if search %}{{ items|length }} von {{ stock_count }} Positionen{% else %}{{ stock_count }} Position{{ stock_count|pluralize:"en" }}{% endif %}</span>
|
||||
</div>
|
||||
{% 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 %}
|
||||
<dialog class="consume-dialog" aria-labelledby="consume-title" data-consume-dialog>
|
||||
<h2 id="consume-title">Spule verbraucht</h2>
|
||||
<p class="muted" data-consume-variant></p>
|
||||
{% include "filament/consume_options.html" %}
|
||||
<form method="dialog"><button class="button secondary" type="submit">Abbrechen</button></form>
|
||||
</dialog>
|
||||
{% elif search %}
|
||||
<div class="empty"><h3>Keine passenden Filamente</h3><p>Für „{{ search }}“ gibt es keine Treffer in deinem Vorrat.</p></div>
|
||||
{% else %}<div class="empty"><span class="empty-icon" aria-hidden="true">◉</span><h3>Platz für dein erstes Filament</h3><p>Wähle eine Variante aus dem gemeinsamen Katalog und lege deinen Vorrat an.</p><a class="button" href="{% url 'inventory_add' %}">Filament auswählen</a></div>{% endif %}</section>
|
||||
<p class="hint">Gut zu wissen: Fügst du dieselbe Variante erneut hinzu, zählen wir die Stückzahl automatisch hoch.</p>{% endblock %}
|
||||
@@ -1,5 +1,20 @@
|
||||
<div class="table-wrap"><table class="inventory-table">
|
||||
<thead><tr><th>Hersteller</th><th>Produktart</th><th>Farbe</th><th>Spulenausführung</th><th title="Hersteller-Farbcode">Artikelnr.</th><th>Stück</th>{% if editable %}<th><span class="sr-only">Aktionen</span></th>{% endif %}</tr></thead>
|
||||
<thead><tr>
|
||||
{% if columns %}
|
||||
{% for column in columns %}
|
||||
<th scope="col"{% if column.title %} title="{{ column.title }}"{% endif %}{% if column.direction %} aria-sort="{{ column.direction }}"{% endif %}>
|
||||
<a class="stock-sort-link" href="?sort={{ column.key }}&direction={{ column.next_direction }}{% if search %}&q={{ search|urlencode:'' }}{% endif %}">
|
||||
{{ column.label }}
|
||||
<span aria-hidden="true">{% if column.direction == "ascending" %}↑{% elif column.direction == "descending" %}↓{% else %}↕{% endif %}</span>
|
||||
<span class="sr-only">{% if column.next_direction == "asc" %}aufsteigend{% else %}absteigend{% endif %} sortieren</span>
|
||||
</a>
|
||||
</th>
|
||||
{% endfor %}
|
||||
{% else %}
|
||||
<th>Hersteller</th><th>Produktart</th><th>Farbe</th><th>Spulenausführung</th><th title="Hersteller-Farbcode">Artikelnr.</th><th>Stück</th>
|
||||
{% endif %}
|
||||
{% if editable %}<th scope="col"><span class="sr-only">Aktionen</span></th>{% endif %}
|
||||
</tr></thead>
|
||||
{% for group in groups %}
|
||||
<tbody class="stock-group">
|
||||
{% for item in group %}
|
||||
|
||||
Reference in new issue
Block a user