694 lines
36 KiB
Python
694 lines
36 KiB
Python
"""Persistent scan-in session and enrichment queue services."""
|
|
from datetime import timedelta
|
|
from decimal import Decimal, InvalidOperation
|
|
import random
|
|
|
|
from django.core.exceptions import ValidationError
|
|
from django.db import IntegrityError, connections, transaction
|
|
from django.db.models import F, Q
|
|
from django.utils import timezone
|
|
from django.utils.dateparse import parse_date
|
|
|
|
from food.models import (FoodProduct, InventoryCategory, InventoryLocation,
|
|
InventoryReviewPreference, InventoryScanBatch,
|
|
InventoryScanItem, InventoryScanRun,
|
|
KitchenInventoryItem)
|
|
from food.services.openfoodfacts import (OpenFoodFactsError,
|
|
OpenFoodFactsTransientError,
|
|
cached_product)
|
|
|
|
|
|
class ScanWorkflowError(ValueError):
|
|
pass
|
|
|
|
|
|
def get_or_create_active_run(user, location_id):
|
|
location = InventoryLocation.objects.filter(user=user, pk=location_id).first()
|
|
if not location:
|
|
raise ScanWorkflowError('Choose one of your existing inventory locations.')
|
|
try:
|
|
with transaction.atomic():
|
|
run = (InventoryScanRun.objects.select_for_update().filter(
|
|
user=user, status=InventoryScanRun.Status.ACTIVE).order_by('-created_at').first())
|
|
if run:
|
|
draft = run.batches.filter(status=InventoryScanBatch.Status.DRAFT).first()
|
|
if not draft:
|
|
raise ScanWorkflowError('This scan session is being saved. Refresh and try again.')
|
|
run.selected_location = draft.location
|
|
return run
|
|
run = InventoryScanRun.objects.create(user=user, selected_location=location)
|
|
InventoryScanBatch.objects.create(run=run, location=location, sequence=1)
|
|
return run
|
|
except IntegrityError:
|
|
run = resume_active_run(user)
|
|
if run:
|
|
return run
|
|
raise
|
|
|
|
|
|
def resume_active_run(user):
|
|
return (InventoryScanRun.objects.filter(user=user, status=InventoryScanRun.Status.ACTIVE)
|
|
.prefetch_related('batches__items').order_by('-created_at').first())
|
|
|
|
|
|
def change_draft_batch_location(user, *, batch_id, location_id):
|
|
"""Change the location for a live draft batch and all of its unsubmitted scans."""
|
|
with transaction.atomic():
|
|
batch = (InventoryScanBatch.objects.select_for_update().select_related('run')
|
|
.filter(pk=batch_id, run__user=user).first())
|
|
if not batch or batch.status != InventoryScanBatch.Status.DRAFT:
|
|
raise ScanWorkflowError('Only the current unsaved batch can change location.')
|
|
run = InventoryScanRun.objects.select_for_update().get(pk=batch.run_id)
|
|
if run.status != InventoryScanRun.Status.ACTIVE:
|
|
raise ScanWorkflowError('This scan session is no longer active.')
|
|
location = InventoryLocation.objects.filter(user=user, pk=location_id).first()
|
|
if not location:
|
|
raise ScanWorkflowError('Choose one of your existing inventory locations.')
|
|
|
|
batch.location = location
|
|
batch.save(update_fields=('location',))
|
|
run.selected_location = location
|
|
run.save(update_fields=('selected_location',))
|
|
items = batch.items.select_for_update().filter(
|
|
status__in=(InventoryScanItem.Status.ACTIVE, InventoryScanItem.Status.CONFIRMED))
|
|
for item in items:
|
|
item.location = location
|
|
item.user_data = _item_data(item)
|
|
item.save(update_fields=('location', 'user_data'))
|
|
return batch
|
|
|
|
|
|
def create_active_scan(run, user, barcode):
|
|
if not barcode.isascii() or not barcode.isdigit() or not 6 <= len(barcode) <= 64:
|
|
raise ScanWorkflowError('Enter a valid numeric barcode.')
|
|
with transaction.atomic():
|
|
locked = InventoryScanRun.objects.select_for_update().filter(
|
|
pk=run.pk, user=user, status=InventoryScanRun.Status.ACTIVE).first()
|
|
if not locked:
|
|
raise ScanWorkflowError('This scan session is no longer active.')
|
|
batch = locked.batches.select_for_update().filter(status=InventoryScanBatch.Status.DRAFT).first()
|
|
if not batch:
|
|
raise ScanWorkflowError('Choose a location before scanning.')
|
|
if batch.items.filter(status=InventoryScanItem.Status.ACTIVE).exists():
|
|
raise ScanWorkflowError('Confirm or remove the active scan before scanning another item.')
|
|
repeated = batch.items.select_for_update().filter(
|
|
barcode=barcode, status=InventoryScanItem.Status.CONFIRMED).order_by('-confirmed_at', '-pk').first()
|
|
if repeated:
|
|
repeated.package_count += 1
|
|
repeated.user_data = _item_data(repeated)
|
|
repeated.save(update_fields=('package_count', 'user_data'))
|
|
return repeated
|
|
product = FoodProduct.objects.filter(owner=user, barcode=barcode).first()
|
|
values = {}
|
|
if product:
|
|
values = {
|
|
'product_name': product.product_name, 'brand': product.brand,
|
|
'package_amount': product.package_amount, 'package_unit': product.package_unit,
|
|
'source_data': {'cached': True, 'source': product.source,
|
|
'nutrition': _product_nutrients(product)},
|
|
}
|
|
return InventoryScanItem.objects.create(batch=batch, location=batch.location,
|
|
barcode=barcode, **values)
|
|
|
|
|
|
def confirm_scan_item(user, item_id, values):
|
|
with transaction.atomic():
|
|
item = (InventoryScanItem.objects.select_for_update().select_related('batch__run')
|
|
.filter(pk=item_id, batch__run__user=user).first())
|
|
if not item or item.status != InventoryScanItem.Status.ACTIVE or item.batch.status != InventoryScanBatch.Status.DRAFT:
|
|
raise ScanWorkflowError('This scan can no longer be edited.')
|
|
location = InventoryLocation.objects.filter(user=user, pk=values.get('location_id')).first()
|
|
if not location:
|
|
raise ScanWorkflowError('Choose one of your existing inventory locations.')
|
|
item.location = location
|
|
item.product_name = str(values.get('product_name', item.product_name)).strip()[:255]
|
|
item.brand = str(values.get('brand', item.brand)).strip()[:255]
|
|
item.category_name = str(values.get('category_name', '')).strip()[:100]
|
|
item.notes = str(values.get('notes', '')).strip()[:4000]
|
|
item.package_count = _integer(values.get('package_count', 1), 'Enter a valid number of packages.', minimum=1, maximum=10000)
|
|
item.package_amount = _decimal_or_none(values.get('package_amount'), 'Enter a valid package size.')
|
|
item.package_unit = str(values.get('package_unit', '')).strip()[:8]
|
|
item.pieces_per_package = _integer_or_none(values.get('pieces_per_package'), 'Enter a valid number of pieces.', maximum=100000)
|
|
item.piece_amount = _decimal_or_none(values.get('piece_amount'), 'Enter a valid amount per piece.')
|
|
item.inner_packs_per_package = _integer_or_none(values.get('inner_packs_per_package'), 'Enter a valid number of inner packs.', maximum=100000)
|
|
item.pieces_per_inner_pack = _integer_or_none(values.get('pieces_per_inner_pack'), 'Enter a valid number of pieces per inner pack.', maximum=100000)
|
|
item.inner_pack_amount = _decimal_or_none(values.get('inner_pack_amount'), 'Enter a valid inner pack amount.')
|
|
item.inner_pack_label = str(values.get('inner_pack_label', '')).strip()[:40]
|
|
item.piece_label = str(values.get('piece_label', '')).strip()[:40]
|
|
best_before = str(values.get('best_before', '')).strip()
|
|
item.best_before = parse_date(best_before) if best_before else None
|
|
if best_before and item.best_before is None:
|
|
raise ScanWorkflowError('Enter a valid best-before date.')
|
|
item.opened = bool(values.get('opened'))
|
|
item.user_data = _item_data(item)
|
|
item.status = InventoryScanItem.Status.CONFIRMED
|
|
item.confirmed_at = timezone.now()
|
|
item.save()
|
|
item.batch.run.selected_location = location
|
|
item.batch.run.save(update_fields=('selected_location',))
|
|
return item
|
|
|
|
|
|
def discard_active_scan(user, item_id):
|
|
deleted, _ = InventoryScanItem.objects.filter(pk=item_id, batch__run__user=user,
|
|
status=InventoryScanItem.Status.ACTIVE, batch__status=InventoryScanBatch.Status.DRAFT).delete()
|
|
if not deleted:
|
|
raise ScanWorkflowError('Only an unconfirmed active scan can be removed.')
|
|
|
|
|
|
def adjust_confirmed_count(user, item_id, delta):
|
|
if delta not in (-1, 1):
|
|
raise ScanWorkflowError('Invalid package adjustment.')
|
|
with transaction.atomic():
|
|
item = InventoryScanItem.objects.select_for_update().filter(pk=item_id, batch__run__user=user,
|
|
batch__status=InventoryScanBatch.Status.DRAFT, status=InventoryScanItem.Status.CONFIRMED).first()
|
|
if not item:
|
|
raise ScanWorkflowError('This queued item can no longer be changed.')
|
|
if item.package_count + delta < 1:
|
|
item.delete()
|
|
return None
|
|
item.package_count += delta
|
|
item.user_data = _item_data(item)
|
|
item.save(update_fields=('package_count', 'user_data'))
|
|
return item
|
|
|
|
|
|
def submit_batch(user, *, batch_id, next_location_id=None, finish=False):
|
|
run_id = None
|
|
with transaction.atomic():
|
|
batch = (InventoryScanBatch.objects.select_for_update().select_related('run')
|
|
.filter(pk=batch_id, run__user=user).first())
|
|
if not batch:
|
|
raise ScanWorkflowError('Scan batch not found.')
|
|
run = InventoryScanRun.objects.select_for_update().get(pk=batch.run_id)
|
|
if batch.status == InventoryScanBatch.Status.QUEUED:
|
|
return run
|
|
if run.status != InventoryScanRun.Status.ACTIVE or batch.status != InventoryScanBatch.Status.DRAFT:
|
|
raise ScanWorkflowError('This scan batch has already been submitted.')
|
|
if batch.items.filter(status=InventoryScanItem.Status.ACTIVE).exists():
|
|
raise ScanWorkflowError('Confirm or remove the active item before saving this batch.')
|
|
items = batch.items.filter(status=InventoryScanItem.Status.CONFIRMED)
|
|
has_items = items.exists()
|
|
prior_submissions = run.batches.exclude(pk=batch.pk).filter(
|
|
status__in=(InventoryScanBatch.Status.QUEUED, InventoryScanBatch.Status.COMPLETE)).exists()
|
|
if not has_items and not (finish and prior_submissions):
|
|
raise ScanWorkflowError('Confirm at least one item before saving this batch.')
|
|
now = timezone.now()
|
|
if has_items:
|
|
items.update(status=InventoryScanItem.Status.QUEUED, next_attempt_at=now)
|
|
batch.status = InventoryScanBatch.Status.QUEUED
|
|
batch.submitted_at = now
|
|
batch.save(update_fields=('status', 'submitted_at'))
|
|
else:
|
|
batch.status = InventoryScanBatch.Status.COMPLETE
|
|
batch.submitted_at = now
|
|
batch.completed_at = now
|
|
batch.save(update_fields=('status', 'submitted_at', 'completed_at'))
|
|
if finish:
|
|
run.status = InventoryScanRun.Status.PROCESSING
|
|
run.submitted_at = now
|
|
run.save(update_fields=('status', 'submitted_at'))
|
|
else:
|
|
location = InventoryLocation.objects.filter(user=user, pk=next_location_id).first()
|
|
if not location:
|
|
raise ScanWorkflowError('Choose an existing location for the next batch.')
|
|
run.selected_location = location
|
|
next_sequence = (run.batches.order_by('-sequence').values_list('sequence', flat=True).first() or 0) + 1
|
|
InventoryScanBatch.objects.create(run=run, location=location, sequence=next_sequence)
|
|
run.save(update_fields=('selected_location',))
|
|
run_id = run.pk
|
|
if finish:
|
|
_refresh_completion(run_id)
|
|
return InventoryScanRun.objects.get(pk=run_id)
|
|
|
|
|
|
def _claim_next_item():
|
|
now = timezone.now()
|
|
with transaction.atomic():
|
|
skip_locked = connections['default'].features.has_select_for_update_skip_locked
|
|
qs = InventoryScanItem.objects.select_for_update(skip_locked=skip_locked).filter(
|
|
Q(status=InventoryScanItem.Status.QUEUED, next_attempt_at__lte=now) |
|
|
Q(status=InventoryScanItem.Status.PROCESSING, lease_until__lt=now),
|
|
batch__status=InventoryScanBatch.Status.QUEUED,
|
|
batch__run__status__in=(InventoryScanRun.Status.ACTIVE, InventoryScanRun.Status.PROCESSING))
|
|
item = qs.select_related('batch__run__user').order_by('next_attempt_at', 'created_at', 'pk').first()
|
|
if not item:
|
|
return None
|
|
item.status = InventoryScanItem.Status.PROCESSING
|
|
item.lease_until = now + timedelta(minutes=15)
|
|
item.last_error = ''
|
|
item.save(update_fields=('status', 'lease_until', 'last_error'))
|
|
return item.pk
|
|
|
|
|
|
def process_next_scan_item():
|
|
item_id = _claim_next_item()
|
|
if not item_id:
|
|
return False
|
|
item = InventoryScanItem.objects.select_related('batch__run__user', 'location').get(pk=item_id)
|
|
product = FoodProduct.objects.filter(owner=item.batch.run.user, barcode=item.barcode).first()
|
|
try:
|
|
if not product:
|
|
def record_http_attempt():
|
|
InventoryScanItem.objects.filter(pk=item_id, status=InventoryScanItem.Status.PROCESSING).update(
|
|
attempts=F('attempts') + 1)
|
|
product = cached_product(item.batch.run.user, item.barcode, wait_for_slot=True,
|
|
on_request_start=record_http_attempt)
|
|
except OpenFoodFactsTransientError as error:
|
|
_retry_or_review(item_id, str(error), retry_after=error.retry_after)
|
|
_refresh_completion(item.batch.run_id)
|
|
return True
|
|
except OpenFoodFactsError as error:
|
|
_mark_needs_review(item_id, str(error))
|
|
_refresh_completion(item.batch.run_id)
|
|
return True
|
|
if not product:
|
|
_mark_needs_review(item_id, 'No product was found for this barcode. Enter product details to approve it.')
|
|
_refresh_completion(item.batch.run_id)
|
|
return True
|
|
with transaction.atomic():
|
|
current = InventoryScanItem.objects.select_for_update().select_related('batch__run').get(pk=item_id)
|
|
if current.status != InventoryScanItem.Status.PROCESSING:
|
|
return True
|
|
_populate_from_product(current, product)
|
|
current.product = product
|
|
if not current.product_name.strip() or not current.location_id or not _valid_stock_input(current):
|
|
current.status = InventoryScanItem.Status.NEEDS_REVIEW
|
|
current.last_error = 'Product identity or package details need review.'
|
|
current.completed_at = timezone.now()
|
|
current.lease_until = None
|
|
current.save()
|
|
else:
|
|
try:
|
|
stock = _create_inventory_stock(current, product)
|
|
except (ValidationError, ScanWorkflowError) as error:
|
|
current.status = InventoryScanItem.Status.NEEDS_REVIEW
|
|
current.last_error = str(error)[:255]
|
|
current.completed_at = timezone.now()
|
|
current.lease_until = None
|
|
current.save()
|
|
else:
|
|
current.status = InventoryScanItem.Status.COMPLETE
|
|
current.inventory_item = stock
|
|
current.completed_at = timezone.now()
|
|
current.lease_until = None
|
|
current.last_error = ''
|
|
current.save()
|
|
_refresh_completion(item.batch.run_id)
|
|
return True
|
|
|
|
|
|
def _retry_or_review(item_id, message, *, retry_after=None):
|
|
with transaction.atomic():
|
|
item = InventoryScanItem.objects.select_for_update().get(pk=item_id)
|
|
item.lease_until = None
|
|
item.last_error = message[:255]
|
|
item.refresh_from_db(fields=('attempts',))
|
|
if item.attempts >= 10:
|
|
item.status = InventoryScanItem.Status.NEEDS_REVIEW
|
|
item.completed_at = timezone.now()
|
|
else:
|
|
backoff = min(900, 5 * (2 ** max(0, item.attempts - 1)))
|
|
delay = max(backoff + random.uniform(0, backoff * 0.25), retry_after or 0)
|
|
item.status = InventoryScanItem.Status.QUEUED
|
|
item.next_attempt_at = timezone.now() + timedelta(seconds=delay)
|
|
item.save(update_fields=('status', 'lease_until', 'last_error', 'completed_at', 'next_attempt_at'))
|
|
|
|
|
|
def _mark_needs_review(item_id, message):
|
|
InventoryScanItem.objects.filter(pk=item_id, status=InventoryScanItem.Status.PROCESSING).update(
|
|
status=InventoryScanItem.Status.NEEDS_REVIEW, last_error=message[:255], completed_at=timezone.now(), lease_until=None)
|
|
|
|
|
|
def _populate_from_product(item, product):
|
|
if not item.product_name:
|
|
item.product_name = product.product_name
|
|
if not item.brand:
|
|
item.brand = product.brand
|
|
if item.package_amount is None and product.package_amount is not None:
|
|
item.package_amount = product.package_amount
|
|
item.package_unit = product.package_unit
|
|
item.source_data = {
|
|
'source': product.source,
|
|
'fetched_at': product.fetched_at.isoformat() if product.fetched_at else None,
|
|
'product_name': product.product_name,
|
|
'brand': product.brand,
|
|
'package_quantity': product.package_quantity,
|
|
'image_url': product.image_url,
|
|
'nutrition': _product_nutrients(product),
|
|
'raw_data': product.raw_data if isinstance(product.raw_data, dict) else {},
|
|
}
|
|
item.user_data = _item_data(item)
|
|
|
|
|
|
def _create_inventory_stock(item, product):
|
|
package_amount = item.package_amount
|
|
package_unit = item.package_unit
|
|
pieces = item.pieces_per_package
|
|
if pieces and package_amount is None and not package_unit:
|
|
package_amount, package_unit = Decimal(pieces), 'pieces'
|
|
if pieces and item.inner_packs_per_package and item.pieces_per_inner_pack:
|
|
if package_amount is None and item.inner_pack_amount is not None:
|
|
package_amount = item.inner_pack_amount * item.inner_packs_per_package
|
|
if package_unit and package_unit not in {'mg', 'g', 'kg', 'ml', 'L', 'pieces', 'packs', 'portions'}:
|
|
raise ScanWorkflowError('Choose a supported package unit.')
|
|
category = None
|
|
if item.category_name:
|
|
category, _ = InventoryCategory.objects.get_or_create(user=item.batch.run.user,
|
|
name__iexact=item.category_name, defaults={'name': item.category_name})
|
|
base = KitchenInventoryItem(
|
|
user=item.batch.run.user, product=product, location=item.location,
|
|
category=category, quantity=item.package_count if package_amount is None else 1,
|
|
notes=item.notes, opened=item.opened, best_before=item.best_before,
|
|
original_amount=package_amount, remaining_amount=package_amount,
|
|
inventory_unit=package_unit, pieces_per_package=pieces,
|
|
remaining_pieces=pieces, piece_amount=item.piece_amount,
|
|
inner_packs_per_package=item.inner_packs_per_package,
|
|
pieces_per_inner_pack=item.pieces_per_inner_pack,
|
|
inner_pack_amount=item.inner_pack_amount, inner_pack_label=item.inner_pack_label,
|
|
piece_label=item.piece_label,
|
|
)
|
|
base.full_clean()
|
|
base.save()
|
|
if package_amount is None:
|
|
return base
|
|
for _ in range(item.package_count - 1):
|
|
package = KitchenInventoryItem(
|
|
user=base.user, product=base.product, location=base.location,
|
|
category=base.category, quantity=1, notes=base.notes, opened=base.opened,
|
|
best_before=base.best_before, original_amount=base.original_amount,
|
|
remaining_amount=base.original_amount, inventory_unit=base.inventory_unit,
|
|
pieces_per_package=base.pieces_per_package, remaining_pieces=base.pieces_per_package,
|
|
piece_amount=base.piece_amount, inner_packs_per_package=base.inner_packs_per_package,
|
|
pieces_per_inner_pack=base.pieces_per_inner_pack, inner_pack_amount=base.inner_pack_amount,
|
|
inner_pack_label=base.inner_pack_label, piece_label=base.piece_label,
|
|
)
|
|
package.full_clean()
|
|
package.save()
|
|
return base
|
|
|
|
|
|
def _valid_stock_input(item):
|
|
if item.package_amount is None and not item.pieces_per_package:
|
|
return True # Count-only packages have unknown size by design.
|
|
if item.package_amount is not None and item.package_unit:
|
|
return True
|
|
return bool(item.inner_packs_per_package and item.pieces_per_inner_pack and
|
|
item.inner_pack_amount and item.package_unit)
|
|
|
|
|
|
def _refresh_completion(run_id):
|
|
with transaction.atomic():
|
|
run = InventoryScanRun.objects.select_for_update().get(pk=run_id)
|
|
for batch in run.batches.filter(status=InventoryScanBatch.Status.QUEUED):
|
|
if not batch.items.filter(status__in=(InventoryScanItem.Status.QUEUED, InventoryScanItem.Status.PROCESSING)).exists():
|
|
batch.status = InventoryScanBatch.Status.COMPLETE
|
|
batch.completed_at = timezone.now()
|
|
batch.save(update_fields=('status', 'completed_at'))
|
|
if run.status == InventoryScanRun.Status.PROCESSING and not run.batches.filter(
|
|
status=InventoryScanBatch.Status.QUEUED).exists():
|
|
review = run.batches.filter(items__status__in=(InventoryScanItem.Status.NEEDS_REVIEW,
|
|
InventoryScanItem.Status.DEFERRED,
|
|
InventoryScanItem.Status.FAILED)).exists()
|
|
run.status = (InventoryScanRun.Status.COMPLETE_WITH_REVIEW if review
|
|
else InventoryScanRun.Status.COMPLETE)
|
|
run.completed_at = timezone.now()
|
|
run.save(update_fields=('status', 'completed_at'))
|
|
if run.status in (InventoryScanRun.Status.COMPLETE, InventoryScanRun.Status.COMPLETE_WITH_REVIEW):
|
|
_send_completion_notification(run_id)
|
|
|
|
|
|
def _send_completion_notification(run_id):
|
|
from planner.services.notifications import NotificationError, send_notification
|
|
|
|
now = timezone.now()
|
|
with transaction.atomic():
|
|
run = InventoryScanRun.objects.select_for_update().select_related('user').get(pk=run_id)
|
|
if run.notification_sent_at or run.status not in (
|
|
InventoryScanRun.Status.COMPLETE, InventoryScanRun.Status.COMPLETE_WITH_REVIEW):
|
|
return
|
|
retry_window = timedelta(seconds=60)
|
|
if run.notification_attempted_at and run.notification_attempted_at > now - retry_window:
|
|
return
|
|
from django.db.models import Count
|
|
tallies = dict(run.batches.values('items__status').annotate(total=Count('items')).values_list('items__status', 'total'))
|
|
succeeded = tallies.get(InventoryScanItem.Status.COMPLETE, 0)
|
|
review = sum(tallies.get(status, 0) for status in (InventoryScanItem.Status.NEEDS_REVIEW,
|
|
InventoryScanItem.Status.DEFERRED))
|
|
failed = tallies.get(InventoryScanItem.Status.FAILED, 0)
|
|
run.notification_attempted_at = now
|
|
run.notification_error = ''
|
|
run.save(update_fields=('notification_attempted_at', 'notification_error'))
|
|
user = run.user
|
|
url = '/food/inventory/#inventory-review'
|
|
message = f'Bulk scan finished: {succeeded} added, {review} need review, {failed} failed. {url}'
|
|
try:
|
|
sent = send_notification(user, 'Kitchen inventory scan complete', message,
|
|
sequence_id=f'inventory-scan-{run.api_id}')
|
|
except NotificationError as error:
|
|
InventoryScanRun.objects.filter(pk=run_id).update(notification_error=str(error)[:255])
|
|
return
|
|
if sent:
|
|
InventoryScanRun.objects.filter(pk=run_id).update(notification_sent_at=timezone.now(), notification_error='')
|
|
else:
|
|
InventoryScanRun.objects.filter(pk=run_id).update(notification_error='ntfy notifications are not enabled')
|
|
|
|
|
|
def dispatch_pending_scan_notifications(limit=50):
|
|
"""Retry completed-run notifications after a short persisted delivery cooldown."""
|
|
cutoff = timezone.now() - timedelta(seconds=60)
|
|
run_ids = InventoryScanRun.objects.filter(
|
|
status__in=(InventoryScanRun.Status.COMPLETE, InventoryScanRun.Status.COMPLETE_WITH_REVIEW),
|
|
notification_sent_at__isnull=True).exclude(notification_error='ntfy notifications are not enabled').filter(
|
|
Q(notification_attempted_at__isnull=True) | Q(notification_attempted_at__lte=cutoff)
|
|
).order_by('completed_at').values_list('pk', flat=True)[:limit]
|
|
for run_id in run_ids:
|
|
_send_completion_notification(run_id)
|
|
|
|
|
|
def update_review_preference(user, mode):
|
|
if mode not in InventoryReviewPreference.Mode.values:
|
|
raise ScanWorkflowError('Choose Individual or Bulk review.')
|
|
preference, _ = InventoryReviewPreference.objects.update_or_create(user=user, defaults={'mode': mode})
|
|
return preference
|
|
|
|
|
|
def approve_scan_item(user, item_id, values):
|
|
with transaction.atomic():
|
|
item = (InventoryScanItem.objects.select_for_update().select_related('batch__run__user')
|
|
.filter(pk=item_id, batch__run__user=user,
|
|
status__in=(InventoryScanItem.Status.NEEDS_REVIEW, InventoryScanItem.Status.DEFERRED,
|
|
InventoryScanItem.Status.FAILED)).first())
|
|
if not item:
|
|
raise ScanWorkflowError('This staged item is no longer awaiting review.')
|
|
if values.get('product_name'):
|
|
item.product_name = str(values['product_name']).strip()[:255]
|
|
if values.get('brand') is not None:
|
|
item.brand = str(values['brand']).strip()[:255]
|
|
if values.get('category_name') is not None:
|
|
item.category_name = str(values['category_name']).strip()[:100]
|
|
if values.get('notes') is not None:
|
|
item.notes = str(values['notes']).strip()[:4000]
|
|
if values.get('location_id'):
|
|
location = InventoryLocation.objects.filter(user=user, pk=values['location_id']).first()
|
|
if not location:
|
|
raise ScanWorkflowError('Choose one of your existing inventory locations.')
|
|
item.location = location
|
|
item.package_count = _integer(values.get('package_count', item.package_count), 'Enter a valid number of packages.', 1, 10000)
|
|
if 'package_amount' in values:
|
|
item.package_amount = _decimal_or_none(values['package_amount'], 'Enter a valid package size.')
|
|
if 'package_unit' in values:
|
|
item.package_unit = str(values['package_unit']).strip()[:8]
|
|
_apply_nutrition_values(item, values)
|
|
if not item.product_name:
|
|
raise ScanWorkflowError('Enter a product name before approving.')
|
|
product = item.product or FoodProduct.objects.filter(owner=user, barcode=item.barcode).first()
|
|
if not product:
|
|
product = FoodProduct.objects.create(owner=user, barcode=item.barcode,
|
|
product_name=item.product_name, brand=item.brand,
|
|
package_quantity=(f'{item.package_amount} {item.package_unit}' if item.package_amount else ''),
|
|
package_amount=item.package_amount, package_unit=item.package_unit,
|
|
source=FoodProduct.Source.MANUAL, raw_data={'scan_item_id': item.pk, 'manual_nutrition_override': True})
|
|
elif product.source == FoodProduct.Source.MANUAL:
|
|
product.product_name = item.product_name
|
|
product.brand = item.brand
|
|
product.save(update_fields=('product_name', 'brand', 'updated_at'))
|
|
else:
|
|
_apply_scan_product_corrections(product, item)
|
|
_populate_from_product(item, product)
|
|
_apply_nutrition_values(item, values, product=product)
|
|
item.product = product
|
|
if not _valid_stock_input(item):
|
|
raise ScanWorkflowError('Add valid package details or choose count-only stock before approving.')
|
|
stock = _create_inventory_stock(item, product)
|
|
item.status = InventoryScanItem.Status.COMPLETE
|
|
item.inventory_item = stock
|
|
item.last_error = ''
|
|
item.completed_at = timezone.now()
|
|
item.save()
|
|
_refresh_completion(item.batch.run_id)
|
|
return item
|
|
|
|
|
|
def save_review_item(user, item_id, values):
|
|
"""Persist edits on a staged row so switching review modes cannot lose them."""
|
|
with transaction.atomic():
|
|
item = (InventoryScanItem.objects.select_for_update().filter(
|
|
pk=item_id, batch__run__user=user,
|
|
status__in=(InventoryScanItem.Status.NEEDS_REVIEW, InventoryScanItem.Status.DEFERRED,
|
|
InventoryScanItem.Status.FAILED)).first())
|
|
if not item:
|
|
raise ScanWorkflowError('This staged item is no longer awaiting review.')
|
|
if values.get('product_name') is not None:
|
|
item.product_name = str(values['product_name']).strip()[:255]
|
|
if values.get('brand') is not None:
|
|
item.brand = str(values['brand']).strip()[:255]
|
|
if values.get('category_name') is not None:
|
|
item.category_name = str(values['category_name']).strip()[:100]
|
|
if values.get('notes') is not None:
|
|
item.notes = str(values['notes']).strip()[:4000]
|
|
if values.get('location_id'):
|
|
location = InventoryLocation.objects.filter(user=user, pk=values['location_id']).first()
|
|
if not location:
|
|
raise ScanWorkflowError('Choose one of your existing inventory locations.')
|
|
item.location = location
|
|
item.package_count = _integer(values.get('package_count', item.package_count), 'Enter a valid number of packages.', 1, 10000)
|
|
if 'package_amount' in values:
|
|
item.package_amount = _decimal_or_none(values['package_amount'], 'Enter a valid package size.')
|
|
if 'package_unit' in values:
|
|
item.package_unit = str(values['package_unit']).strip()[:8]
|
|
_apply_nutrition_values(item, values)
|
|
item.user_data = _item_data(item)
|
|
item.save()
|
|
return item
|
|
|
|
|
|
def approve_scan_items(user, item_ids, values):
|
|
"""Approve a selected group atomically after a preview has been shown."""
|
|
ids = list(dict.fromkeys(int(value) for value in item_ids))
|
|
if not ids:
|
|
raise ScanWorkflowError('Select at least one product to approve.')
|
|
with transaction.atomic():
|
|
items = list(InventoryScanItem.objects.select_for_update().filter(
|
|
pk__in=ids, batch__run__user=user,
|
|
status__in=(InventoryScanItem.Status.NEEDS_REVIEW, InventoryScanItem.Status.DEFERRED)))
|
|
if len(items) != len(ids):
|
|
raise ScanWorkflowError('One or more selected products are no longer awaiting review. Reload and try again.')
|
|
approved = []
|
|
for item in items:
|
|
shared = dict(values)
|
|
shared.setdefault('product_name', item.product_name)
|
|
shared.setdefault('brand', item.brand)
|
|
shared.setdefault('package_count', item.package_count)
|
|
shared.setdefault('package_amount', str(item.package_amount) if item.package_amount is not None else '')
|
|
shared.setdefault('package_unit', item.package_unit)
|
|
approved.append(approve_scan_item(user, item.pk, shared))
|
|
for run_id in set(item.batch.run_id for item in approved):
|
|
_refresh_completion(run_id)
|
|
return approved
|
|
|
|
|
|
def _apply_nutrition_values(item, values, product=None):
|
|
fields = {'energy_kcal_100': 'energy_kcal_100', 'fat_100': 'fat_100',
|
|
'saturated_fat_100': 'saturated_fat_100', 'carbohydrates_100': 'carbohydrates_100',
|
|
'sugars_100': 'sugars_100', 'protein_100': 'protein_100',
|
|
'salt_100': 'salt_100', 'fibre_100': 'fibre_100'}
|
|
if not any(key in values for key in fields):
|
|
return
|
|
product = product or item.product or FoodProduct.objects.filter(owner=item.batch.run.user, barcode=item.barcode).first()
|
|
nutrition = {}
|
|
for key, field in fields.items():
|
|
if key not in values:
|
|
continue
|
|
value = values[key]
|
|
parsed = _decimal_nonnegative_or_none(value, f'Enter a valid {key.replace("_100", "").replace("_", " ")} value.')
|
|
nutrition[key] = str(parsed) if parsed is not None else None
|
|
source_data = item.source_data if isinstance(item.source_data, dict) else {}
|
|
item.source_data = {**source_data,
|
|
'nutrition': {**source_data.get('nutrition', {}), **nutrition}}
|
|
if product:
|
|
for key, value in nutrition.items():
|
|
setattr(product, key, Decimal(value) if value is not None else None)
|
|
raw_data = product.raw_data if isinstance(product.raw_data, dict) else {}
|
|
product.raw_data = {**raw_data, 'manual_nutrition_override': True}
|
|
product.save(update_fields=(*nutrition.keys(), 'raw_data', 'updated_at'))
|
|
source_nutrition = item.source_data.get('nutrition', {}) if isinstance(item.source_data, dict) else {}
|
|
item.source_data = {**(item.source_data if isinstance(item.source_data, dict) else {}),
|
|
'nutrition': {**source_nutrition, **nutrition}}
|
|
|
|
|
|
def _decimal_nonnegative_or_none(value, message):
|
|
if value in (None, ''):
|
|
return None
|
|
try:
|
|
result = Decimal(str(value))
|
|
except (InvalidOperation, TypeError, ValueError):
|
|
raise ScanWorkflowError(message) from None
|
|
if not result.is_finite() or result < 0 or result > Decimal('9999999.999'):
|
|
raise ScanWorkflowError(message)
|
|
return result.quantize(Decimal('.001'))
|
|
|
|
|
|
def defer_scan_item(user, item_id):
|
|
updated = InventoryScanItem.objects.filter(pk=item_id, batch__run__user=user,
|
|
status__in=(InventoryScanItem.Status.NEEDS_REVIEW, InventoryScanItem.Status.FAILED,
|
|
InventoryScanItem.Status.DEFERRED)).update(
|
|
status=InventoryScanItem.Status.DEFERRED)
|
|
if not updated:
|
|
raise ScanWorkflowError('This item can no longer be deferred.')
|
|
|
|
|
|
def _apply_scan_product_corrections(product, item):
|
|
# This path updates identity and package metadata only; nutrition overrides remain untouched.
|
|
product.product_name = item.product_name or product.product_name
|
|
product.brand = item.brand or product.brand
|
|
product.package_amount = item.package_amount or product.package_amount
|
|
product.package_unit = item.package_unit or product.package_unit
|
|
product.save(update_fields=('product_name', 'brand', 'package_amount', 'package_unit', 'updated_at'))
|
|
|
|
|
|
def _product_nutrients(product):
|
|
return {field: str(getattr(product, field)) if getattr(product, field) is not None else None
|
|
for field in ('energy_kcal_100', 'fat_100', 'saturated_fat_100', 'carbohydrates_100',
|
|
'sugars_100', 'protein_100', 'salt_100', 'fibre_100')}
|
|
|
|
|
|
def _item_data(item):
|
|
return {'product_name': item.product_name, 'brand': item.brand, 'category_name': item.category_name,
|
|
'notes': item.notes, 'location_id': item.location_id, 'package_count': item.package_count,
|
|
'package_amount': str(item.package_amount) if item.package_amount is not None else None,
|
|
'package_unit': item.package_unit, 'pieces_per_package': item.pieces_per_package,
|
|
'piece_amount': str(item.piece_amount) if item.piece_amount is not None else None,
|
|
'inner_packs_per_package': item.inner_packs_per_package,
|
|
'pieces_per_inner_pack': item.pieces_per_inner_pack,
|
|
'inner_pack_amount': str(item.inner_pack_amount) if item.inner_pack_amount is not None else None,
|
|
'best_before': item.best_before.isoformat() if item.best_before else None, 'opened': item.opened}
|
|
|
|
|
|
def _integer(value, message, minimum=0, maximum=100000):
|
|
try:
|
|
result = int(value)
|
|
except (TypeError, ValueError):
|
|
raise ScanWorkflowError(message) from None
|
|
if str(result) != str(value).strip() and not isinstance(value, int):
|
|
raise ScanWorkflowError(message)
|
|
if not minimum <= result <= maximum:
|
|
raise ScanWorkflowError(message)
|
|
return result
|
|
|
|
|
|
def _integer_or_none(value, message, maximum=100000):
|
|
return None if value in (None, '') else _integer(value, message, 1, maximum)
|
|
|
|
|
|
def _decimal_or_none(value, message):
|
|
if value in (None, ''):
|
|
return None
|
|
try:
|
|
result = Decimal(str(value))
|
|
except (InvalidOperation, TypeError, ValueError):
|
|
raise ScanWorkflowError(message) from None
|
|
if not result.is_finite() or result <= 0 or result > Decimal('9999999.999'):
|
|
raise ScanWorkflowError(message)
|
|
return result.quantize(Decimal('.001'))
|