Add continuous inventory barcode scanning workflow

This commit is contained in:
Joren de Graaf
2026-10-26 01:19:55 +01:00
parent a66ecd7c85
commit d305a99b1b
10 changed files with 776 additions and 11 deletions
+156
View File
@@ -0,0 +1,156 @@
import json
from django.contrib.auth.decorators import login_required
from django.db.models import Prefetch
from django.http import JsonResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.template.loader import render_to_string
from django.urls import reverse
from django.views.decorators.http import require_GET, require_POST
from .models import (InventoryLocation, InventoryScanBatch, InventoryScanItem,
InventoryScanRun)
from .services.inventory_scanner import (ScanWorkflowError, adjust_confirmed_count,
confirm_scan_item, create_active_scan, discard_active_scan,
get_or_create_active_run, resume_active_run, submit_batch)
@login_required
def scanner(request):
run = resume_active_run(request.user)
draft_batch = run.batches.filter(status=InventoryScanBatch.Status.DRAFT).first() if run else None
active_item = draft_batch.items.filter(status=InventoryScanItem.Status.ACTIVE).first() if draft_batch else None
queued_count = sum(batch.items.exclude(status=InventoryScanItem.Status.ACTIVE).count()
for batch in run.batches.all()) if run else 0
queue_batches = _queue_batches(run) if run else []
return render(request, 'food/inventory_scanner.html', {
'run': run, 'draft_batch': draft_batch, 'active_item': active_item,
'locations': InventoryLocation.objects.filter(user=request.user), 'queued_count': queued_count,
'queue_batches': queue_batches,
})
@login_required
@require_POST
def start_run(request):
try:
run = get_or_create_active_run(request.user, request.POST.get('location_id'))
except (ScanWorkflowError, TypeError, ValueError) as error:
return render(request, 'food/inventory_scanner.html', {
'locations': InventoryLocation.objects.filter(user=request.user), 'start_error': str(error),
}, status=400)
return redirect('food:scanner-inventory')
@login_required
@require_POST
def create_scan(request, run_id):
run = get_object_or_404(InventoryScanRun, api_id=run_id, user=request.user,
status=InventoryScanRun.Status.ACTIVE)
payload, error = _payload(request)
if error:
return JsonResponse({'error': error}, status=400)
try:
item = create_active_scan(run, request.user, str(payload.get('barcode', '')).strip())
except ScanWorkflowError as error:
return JsonResponse({'error': str(error)}, status=400)
html = render_to_string('food/partials/inventory_scan_active.html', {
'item': item, 'locations': InventoryLocation.objects.filter(user=request.user),
}, request=request)
return JsonResponse({'item': _item_json(item), 'existing': bool(item.source_data.get('cached')), 'html': html})
@login_required
@require_POST
def confirm_scan(request, item_id):
payload, error = _payload(request)
if error:
return JsonResponse({'error': error}, status=400)
payload['opened'] = str(payload.get('opened', '')).lower() in {'true', '1', 'yes', 'on'}
try:
item = confirm_scan_item(request.user, item_id, payload)
except (ScanWorkflowError, TypeError, ValueError) as error:
return JsonResponse({'error': str(error)}, status=400)
return JsonResponse({'item': _item_json(item)})
@login_required
@require_POST
def discard_scan(request, item_id):
try:
discard_active_scan(request.user, item_id)
except ScanWorkflowError as error:
return JsonResponse({'error': str(error)}, status=400)
return JsonResponse({'ok': True})
@login_required
@require_POST
def adjust_scan_count(request, item_id):
payload, error = _payload(request)
if error:
return JsonResponse({'error': error}, status=400)
try:
item = adjust_confirmed_count(request.user, item_id, int(payload.get('delta', 0)))
except (ScanWorkflowError, TypeError, ValueError) as error:
return JsonResponse({'error': str(error)}, status=400)
return JsonResponse({'ok': True, 'deleted': item is None,
'item': _item_json(item) if item else None})
@login_required
@require_POST
def submit_scan_batch(request, batch_id):
payload, error = _payload(request)
if error:
return JsonResponse({'error': error}, status=400)
finish = str(payload.get('finish', '')).lower() in {'true', '1', 'yes', 'on'}
try:
run = submit_batch(request.user, batch_id=batch_id,
next_location_id=payload.get('next_location_id'), finish=finish)
except (ScanWorkflowError, TypeError, ValueError) as error:
return JsonResponse({'error': str(error)}, status=400)
if finish:
return JsonResponse({'ok': True, 'redirect': reverse('food:inventory')})
next_batch = run.batches.get(status=InventoryScanBatch.Status.DRAFT)
return JsonResponse({'ok': True, 'batch_id': next_batch.pk,
'location_id': next_batch.location_id, 'location': next_batch.location.name,
'submit_url': reverse('food:inventory-scan-submit', args=[next_batch.pk])})
@login_required
@require_GET
def scan_queue(request, run_id):
run = get_object_or_404(InventoryScanRun, api_id=run_id, user=request.user)
batches = _queue_batches(run)
return render(request, 'food/partials/inventory_scan_queue.html', {'run': run, 'batches': batches})
def _queue_batches(run):
batches = list(run.batches.select_related('location').prefetch_related(Prefetch(
'items', queryset=InventoryScanItem.objects.exclude(status=InventoryScanItem.Status.ACTIVE)
.select_related('location', 'product'), to_attr='queue_items')).all())
return batches
def _payload(request):
if request.content_type == 'application/json':
try:
payload = json.loads(request.body or b'{}')
if not isinstance(payload, dict):
return {}, 'Invalid request body.'
return payload, None
except (json.JSONDecodeError, UnicodeDecodeError):
return {}, 'Invalid request body.'
return request.POST.dict(), None
def _item_json(item):
return {
'id': item.pk, 'barcode': item.barcode, 'status': item.status,
'product_name': item.product_name, 'brand': item.brand,
'package_count': item.package_count,
'package_amount': str(item.package_amount) if item.package_amount is not None else '',
'package_unit': item.package_unit, 'location_id': item.location_id,
'location': item.location.name, 'cached': bool(item.source_data.get('cached')),
}