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
@@ -0,0 +1,127 @@
# Generated by Django 6.1.1 on 2026-10-25 23:32
import django.core.validators
import django.db.models.deletion
import uuid
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('food', '0014_alter_inventoryconsumption_unit'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='OpenFoodFactsRequest',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('requested_at', models.DateTimeField(db_index=True)),
('endpoint', models.CharField(blank=True, max_length=24)),
],
),
migrations.CreateModel(
name='OpenFoodFactsRateLimit',
fields=[
('id', models.PositiveSmallIntegerField(default=1, editable=False, primary_key=True, serialize=False)),
('updated_at', models.DateTimeField(auto_now=True)),
],
),
migrations.CreateModel(
name='InventoryReviewPreference',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('mode', models.CharField(choices=[('individual', 'Individual'), ('bulk', 'Bulk')], default='individual', max_length=12)),
('updated_at', models.DateTimeField(auto_now=True)),
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, related_name='inventory_review_preference', to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='InventoryScanRun',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('api_id', models.UUIDField(default=uuid.uuid4, editable=False, unique=True)),
('status', models.CharField(choices=[('active', 'Active'), ('processing', 'Processing'), ('complete', 'Complete'), ('complete_with_review', 'Complete with review'), ('cancelled', 'Cancelled')], db_index=True, default='active', max_length=24)),
('created_at', models.DateTimeField(auto_now_add=True)),
('submitted_at', models.DateTimeField(blank=True, null=True)),
('completed_at', models.DateTimeField(blank=True, null=True)),
('notification_attempted_at', models.DateTimeField(blank=True, null=True)),
('notification_sent_at', models.DateTimeField(blank=True, null=True)),
('notification_error', models.CharField(blank=True, max_length=255)),
('selected_location', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='active_scan_runs', to='food.inventorylocation')),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='inventory_scan_runs', to=settings.AUTH_USER_MODEL)),
],
),
migrations.CreateModel(
name='InventoryScanBatch',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('sequence', models.PositiveIntegerField()),
('status', models.CharField(choices=[('draft', 'Draft'), ('queued', 'Queued'), ('complete', 'Complete')], db_index=True, default='draft', max_length=12)),
('created_at', models.DateTimeField(auto_now_add=True)),
('submitted_at', models.DateTimeField(blank=True, null=True)),
('completed_at', models.DateTimeField(blank=True, null=True)),
('location', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='scan_batches', to='food.inventorylocation')),
('run', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='batches', to='food.inventoryscanrun')),
],
options={
'ordering': ('sequence',),
},
),
migrations.CreateModel(
name='InventoryScanItem',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('barcode', models.CharField(db_index=True, max_length=64)),
('status', models.CharField(choices=[('active', 'Active scan'), ('confirmed', 'Confirmed'), ('queued', 'Queued'), ('processing', 'Processing'), ('needs_review', 'Needs review'), ('complete', 'Complete'), ('deferred', 'Deferred'), ('failed', 'Failed')], db_index=True, default='active', max_length=16)),
('package_count', models.PositiveIntegerField(default=1, validators=[django.core.validators.MinValueValidator(1)])),
('package_amount', models.DecimalField(blank=True, decimal_places=3, max_digits=10, null=True, validators=[django.core.validators.MinValueValidator(0.001)])),
('package_unit', models.CharField(blank=True, max_length=8)),
('pieces_per_package', models.PositiveIntegerField(blank=True, null=True)),
('piece_amount', models.DecimalField(blank=True, decimal_places=3, max_digits=10, null=True, validators=[django.core.validators.MinValueValidator(0.001)])),
('inner_packs_per_package', models.PositiveIntegerField(blank=True, null=True)),
('pieces_per_inner_pack', models.PositiveIntegerField(blank=True, null=True)),
('inner_pack_amount', models.DecimalField(blank=True, decimal_places=3, max_digits=10, null=True, validators=[django.core.validators.MinValueValidator(0.001)])),
('inner_pack_label', models.CharField(blank=True, max_length=40)),
('piece_label', models.CharField(blank=True, max_length=40)),
('product_name', models.CharField(blank=True, max_length=255)),
('brand', models.CharField(blank=True, max_length=255)),
('category_name', models.CharField(blank=True, max_length=100)),
('notes', models.TextField(blank=True)),
('best_before', models.DateField(blank=True, null=True)),
('opened', models.BooleanField(default=False)),
('source_data', models.JSONField(blank=True, default=dict)),
('user_data', models.JSONField(blank=True, default=dict)),
('attempts', models.PositiveSmallIntegerField(default=0)),
('next_attempt_at', models.DateTimeField(blank=True, db_index=True, null=True)),
('lease_until', models.DateTimeField(blank=True, db_index=True, null=True)),
('last_error', models.CharField(blank=True, max_length=255)),
('created_at', models.DateTimeField(auto_now_add=True)),
('confirmed_at', models.DateTimeField(blank=True, null=True)),
('completed_at', models.DateTimeField(blank=True, null=True)),
('batch', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='items', to='food.inventoryscanbatch')),
('location', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='scan_items', to='food.inventorylocation')),
('inventory_item', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='scan_items', to='food.kitcheninventoryitem')),
('product', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='scan_items', to='food.foodproduct')),
],
options={
'ordering': ('created_at', 'pk'),
'indexes': [models.Index(fields=['status', 'next_attempt_at'], name='food_scan_item_due')],
},
),
migrations.AddIndex(
model_name='inventoryscanrun',
index=models.Index(fields=['user', 'status', '-created_at'], name='food_scan_run_user_status'),
),
migrations.AddConstraint(
model_name='inventoryscanbatch',
constraint=models.UniqueConstraint(fields=('run', 'sequence'), name='food_unique_scan_batch_sequence'),
),
migrations.AddConstraint(
model_name='inventoryscanbatch',
constraint=models.UniqueConstraint(condition=models.Q(('status', 'draft')), fields=('run',), name='food_one_draft_scan_batch'),
),
]
@@ -0,0 +1,19 @@
# Generated by Django 6.1.1 on 2026-10-25 23:36
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('food', '0015_openfoodfactsrequest_inventoryreviewpreference_and_more'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.AddConstraint(
model_name='inventoryscanrun',
constraint=models.UniqueConstraint(condition=models.Q(('status', 'active')), fields=('user',), name='food_one_active_scan_run'),
),
]