Add form to add multiple things to a box
All checks were successful
Build containers when image tags change / build-if-image-changed (., web, containers, main container, git.baumann.gr/adebaumann/labhelper) (push) Successful in 32s
Build containers when image tags change / build-if-image-changed (data-loader, loader, initContainers, init-container, git.baumann.gr/adebaumann/labhelper-data-loader) (push) Successful in 8s

This commit is contained in:
2025-12-28 22:50:37 +01:00
parent f30627196e
commit f6a953158d
7 changed files with 400 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
from django.http import HttpResponse, JsonResponse
from django.shortcuts import get_object_or_404, render
from django.shortcuts import get_object_or_404, redirect, render
from .forms import ThingFormSet
from .models import Box, Thing
@@ -55,3 +56,34 @@ def search_api(request):
for thing in things
]
return JsonResponse({'results': results})
def add_things(request, box_id):
"""Add multiple things to a box at once."""
box = get_object_or_404(Box, pk=box_id)
if request.method == 'POST':
formset = ThingFormSet(request.POST)
if formset.is_valid():
things = formset.save(commit=False)
created_count = 0
for thing in things:
if thing.name or thing.thing_type or thing.description or thing.picture:
thing.box = box
thing.save()
created_count += 1
print(f'DEBUG: created_count={created_count}')
if created_count > 0:
print(f'DEBUG: Redirecting to box_detail with box_id={box_id}')
return redirect('box_detail', box_id=box_id)
else:
print('DEBUG: No valid data submitted')
else:
formset = ThingFormSet()
return render(request, 'boxes/add_things.html', {
'box': box,
'formset': formset,
})