Add consolidated shopping list for filament orders

This commit is contained in:
Mikei386 committed 2026-11-26 21:39:00 +01:00
1 parent 9f07b07bb7
commit 6f2e322b74
8 files changed
+222 -3

No files matched your search

+1
View File
@@ -73,6 +73,7 @@ urlpatterns = [
path("bestand/<int:pk>/loeschen/", views.inventory_delete, name="inventory_delete"),
path("bestand/<int:pk>/anbieten/", views.marketplace_offer, name="marketplace_offer"),
path("bestellung/", views.orders, name="orders"),
path("bestellung/einkaufsliste/", views.order_shopping_list, name="order_shopping_list"),
path("bestellung/historie/", views.order_history, name="order_history"),
path(
"bestellung/historie/<int:pk>/uebergabe-rueckgaengig/",
+1
View File
@@ -16,6 +16,7 @@ class PrivatePageMiddleware:
"empty_spools",
"shared_inventory",
"orders",
"order_shopping_list",
"order_history",
"marketplace",
"marketplace_take",
+17
View File
@@ -325,6 +325,20 @@ main {
gap: 20px;
margin-bottom: clamp(22px, 3vw, 36px);
}
.order-page-actions,
.order-shop-links {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.article-copy {
display: flex;
align-items: center;
gap: 10px;
}
.article-copy [hidden] {
display: none;
}
.eyebrow {
letter-spacing: 2px;
font-size: 10px;
@@ -1222,6 +1236,9 @@ td.quantity {
.page-heading > .button {
margin-top: 16px;
}
.order-page-actions {
margin-top: 16px;
}
.stats {
grid-template-columns: repeat(2, minmax(0, 1fr));
gap: 10px;
@@ -0,0 +1,30 @@
"use strict";
const copyStatus = document.querySelector("[data-copy-status]");
for (const button of document.querySelectorAll("[data-copy-article]")) {
button.hidden = false;
let resetLabel;
button.addEventListener("click", async () => {
const article = document.getElementById(button.dataset.copyArticle);
const code = article.textContent.trim();
clearTimeout(resetLabel);
copyStatus.textContent = "";
try {
await navigator.clipboard.writeText(code);
button.textContent = "Kopiert";
copyStatus.textContent = `Artikelnummer ${code} kopiert.`;
} catch {
const range = document.createRange();
range.selectNodeContents(article);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
button.textContent = "Markiert";
copyStatus.textContent = "Artikelnummer markiert. Kopiere die Auswahl.";
}
resetLabel = setTimeout(() => {
button.textContent = "Kopieren";
}, 2000);
});
}
+60 -1
View File
@@ -1,17 +1,76 @@
from datetime import UTC, datetime
from unittest.mock import patch
from urllib.parse import parse_qs, urlsplit
from django.contrib.auth import get_user_model
from django.test import Client
from django.urls import reverse
from filament.models import InventoryItem, OrderItem, StockMovement
from filament.models import Color, InventoryItem, OrderItem, StockMovement, Variant
from filament.services import add_quantity
from .base import CatalogTestCase
class SharedOrderTests(CatalogTestCase):
def test_shopping_list_combines_open_quantities_and_preserves_spool_variants(self):
url = reverse("order_shopping_list")
self.assertRedirects(self.client.get(url), f"/anmelden/?next={url}")
Variant.objects.filter(pk__in=[self.spool.pk, self.refill.pk]).update(
manufacturer_color_code="10100"
)
Variant.objects.filter(pk=self.black_spool.pk).update(manufacturer_color_code="10101")
OrderItem.objects.create(owner=self.alice, variant=self.spool, quantity=1)
OrderItem.objects.create(owner=self.bob, variant=self.spool, quantity=2)
OrderItem.objects.create(owner=self.alice, variant=self.refill, quantity=1)
OrderItem.objects.create(owner=self.bob, variant=self.black_spool, quantity=1)
for status in (
OrderItem.Status.ORDERED,
OrderItem.Status.ARRIVED,
OrderItem.Status.HANDED_OVER,
):
OrderItem.objects.create(owner=self.bob, variant=self.spool, quantity=4, status=status)
self.client.force_login(self.alice)
self.assertContains(self.client.get(reverse("orders")), url)
result = self.client.get(url, {"q": "10100"})
self.assertEqual(
{variant.pk: variant.order_quantity for variant in result.context["variants"]},
{self.spool.pk: 3, self.refill.pk: 1},
)
self.assertContains(result, 'aria-label="Artikelnummer 10100 kopieren"', count=2)
self.assertEqual(result.context["total"], 4)
self.assertEqual(
result.context["shop_searches"],
["https://eu.store.bambulab.com/search?q=10100"],
)
self.assertEqual(self.client.get(url, {"q": "10100, 10101"}).context["total"], 5)
self.assertEqual(self.client.post(url).status_code, 405)
def test_shopping_list_shop_search_batches_distinct_codes(self):
codes = [str(10800 + offset) for offset in range(13)]
for code in codes:
variant = Variant.objects.create(
product_type=self.product,
color=Color.objects.create(name=code),
spool_type=Variant.SpoolType.SPOOL,
manufacturer_color_code=code,
)
OrderItem.objects.create(owner=self.alice, variant=variant, quantity=1)
OrderItem.objects.create(
owner=self.alice, variant=self.spool, quantity=2, status=OrderItem.Status.UNAVAILABLE
)
self.client.force_login(self.bob)
url = reverse("order_shopping_list")
result = self.client.get(url)
batches = [
parse_qs(urlsplit(link).query)["q"][0].split(", ")
for link in result.context["shop_searches"]
]
self.assertEqual(batches, [codes[:12], codes[12:]])
self.assertContains(result, "Im Bambu-Shop suchen (2/2)")
self.assertEqual(result.context["total"], 13)
def test_history_is_shared_and_records_order_and_handover_times(self):
url = reverse("order_history")
self.assertRedirects(self.client.get(url), f"/anmelden/?next={url}")
+50 -1
View File
@@ -1,6 +1,7 @@
import re
from itertools import groupby
from itertools import batched, groupby
from pathlib import PurePosixPath
from urllib.parse import urlencode
from uuid import UUID, uuid4
from django.contrib import messages
@@ -940,6 +941,54 @@ def orders(request):
)
@login_required
@require_http_methods(["GET", "HEAD"])
def order_shopping_list(request):
search = request.GET.get("q", "").strip()[:200]
codes = {code.casefold() for code in re.split(r"[\s,;]+", search) if code}
if search:
codes.add(search.casefold())
variants = list(
Variant.objects.filter(orderitem__status=OrderItem.Status.OPEN)
.select_related("product_type__manufacturer", "color")
.annotate(order_quantity=Sum("orderitem__quantity"))
.order_by(
"product_type__manufacturer__name",
"product_type__name",
"color__name",
"spool_type",
"pk",
)
)
if search:
variants = [
variant
for variant in variants
if variant.manufacturer_color_code.strip().casefold() in codes
]
bambu_codes = dict.fromkeys(
variant.manufacturer_color_code.strip()
for variant in variants
if variant.manufacturer.name.casefold() == "bambu lab"
and re.fullmatch(r"[0-9]{5}", variant.manufacturer_color_code.strip())
)
# The shop accepted a dozen codes in one search. A full order can exceed its limit.
shop_searches = [
"https://eu.store.bambulab.com/search?" + urlencode({"q": ", ".join(batch)})
for batch in batched(bambu_codes, 12)
]
return render(
request,
"filament/order_shopping_list.html",
{
"variants": variants,
"total": sum(variant.order_quantity for variant in variants),
"search": search,
"shop_searches": shop_searches,
},
)
@login_required
@require_http_methods(["POST"])
def order_status(request, pk):
@@ -0,0 +1,59 @@
{% extends "base.html" %}
{% load static %}
{% block title %}Konsolidierte Bestellliste · Fil-A-Management{% endblock %}
{% block content %}
<a class="back-link" href="{% url 'orders' %}">← Sammelbestellung nach Person</a>
<div class="page-heading">
<div><h1>Konsolidierte Bestellliste</h1><p class="muted">Offene Bestellwünsche aller Personen, zusammengezählt pro Variante.</p></div>
</div>
{% include "filament/list_search.html" with search_url="order_shopping_list" search_label="Nach einer oder mehreren Artikelnummern suchen" placeholder="Artikelnummern, z. B. 10100, 10604" %}
{% if shop_searches %}
<div class="order-shop-links" role="group" aria-label="Bambu-Lab-Shop-Suche">
{% for url in shop_searches %}
<a class="button secondary" href="{{ url }}" target="_blank" rel="noreferrer">Im Bambu-Shop suchen{% if shop_searches|length > 1 %} ({{ forloop.counter }}/{{ shop_searches|length }}){% endif %} ↗</a>
{% endfor %}
</div>
<p class="hint">Prüfe im Shop Farbe, Spulenausführung und Stückzahl anhand dieser Liste.</p>
{% endif %}
{% if variants %}
<section class="panel" aria-label="Offene Bestellwünsche">
<div class="panel-heading">
<h2>{% if search %}Suchergebnis{% else %}Offene Bestellwünsche{% endif %}</h2>
<span class="badge">{{ total }} Rolle{{ total|pluralize:"n" }} · {{ variants|length }} Variante{{ variants|length|pluralize:"n" }}</span>
</div>
<div class="table-wrap">
<table class="order-table">
<thead><tr><th scope="col">Stück</th><th scope="col">Produktart</th><th scope="col">Farbe</th><th scope="col">Spulenausführung</th><th scope="col">Artikelnr.</th></tr></thead>
<tbody>
{% for variant in variants %}
{% with article=variant.manufacturer_color_code.strip %}
<tr>
<td class="quantity">{{ variant.order_quantity }}</td>
<td>{{ variant.product_type.name }}<small>{{ variant.manufacturer }}</small></td>
<td><div class="variant-preview">{% include "filament/variant_thumbnail.html" with variant=variant only %}<strong>{{ variant.color_label }}</strong></div></td>
<td>
<span class="badge">{{ variant.get_spool_type_display }}</span>
{% if variant.net_weight_g or variant.diameter_mm %}<small>{% if variant.net_weight_g %}{{ variant.net_weight_g }} g{% endif %}{% if variant.net_weight_g and variant.diameter_mm %} · {% endif %}{% if variant.diameter_mm %}{{ variant.diameter_mm }} mm{% endif %}</small>{% endif %}
{% if not variant.is_active %}<small class="warning">Katalogvariante deaktiviert</small>{% endif %}
</td>
<td class="article-number">
{% if article %}
<div class="article-copy">
<span id="order-article-{{ variant.pk }}">{{ article }}</span>
<button type="button" class="button secondary" data-copy-article="order-article-{{ variant.pk }}" aria-label="Artikelnummer {{ article }} kopieren" hidden>Kopieren</button>
</div>
{% else %}–{% endif %}
</td>
</tr>
{% endwith %}
{% endfor %}
</tbody>
</table>
</div>
<p class="sr-only" role="status" data-copy-status></p>
</section>
<script src="{% static 'filament/order_shopping_list.js' %}" defer></script>
{% else %}
<section class="panel"><div class="empty"><h2>{% if search %}Keine offenen Wünsche zu diesen Artikelnummern{% else %}Keine offenen Bestellwünsche{% endif %}</h2></div></section>
{% endif %}
{% endblock %}
+4 -1
View File
@@ -3,7 +3,10 @@
{% block content %}
<div class="page-heading">
<div><h1>Sammelbestellung</h1><p class="muted">Bestellwünsche und Lieferstatus pro Person.</p></div>
<a class="button" href="{% url 'order_add' %}">+ Bestellwunsch hinzufügen</a>
<div class="order-page-actions">
<a class="button secondary" href="{% url 'order_shopping_list' %}">Konsolidierte Bestellliste</a>
<a class="button" href="{% url 'order_add' %}">+ Bestellwunsch hinzufügen</a>
</div>
</div>
<div class="stats">
<section class="stat"><span>Rollen insgesamt</span><strong>{{ total }}</strong></section>