Live refresh inventory scan review counts
This commit is contained in:
+19
-6
@@ -24,6 +24,16 @@ def _owned_items(user):
|
||||
return KitchenInventoryItem.objects.filter(user=user, archived_at__isnull=True).select_related('product', 'location', 'category')
|
||||
|
||||
|
||||
def _inventory_review_counts(user):
|
||||
review_items = InventoryScanItem.objects.filter(batch__run__user=user)
|
||||
return {
|
||||
'processing_count': review_items.filter(status__in=(InventoryScanItem.Status.QUEUED,
|
||||
InventoryScanItem.Status.PROCESSING)).count(),
|
||||
'needs_review_count': review_items.filter(status__in=(InventoryScanItem.Status.NEEDS_REVIEW,
|
||||
InventoryScanItem.Status.DEFERRED, InventoryScanItem.Status.FAILED)).count(),
|
||||
}
|
||||
|
||||
|
||||
@login_required
|
||||
def overview(request):
|
||||
items = _owned_items(request.user)
|
||||
@@ -74,11 +84,7 @@ def overview(request):
|
||||
if item.inner_packs_per_package and 'inner_packs' not in cards[key]['units']:
|
||||
cards[key]['units'].append('inner_packs')
|
||||
recent = InventoryConsumption.objects.filter(user=request.user, reverses__isnull=True).select_related('product').prefetch_related('lines__package').order_by('-created_at')[:8]
|
||||
review_items = InventoryScanItem.objects.filter(batch__run__user=request.user)
|
||||
processing_count = review_items.filter(status__in=(InventoryScanItem.Status.QUEUED,
|
||||
InventoryScanItem.Status.PROCESSING)).count()
|
||||
needs_review_count = review_items.filter(status__in=(InventoryScanItem.Status.NEEDS_REVIEW,
|
||||
InventoryScanItem.Status.DEFERRED, InventoryScanItem.Status.FAILED)).count()
|
||||
review_counts = _inventory_review_counts(request.user)
|
||||
preference = InventoryReviewPreference.objects.filter(user=request.user).first()
|
||||
return render(request, 'food/inventory_overview.html', {
|
||||
'items': items, 'cards': list(cards.values()), 'recent_consumption': recent,
|
||||
@@ -87,11 +93,18 @@ def overview(request):
|
||||
'locations': InventoryLocation.objects.filter(user=request.user),
|
||||
'categories': InventoryCategory.objects.filter(user=request.user),
|
||||
'today': timezone.localdate(), 'soon_date': timezone.localdate() + timedelta(days=14),
|
||||
'processing_count': processing_count, 'needs_review_count': needs_review_count,
|
||||
**review_counts,
|
||||
'review_mode': preference.mode if preference else InventoryReviewPreference.Mode.INDIVIDUAL,
|
||||
})
|
||||
|
||||
|
||||
@login_required
|
||||
@require_http_methods(['GET'])
|
||||
def inventory_staging_counts(request):
|
||||
return render(request, 'food/partials/inventory_staging_counts.html',
|
||||
_inventory_review_counts(request.user))
|
||||
|
||||
|
||||
@login_required
|
||||
@require_http_methods(['GET', 'POST'])
|
||||
def add(request):
|
||||
|
||||
@@ -189,6 +189,84 @@ body[data-theme="dark"] .food-tabs a[aria-current=page]{color:#102317}
|
||||
.food-quantity-form{display:flex;align-items:center;gap:.35rem}.food-quantity{font-weight:700;white-space:nowrap}.food-quantity-form button{min-width:44px;min-height:44px;border:1px solid var(--food-border);border-radius:50%;background:var(--food-surface);color:var(--food-ink);font:inherit;font-size:1.4rem;cursor:pointer}.food-quantity-form button:focus-visible{outline:2px solid var(--food-green);outline-offset:2px}.food-quantity-form.htmx-request button{opacity:.6;cursor:progress}.food-screen-reader{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border:0}
|
||||
.food-quantity-controls{display:flex;align-items:center;gap:.35rem;white-space:nowrap}.food-quantity-controls .food-quantity{min-width:2.5ch;text-align:center}.food-quantity-controls .food-quantity-form{margin:0}
|
||||
|
||||
/* Bulk scanner surfaces follow the app's dark appearance setting. */
|
||||
body[data-theme="dark"] .inventory-staging,
|
||||
body[data-theme="dark"] .inventory-review-page .food-card,
|
||||
body[data-theme="dark"] .inventory-scan-start,
|
||||
body[data-theme="dark"] .inventory-scan-camera,
|
||||
body[data-theme="dark"] .inventory-scan-active,
|
||||
body[data-theme="dark"] .inventory-scan-queue {border-color:#3b473f;background:#202923;color:#edf5ef}
|
||||
body[data-theme="dark"] .inventory-staging {background:#242e27}
|
||||
body[data-theme="dark"] .inventory-staging-top h2,
|
||||
body[data-theme="dark"] .inventory-review-page h2,
|
||||
body[data-theme="dark"] .inventory-review-page .food-heading h1,
|
||||
body[data-theme="dark"] .inventory-scan-page>.food-heading h1,
|
||||
body[data-theme="dark"] .inventory-scan-queue-heading h2,
|
||||
body[data-theme="dark"] .inventory-scan-queue-item-main strong {color:#edf5ef}
|
||||
body[data-theme="dark"] .inventory-staging-top p:last-child,
|
||||
body[data-theme="dark"] .inventory-review-page .food-heading p:last-child,
|
||||
body[data-theme="dark"] .inventory-review-row p,
|
||||
body[data-theme="dark"] .inventory-bulk-review>p,
|
||||
body[data-theme="dark"] .inventory-bulk-selection small,
|
||||
body[data-theme="dark"] .inventory-review-preview span,
|
||||
body[data-theme="dark"] .inventory-scan-meta,
|
||||
body[data-theme="dark"] .inventory-scan-hint,
|
||||
body[data-theme="dark"] .inventory-scan-note,
|
||||
body[data-theme="dark"] .inventory-scan-empty,
|
||||
body[data-theme="dark"] .inventory-scan-queue-item-main small,
|
||||
body[data-theme="dark"] .inventory-scan-queue-item-side small,
|
||||
body[data-theme="dark"] .inventory-scan-queue-heading>span,
|
||||
body[data-theme="dark"] .inventory-scan-batch-heading>span {color:#b8c3bb}
|
||||
body[data-theme="dark"] .inventory-kicker,
|
||||
body[data-theme="dark"] .inventory-scan-active-heading,
|
||||
body[data-theme="dark"] .inventory-scan-batch-heading h3 {color:#9bd5ae}
|
||||
body[data-theme="dark"] .inventory-staging-counts>a,
|
||||
body[data-theme="dark"] .inventory-review-mode button,
|
||||
body[data-theme="dark"] .inventory-review-toolbar button,
|
||||
body[data-theme="dark"] .inventory-review-states a {border-color:#46544b;background:#27312b;color:#edf5ef}
|
||||
body[data-theme="dark"] .inventory-staging-counts strong {color:#9bd5ae}
|
||||
body[data-theme="dark"] .inventory-review-mode button.is-selected,
|
||||
body[data-theme="dark"] .inventory-review-toolbar button.is-selected,
|
||||
body[data-theme="dark"] .inventory-review-states a[aria-current=page] {border-color:#568466;background:#294332;color:#d9f5e0}
|
||||
body[data-theme="dark"] .inventory-scan-location {border-color:#568466;background:#263c2e;color:#b9edc8}
|
||||
body[data-theme="dark"] .inventory-scan-location-panel,
|
||||
body[data-theme="dark"] .inventory-scan-controls {border-color:#3b473f;background:#26342b;color:#d1ddd4}
|
||||
body[data-theme="dark"] .inventory-scan-location-form select,
|
||||
body[data-theme="dark"] .inventory-scan-start-form select,
|
||||
body[data-theme="dark"] .inventory-scan-next-row select,
|
||||
body[data-theme="dark"] .inventory-scan-active input,
|
||||
body[data-theme="dark"] .inventory-scan-active select,
|
||||
body[data-theme="dark"] .inventory-scan-active textarea,
|
||||
body[data-theme="dark"] .inventory-review-fields input,
|
||||
body[data-theme="dark"] .inventory-review-fields select,
|
||||
body[data-theme="dark"] .inventory-review-fields textarea,
|
||||
body[data-theme="dark"] .inventory-review-shared-fields input,
|
||||
body[data-theme="dark"] .inventory-review-shared-fields select,
|
||||
body[data-theme="dark"] .inventory-review-nutrients input {border-color:#536159;background:#161d19;color:#edf5ef;color-scheme:dark}
|
||||
body[data-theme="dark"] .inventory-scan-active form>label,
|
||||
body[data-theme="dark"] .inventory-scan-active .inventory-scan-product-title,
|
||||
body[data-theme="dark"] .inventory-scan-fields label,
|
||||
body[data-theme="dark"] .inventory-scan-queue-item-side {color:#d9e4dc}
|
||||
body[data-theme="dark"] .inventory-scan-more,
|
||||
body[data-theme="dark"] .inventory-scan-nutrition,
|
||||
body[data-theme="dark"] .inventory-scan-count-controls button {border-color:#3b473f;background:#27312b;color:#edf5ef}
|
||||
body[data-theme="dark"] .inventory-scan-batch-heading,
|
||||
body[data-theme="dark"] .inventory-scan-queue-item,
|
||||
body[data-theme="dark"] .inventory-scan-manual,
|
||||
body[data-theme="dark"] .inventory-scan-nutrition dl div,
|
||||
body[data-theme="dark"] .inventory-bulk-selection,
|
||||
body[data-theme="dark"] .inventory-bulk-selection label,
|
||||
body[data-theme="dark"] .inventory-review-preview {border-color:#3b473f}
|
||||
body[data-theme="dark"] .inventory-scan-nutrition dt {color:#b8c3bb}
|
||||
body[data-theme="dark"] .inventory-scan-controls .inventory-scan-done {background:#39734e}
|
||||
body[data-theme="dark"] .inventory-review-error,
|
||||
body[data-theme="dark"] .inventory-scan-error,
|
||||
body[data-theme="dark"] .inventory-scan-location-form [data-scan-location-error]:not([hidden]) {color:#ffb4a9}
|
||||
body[data-theme="dark"] .inventory-review-success {color:#9bd5ae}
|
||||
body[data-theme="dark"] .inventory-scan-page :focus-visible,
|
||||
body[data-theme="dark"] .inventory-review-page :focus-visible,
|
||||
body[data-theme="dark"] .inventory-staging :focus-visible {outline-color:#f2c16d}
|
||||
|
||||
/* Shared ring geometry: the outer lap stays complete beneath excess progress. */
|
||||
.food-ring{position:relative;display:grid;place-items:center;background:none}
|
||||
.food-ring svg{display:block;width:100%;height:100%;transform:rotate(-90deg);overflow:visible}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
{% extends 'planner/base.html' %}{% load static %}
|
||||
{% block title %}Kitchen Inventory · Tasknest{% endblock %}
|
||||
{% block content %}<link rel="stylesheet" href="{% static 'food/food.css' %}?v=20261026-review-1">
|
||||
{% block content %}<link rel="stylesheet" href="{% static 'food/food.css' %}?v=20261026-review-2">
|
||||
<div class="food-page inventory-page"><header class="food-heading"><div><h1>Kitchen Inventory</h1><p>What you have and where it lives.</p></div><div class="inventory-heading-actions"><a class="button" href="{% url 'food:inventory-add' %}">+ Add item</a><a class="button button--secondary" href="{% url 'food:scanner' %}?mode=inventory">Scan barcode</a></div></header>
|
||||
<section class="food-card inventory-staging" id="inventory-review" aria-labelledby="inventory-staging-title"><div class="inventory-staging-top"><div><p class="inventory-kicker">Bulk scanner</p><h2 id="inventory-staging-title">Staged scan items</h2><p>Track background enrichment and finish products that need your input.</p></div><a class="button" href="{% url 'food:inventory-review' %}">Open review</a></div><div class="inventory-staging-counts"><a href="{% url 'food:inventory-review' %}?state=processing"><strong>{{ processing_count }}</strong><span>Processing</span></a><a href="{% url 'food:inventory-review' %}"><strong>{{ needs_review_count }}</strong><span>Needs Review</span></a></div><form method="post" action="{% url 'food:inventory-review' %}" class="inventory-review-mode">{% csrf_token %}<input type="hidden" name="action" value="mode"><span>Review mode</span><button name="mode" value="individual" aria-pressed="{% if review_mode == 'individual' %}true{% else %}false{% endif %}" class="{% if review_mode == 'individual' %}is-selected{% endif %}">Individual</button><button name="mode" value="bulk" aria-pressed="{% if review_mode == 'bulk' %}true{% else %}false{% endif %}" class="{% if review_mode == 'bulk' %}is-selected{% endif %}">Bulk review</button></form></section>
|
||||
<section class="food-card inventory-staging" id="inventory-review" aria-labelledby="inventory-staging-title"><div class="inventory-staging-top"><div><p class="inventory-kicker">Bulk scanner</p><h2 id="inventory-staging-title">Staged scan items</h2><p>Track background enrichment and finish products that need your input.</p></div><a class="button" href="{% url 'food:inventory-review' %}">Open review</a></div>{% include 'food/partials/inventory_staging_counts.html' %}<form method="post" action="{% url 'food:inventory-review' %}" class="inventory-review-mode">{% csrf_token %}<input type="hidden" name="action" value="mode"><span>Review mode</span><button name="mode" value="individual" aria-pressed="{% if review_mode == 'individual' %}true{% else %}false{% endif %}" class="{% if review_mode == 'individual' %}is-selected{% endif %}">Individual</button><button name="mode" value="bulk" aria-pressed="{% if review_mode == 'bulk' %}true{% else %}false{% endif %}" class="{% if review_mode == 'bulk' %}is-selected{% endif %}">Bulk review</button></form></section>
|
||||
<form method="get" class="food-card inventory-filters" role="search" aria-label="Search kitchen inventory">
|
||||
<div class="inventory-filter-search"><label for="inventory-search">Search inventory</label><div><input id="inventory-search" type="search" name="q" value="{{ query }}"><button class="button" type="submit">Search</button></div></div>
|
||||
<details class="inventory-filter-details" data-responsive-filters open><summary>More filters{% if selected_location or selected_category or opened or use_soon == '1' or sort != 'location' %} · Applied{% endif %}</summary><div class="inventory-filter-options"><label>Location<select name="location"><option value="">All locations</option>{% for location in locations %}<option value="{{ location.pk }}" {% if selected_location == location.pk|stringformat:'s' %}selected{% endif %}>{{ location.name }}</option>{% endfor %}</select></label><label>Category<select name="category"><option value="">All categories</option>{% for category in categories %}<option value="{{ category.pk }}" {% if selected_category == category.pk|stringformat:'s' %}selected{% endif %}>{{ category.name }}</option>{% endfor %}</select></label><label>Opened<select name="opened"><option value="">All</option><option value="yes" {% if opened == 'yes' %}selected{% endif %}>Opened</option><option value="no" {% if opened == 'no' %}selected{% endif %}>Unopened</option></select></label><label>Sort<select name="sort"><option value="location" {% if sort == 'location' %}selected{% endif %}>Location</option><option value="category" {% if sort == 'category' %}selected{% endif %}>Category</option><option value="name" {% if sort == 'name' %}selected{% endif %}>Product name</option><option value="recent" {% if sort == 'recent' %}selected{% endif %}>Recently added</option><option value="best_before" {% if sort == 'best_before' %}selected{% endif %}>Best before</option></select></label><label class="inventory-check"><input type="checkbox" name="use_soon" value="1" {% if use_soon == '1' %}checked{% endif %}> Use soon</label><button class="button" type="submit">Apply filters</button></div></details>
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
<div class="inventory-staging-counts" id="inventory-staging-counts"
|
||||
hx-get="{% url 'food:inventory-staging-counts' %}"
|
||||
hx-trigger="load, every 5s" hx-swap="outerHTML"
|
||||
aria-live="polite" aria-atomic="true">
|
||||
<a href="{% url 'food:inventory-review' %}?state=processing"><strong>{{ processing_count }}</strong><span>Processing</span></a>
|
||||
<a href="{% url 'food:inventory-review' %}"><strong>{{ needs_review_count }}</strong><span>Needs Review</span></a>
|
||||
</div>
|
||||
+30
-1
@@ -12,7 +12,9 @@ from planner.models import UserSettings
|
||||
|
||||
from .services.diary import diary_context, ring_progress
|
||||
|
||||
from .models import FoodFavourite, FoodLogAttachment, FoodLogEntry, FoodPortion, FoodProduct, MealType, Unit, InventoryLocation, KitchenInventoryItem, InventoryConsumption
|
||||
from .models import (FoodFavourite, FoodLogAttachment, FoodLogEntry, FoodPortion, FoodProduct, MealType, Unit,
|
||||
InventoryLocation, KitchenInventoryItem, InventoryConsumption, InventoryScanRun,
|
||||
InventoryScanBatch, InventoryScanItem)
|
||||
|
||||
|
||||
class FoodFoundationTests(TestCase):
|
||||
@@ -21,6 +23,33 @@ class FoodFoundationTests(TestCase):
|
||||
self.other = get_user_model().objects.create_user(username='food-two', password='test-password')
|
||||
self.product = FoodProduct.objects.create(owner=self.user, product_name='Yoghurt', barcode='123', base_unit=Unit.GRAM)
|
||||
|
||||
def test_inventory_staging_counts_refresh_and_are_user_scoped(self):
|
||||
location = InventoryLocation.objects.create(user=self.user, name='Fridge')
|
||||
run = InventoryScanRun.objects.create(user=self.user)
|
||||
batch = InventoryScanBatch.objects.create(run=run, location=location, sequence=1)
|
||||
InventoryScanItem.objects.create(batch=batch, location=location, barcode='111',
|
||||
status=InventoryScanItem.Status.QUEUED)
|
||||
InventoryScanItem.objects.create(batch=batch, location=location, barcode='222',
|
||||
status=InventoryScanItem.Status.FAILED)
|
||||
other_location = InventoryLocation.objects.create(user=self.other, name='Other fridge')
|
||||
other_run = InventoryScanRun.objects.create(user=self.other)
|
||||
other_batch = InventoryScanBatch.objects.create(run=other_run, location=other_location, sequence=1)
|
||||
InventoryScanItem.objects.create(batch=other_batch, location=other_location, barcode='333',
|
||||
status=InventoryScanItem.Status.PROCESSING)
|
||||
self.client.force_login(self.user)
|
||||
|
||||
url = reverse('food:inventory-staging-counts')
|
||||
response = self.client.get(url, secure=True)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertContains(response, '<strong>1</strong>', html=True)
|
||||
self.assertContains(response, '<strong>1</strong>', html=True)
|
||||
self.assertContains(response, 'hx-trigger="load, every 5s"')
|
||||
|
||||
InventoryScanItem.objects.filter(batch=batch, barcode='111').update(status=InventoryScanItem.Status.COMPLETE)
|
||||
response = self.client.get(url, secure=True)
|
||||
self.assertContains(response, '<strong>0</strong>', html=True)
|
||||
self.assertContains(response, '<strong>1</strong>', html=True)
|
||||
|
||||
def test_meals_are_fixed(self):
|
||||
self.assertEqual(list(MealType.values), ['breakfast', 'lunch', 'dinner', 'snacks'])
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ urlpatterns = [
|
||||
path('settings/tokens/', token_views.tokens, name='tokens'),
|
||||
path('settings/tokens/<int:pk>/revoke/', token_views.revoke_token, name='token-revoke'),
|
||||
path('inventory/', inventory.overview, name='inventory'),
|
||||
path('inventory/staging-counts/', inventory.inventory_staging_counts, name='inventory-staging-counts'),
|
||||
path('inventory/add/', inventory.add, name='inventory-add'),
|
||||
path('inventory/lookup/', inventory.lookup, name='inventory-lookup'),
|
||||
path('inventory/search/openfoodfacts/', inventory.external_search, name='inventory-external'),
|
||||
|
||||
Reference in New Issue
Block a user