Add persistent inventory scan job foundation

This commit is contained in:
Joren de Graaf
2026-10-26 01:00:17 +01:00
parent 5217c7f30c
commit a66ecd7c85
9 changed files with 1068 additions and 13 deletions
+124
View File
@@ -380,3 +380,127 @@ class ApiIdempotencyRecord(models.Model):
class Meta:
constraints = [models.UniqueConstraint(fields=('token', 'key'), name='food_unique_api_idempotency_key')]
class InventoryScanRun(models.Model):
"""A resumable scan-in session, including batches submitted while scanning continues."""
class Status(models.TextChoices):
ACTIVE = 'active', 'Active'
PROCESSING = 'processing', 'Processing'
COMPLETE = 'complete', 'Complete'
COMPLETE_WITH_REVIEW = 'complete_with_review', 'Complete with review'
CANCELLED = 'cancelled', 'Cancelled'
api_id = models.UUIDField(default=uuid.uuid4, unique=True, editable=False)
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='inventory_scan_runs')
status = models.CharField(max_length=24, choices=Status.choices, default=Status.ACTIVE, db_index=True)
selected_location = models.ForeignKey(InventoryLocation, on_delete=models.SET_NULL, null=True, blank=True,
related_name='active_scan_runs')
created_at = models.DateTimeField(auto_now_add=True)
submitted_at = models.DateTimeField(null=True, blank=True)
completed_at = models.DateTimeField(null=True, blank=True)
notification_attempted_at = models.DateTimeField(null=True, blank=True)
notification_sent_at = models.DateTimeField(null=True, blank=True)
notification_error = models.CharField(max_length=255, blank=True)
class Meta:
indexes = [models.Index(fields=('user', 'status', '-created_at'), name='food_scan_run_user_status')]
constraints = [models.UniqueConstraint(fields=('user',), condition=Q(status='active'),
name='food_one_active_scan_run')]
class InventoryScanBatch(models.Model):
"""A location-specific portion of a run. Save & next closes one and opens another."""
class Status(models.TextChoices):
DRAFT = 'draft', 'Draft'
QUEUED = 'queued', 'Queued'
COMPLETE = 'complete', 'Complete'
run = models.ForeignKey(InventoryScanRun, on_delete=models.CASCADE, related_name='batches')
location = models.ForeignKey(InventoryLocation, on_delete=models.PROTECT, related_name='scan_batches')
sequence = models.PositiveIntegerField()
status = models.CharField(max_length=12, choices=Status.choices, default=Status.DRAFT, db_index=True)
created_at = models.DateTimeField(auto_now_add=True)
submitted_at = models.DateTimeField(null=True, blank=True)
completed_at = models.DateTimeField(null=True, blank=True)
class Meta:
ordering = ('sequence',)
constraints = [
models.UniqueConstraint(fields=('run', 'sequence'), name='food_unique_scan_batch_sequence'),
models.UniqueConstraint(fields=('run',), condition=Q(status='draft'), name='food_one_draft_scan_batch'),
]
class InventoryScanItem(models.Model):
"""A persisted scan entry that becomes its own retryable enrichment job on submit."""
class Status(models.TextChoices):
ACTIVE = 'active', 'Active scan'
CONFIRMED = 'confirmed', 'Confirmed'
QUEUED = 'queued', 'Queued'
PROCESSING = 'processing', 'Processing'
NEEDS_REVIEW = 'needs_review', 'Needs review'
COMPLETE = 'complete', 'Complete'
DEFERRED = 'deferred', 'Deferred'
FAILED = 'failed', 'Failed'
batch = models.ForeignKey(InventoryScanBatch, on_delete=models.CASCADE, related_name='items')
location = models.ForeignKey(InventoryLocation, on_delete=models.PROTECT, related_name='scan_items')
barcode = models.CharField(max_length=64, db_index=True)
status = models.CharField(max_length=16, choices=Status.choices, default=Status.ACTIVE, db_index=True)
package_count = models.PositiveIntegerField(default=1, validators=[MinValueValidator(1)])
package_amount = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True, validators=POSITIVE)
package_unit = models.CharField(max_length=8, blank=True)
pieces_per_package = models.PositiveIntegerField(null=True, blank=True)
piece_amount = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True, validators=POSITIVE)
inner_packs_per_package = models.PositiveIntegerField(null=True, blank=True)
pieces_per_inner_pack = models.PositiveIntegerField(null=True, blank=True)
inner_pack_amount = models.DecimalField(max_digits=10, decimal_places=3, null=True, blank=True, validators=POSITIVE)
inner_pack_label = models.CharField(max_length=40, blank=True)
piece_label = models.CharField(max_length=40, blank=True)
product_name = models.CharField(max_length=255, blank=True)
brand = models.CharField(max_length=255, blank=True)
category_name = models.CharField(max_length=100, blank=True)
notes = models.TextField(blank=True)
best_before = models.DateField(null=True, blank=True)
opened = models.BooleanField(default=False)
source_data = models.JSONField(default=dict, blank=True)
user_data = models.JSONField(default=dict, blank=True)
product = models.ForeignKey(FoodProduct, on_delete=models.SET_NULL, null=True, blank=True,
related_name='scan_items')
inventory_item = models.ForeignKey(KitchenInventoryItem, on_delete=models.SET_NULL, null=True, blank=True,
related_name='scan_items')
attempts = models.PositiveSmallIntegerField(default=0)
next_attempt_at = models.DateTimeField(null=True, blank=True, db_index=True)
lease_until = models.DateTimeField(null=True, blank=True, db_index=True)
last_error = models.CharField(max_length=255, blank=True)
created_at = models.DateTimeField(auto_now_add=True)
confirmed_at = models.DateTimeField(null=True, blank=True)
completed_at = models.DateTimeField(null=True, blank=True)
class Meta:
ordering = ('created_at', 'pk')
indexes = [models.Index(fields=('status', 'next_attempt_at'), name='food_scan_item_due')]
class OpenFoodFactsRequest(models.Model):
"""A durable timestamp for each actual OFF HTTP request; shared by web and worker processes."""
requested_at = models.DateTimeField(db_index=True)
endpoint = models.CharField(max_length=24, blank=True)
class OpenFoodFactsRateLimit(models.Model):
"""A single database lock row coordinates request reservations across processes."""
id = models.PositiveSmallIntegerField(primary_key=True, default=1, editable=False)
updated_at = models.DateTimeField(auto_now=True)
class InventoryReviewPreference(models.Model):
class Mode(models.TextChoices):
INDIVIDUAL = 'individual', 'Individual'
BULK = 'bulk', 'Bulk'
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
related_name='inventory_review_preference')
mode = models.CharField(max_length=12, choices=Mode.choices, default=Mode.INDIVIDUAL)
updated_at = models.DateTimeField(auto_now=True)