Allow changing active scan batch location

This commit is contained in:
Joren de Graaf
2026-10-26 15:48:18 +01:00
parent 244724abd5
commit 7d9beb915f
7 changed files with 174 additions and 12 deletions
+54 -1
View File
@@ -11,7 +11,8 @@ from food.models import (FoodProduct, InventoryLocation, InventoryScanBatch,
InventoryReviewPreference, InventoryScanItem, InventoryScanRun,
KitchenInventoryItem, OpenFoodFactsRequest)
from food.services.inventory_scanner import (ScanWorkflowError,
adjust_confirmed_count, approve_scan_item, approve_scan_items, confirm_scan_item, create_active_scan, get_or_create_active_run,
adjust_confirmed_count, approve_scan_item, approve_scan_items, change_draft_batch_location,
confirm_scan_item, create_active_scan, get_or_create_active_run,
dispatch_pending_scan_notifications, process_next_scan_item, save_review_item,
submit_batch, update_review_preference)
from food.services.off_rate_limit import OpenFoodFactsRateLimited, off_http_request
@@ -49,6 +50,33 @@ class InventoryScanQueueTests(TestCase):
item.refresh_from_db()
self.assertEqual(item.status, InventoryScanItem.Status.QUEUED)
def test_current_batch_location_change_moves_unsaved_items_but_not_saved_batches(self):
run, saved_item = self._confirmed_item()
current_run = submit_batch(self.user, batch_id=saved_item.batch_id,
next_location_id=self.shelf.pk)
current_batch = current_run.batches.get(status=InventoryScanBatch.Status.DRAFT)
pantry = InventoryLocation.objects.create(user=self.user, name='Pantry')
active_item = create_active_scan(current_run, self.user, '87654321')
confirm_scan_item(self.user, active_item.pk, {
'location_id': self.shelf.pk, 'product_name': 'Rice',
})
changed = change_draft_batch_location(
self.user, batch_id=current_batch.pk, location_id=pantry.pk)
saved_item.refresh_from_db()
active_item.refresh_from_db()
current_batch.refresh_from_db()
current_run.refresh_from_db()
self.assertEqual((saved_item.location, saved_item.batch.location), (self.fridge, self.fridge))
self.assertEqual((changed.location, current_batch.location), (pantry, pantry))
self.assertEqual(active_item.location, pantry)
self.assertEqual(active_item.user_data['location_id'], pantry.pk)
self.assertEqual(current_run.selected_location, pantry)
with self.assertRaises(ScanWorkflowError):
change_draft_batch_location(self.user, batch_id=saved_item.batch_id,
location_id=pantry.pk)
def test_batch_rejects_other_users_location(self):
run, item = self._confirmed_item()
foreign = InventoryLocation.objects.create(user=self.other, name='Other')
@@ -237,6 +265,7 @@ class InventoryScanWorkflowViewTests(TestCase):
def test_scanner_page_starts_and_resumes_a_persisted_session(self):
response = self.client.get(reverse('food:scanner-inventory'))
self.assertContains(response, 'Choose a starting location')
self.assertContains(response, '<option value="" selected disabled>Choose a location</option>', html=True)
response = self.client.post(reverse('food:inventory-scan-start'), {'location_id': self.fridge.pk})
self.assertRedirects(response, reverse('food:scanner-inventory'), fetch_redirect_response=False)
run = InventoryScanRun.objects.get(user=self.user)
@@ -245,6 +274,28 @@ class InventoryScanWorkflowViewTests(TestCase):
self.assertContains(response, 'Fridge')
self.assertContains(response, str(run.api_id))
def test_running_session_location_can_be_changed_and_start_requires_explicit_choice(self):
response = self.client.post(reverse('food:inventory-scan-start'), {})
self.assertEqual(response.status_code, 400)
self.assertContains(response, 'Choose one of your existing inventory locations.', status_code=400)
self.assertFalse(InventoryScanRun.objects.filter(user=self.user).exists())
run = get_or_create_active_run(self.user, self.fridge.pk)
item = create_active_scan(run, self.user, '87654321')
confirm_scan_item(self.user, item.pk, {
'location_id': self.fridge.pk, 'product_name': 'Rice',
})
response = self.client.post(reverse('food:inventory-scan-location', args=[item.batch_id]), {
'location_id': self.pantry.pk,
})
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['location'], 'Pantry')
item.refresh_from_db()
self.assertEqual(item.location, self.pantry)
response = self.client.get(reverse('food:scanner-inventory'))
self.assertContains(response, 'Current batch location')
self.assertContains(response, f'<option value="{self.pantry.pk}" selected>Pantry</option>', html=True)
def test_confirm_save_next_location_and_done_endpoints(self):
run = get_or_create_active_run(self.user, self.fridge.pk)
response = self.client.post(reverse('food:inventory-scan-create', args=[run.api_id]),
@@ -263,6 +314,8 @@ class InventoryScanWorkflowViewTests(TestCase):
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()['location'], 'Pantry')
next_batch = run.batches.get(status=InventoryScanBatch.Status.DRAFT)
self.assertEqual(response.json()['location_url'],
reverse('food:inventory-scan-location', args=[next_batch.pk]))
response = self.client.post(reverse('food:inventory-scan-submit', args=[next_batch.pk]), {'finish': 'true'})
self.assertEqual(response.json()['redirect'], reverse('food:inventory'))
run.refresh_from_db()