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

24
boxes/forms.py Normal file
View File

@@ -0,0 +1,24 @@
from django import forms
from .models import Thing
class ThingForm(forms.ModelForm):
"""Form for adding a Thing."""
class Meta:
model = Thing
fields = ('name', 'thing_type', 'description', 'picture')
widgets = {
'name': forms.TextInput(attrs={'class': 'form-control'}),
'thing_type': forms.Select(attrs={'class': 'form-control'}),
'description': forms.Textarea(attrs={'class': 'form-control', 'rows': 2}),
}
ThingFormSet = forms.modelformset_factory(
Thing,
form=ThingForm,
extra=1,
can_delete=False
)

View File

@@ -0,0 +1,215 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add Things to Box {{ box.id }} - LabHelper</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
margin: 20px;
background-color: #f5f5f5;
}
h1 {
color: #333;
}
.container {
background: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}
form {
display: table;
width: 100%;
}
.form-row {
display: table-row;
}
.form-cell {
display: table-cell;
padding: 8px;
}
.form-header {
font-weight: 600;
color: #333;
padding-bottom: 8px;
}
.form-header-cell {
padding-top: 0;
}
.form-cell input {
width: 100%;
padding: 8px 12px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
font-size: 14px;
}
.form-cell input:focus {
outline: none;
border-color: #4a90a4;
}
.form-cell textarea {
width: 100%;
padding: 8px 12px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
font-size: 14px;
resize: vertical;
}
.form-cell textarea:focus {
outline: none;
border-color: #4a90a4;
}
.form-cell select {
width: 100%;
padding: 8px 12px;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
font-size: 14px;
background-color: white;
}
.form-cell select:focus {
outline: none;
border-color: #4a90a4;
}
.btn {
background-color: #4a90a4;
color: white;
padding: 12px 24px;
border: none;
border-radius: 4px;
font-size: 16px;
cursor: pointer;
font-weight: 600;
}
.btn:hover {
background-color: #3d7a96;
}
.back-link {
margin-bottom: 20px;
display: inline-block;
color: #4a90a4;
text-decoration: none;
}
.back-link:hover {
text-decoration: underline;
}
.error-list {
color: #d9534f;
list-style: none;
padding: 0;
}
.error-list li {
padding: 8px 0;
margin-bottom: 8px;
}
.success-message {
background-color: #d4edda;
color: #155724;
padding: 15px;
border-radius: 6px;
margin-bottom: 20px;
}
.required {
color: #d9534f;
}
</style>
</head>
<body>
<a href="/" class="back-link">&larr; Home</a>
<h1>Add Things to Box {{ box.id }}</h1>
<div class="container">
<p>
<strong>Box:</strong> {{ box.id }} ({{ box.box_type.name }})
</p>
{% if formset.non_form_errors %}
<div class="error-list">
{% for form_errors in formset.non_form_errors %}
{% for field, errors in form_errors.items %}
{% for error in errors %}
<li>{{ error }}</li>
{% endfor %}
{% endfor %}
{% endfor %}
</div>
{% endif %}
{% if formset.total_form_count %}
<form method="post" action="">
{% csrf_token %}
<table>
<tr class="form-row">
<th class="form-header-cell"></th>
<th class="form-header form-header-cell">Name</th>
<th class="form-header form-header-cell">Type</th>
<th class="form-header form-header-cell">Description</th>
<th class="form-header form-header-cell">Picture</th>
</tr>
{{ formset.management_form }}
{% for form in formset %}
<tr class="form-row">
<td class="form-cell">
{{ form.id }}
</td>
<td class="form-cell">
{{ form.name }}
{% for error in form.name.errors %}
<div class="error-list">
{% for e in error %}
<li>{{ e }}</li>
{% endfor %}
</div>
{% endfor %}
<label class="required">*</label>
</td>
<td class="form-cell">
{{ form.thing_type }}
{% for error in form.thing_type.errors %}
<div class="error-list">
{% for e in error %}
<li>{{ e }}</li>
{% endfor %}
</div>
{% endfor %}
<label class="required">*</label>
</td>
<td class="form-cell">
{{ form.description }}
{% for error in form.description.errors %}
<div class="error-list">
{% for e in error %}
<li>{{ e }}</li>
{% endfor %}
</div>
{% endfor %}
</td>
<td class="form-cell">
{{ form.picture }}
{% for error in form.picture.errors %}
<div class="error-list">
{% for e in error %}
<li>{{ e }}</li>
{% endfor %}
</div>
{% endfor %}
</td>
</tr>
{% endfor %}
<tr class="form-row">
<td class="form-cell" colspan="5">
<button type="submit" class="btn">Save Things</button>
</td>
</tr>
</table>
</form>
{% endif %}
</div>
</body>
</html>

View File

@@ -79,6 +79,8 @@
<div class="box-info">
<strong>Type:</strong> {{ box.box_type.name }}
({{ box.box_type.width }} x {{ box.box_type.height }} x {{ box.box_type.length }} mm)
<br><br>
<a href="/box/{{ box.id }}/add/">+ Add Things</a>
</div>
{% if things %}

View File

@@ -663,3 +663,126 @@ class SearchApiTests(TestCase):
results = response.json()['results']
self.assertEqual(results[0]['type'], 'Electronics')
self.assertEqual(results[0]['box'], 'BOX001')
class AddThingsViewTests(TestCase):
"""Tests for add things view."""
def setUp(self):
"""Set up test fixtures."""
self.client = Client()
self.box_type = BoxType.objects.create(
name='Standard Box',
width=200,
height=100,
length=300
)
self.box = Box.objects.create(
id='BOX001',
box_type=self.box_type
)
self.thing_type = ThingType.objects.create(name='Electronics')
def test_add_things_get_request(self):
"""Add things page should return 200 for GET request."""
response = self.client.get(f'/box/{self.box.id}/add/')
self.assertEqual(response.status_code, 200)
def test_add_things_shows_box_id(self):
"""Add things page should show box ID."""
response = self.client.get(f'/box/{self.box.id}/add/')
self.assertContains(response, 'BOX001')
self.assertContains(response, 'Standard Box')
def test_add_things_shows_form(self):
"""Add things page should show form."""
response = self.client.get(f'/box/{self.box.id}/add/')
self.assertContains(response, 'Save Things')
self.assertContains(response, 'Name')
self.assertContains(response, 'Type')
self.assertContains(response, 'Description')
self.assertContains(response, 'Picture')
def test_add_things_post_valid(self):
"""Adding valid things should redirect to box detail."""
response = self.client.post(f'/box/{self.box.id}/add/', {
'form-TOTAL_FORMS': '3',
'form-0-name': 'Arduino Uno',
'form-0-thing_type': self.thing_type.id,
'form-0-description': 'A microcontroller',
'form-1-name': 'LED Strip',
'form-1-thing_type': self.thing_type.id,
'form-1-description': 'Lighting component',
})
self.assertRedirects(response, f'/box/{self.box.id}/')
self.assertEqual(Thing.objects.count(), 2)
def test_add_things_post_required_name(self):
"""Adding things without name should show error."""
response = self.client.post(f'/box/{self.box.id}/add/', {
'form-TOTAL_FORMS': '2',
'form-0-thing_type': self.thing_type.id,
'form-1-thing_type': self.thing_type.id,
})
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'This field is required')
def test_add_things_post_partial_valid_invalid(self):
"""Partial submission: one valid, one missing name."""
response = self.client.post(f'/box/{self.box.id}/add/', {
'form-TOTAL_FORMS': '2',
'form-0-name': 'Arduino Uno',
'form-0-thing_type': self.thing_type.id,
'form-1-thing_type': self.thing_type.id,
})
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'This field is required')
self.assertEqual(Thing.objects.count(), 1)
def test_add_things_creates_thing_types(self):
"""Can create new thing types while adding things."""
new_type = ThingType.objects.create(name='Components')
response = self.client.post(f'/box/{self.box.id}/add/', {
'form-TOTAL_FORMS': '2',
'form-0-name': 'Resistor',
'form-0-thing_type': new_type.id,
'form-1-name': 'Capacitor',
'form-1-thing_type': new_type.id,
})
self.assertRedirects(response, f'/box/{self.box.id}/')
self.assertEqual(Thing.objects.count(), 2)
self.assertEqual(Thing.objects.filter(thing_type=new_type).count(), 2)
def test_add_things_empty_all(self):
"""Submitting empty forms should not create anything."""
response = self.client.post(f'/box/{self.box.id}/add/', {
'form-TOTAL_FORMS': '2',
'form-0-name': '',
'form-0-thing_type': self.thing_type.id,
'form-1-name': '',
'form-1-thing_type': self.thing_type.id,
})
self.assertRedirects(response, f'/box/{self.box.id}/')
self.assertEqual(Thing.objects.count(), 0)
def test_add_things_box_not_exists(self):
"""Adding things to non-existent box should return 404."""
response = self.client.get('/box/INVALID/add/')
self.assertEqual(response.status_code, 404)
def test_add_things_populates_box(self):
"""Created things should be assigned to the correct box."""
self.thing_type_2 = ThingType.objects.create(name='Mechanical')
response = self.client.post(f'/box/{self.box.id}/add/', {
'form-TOTAL_FORMS': '3',
'form-0-name': 'Bolt',
'form-0-thing_type': self.thing_type_2.id,
'form-1-name': 'Nut',
'form-1-thing_type': self.thing_type_2.id,
'form-2-name': 'Washer',
'form-2-thing_type': self.thing_type_2.id,
})
self.assertRedirects(response, f'/box/{self.box.id}/')
things = Thing.objects.all()
for thing in things:
self.assertEqual(thing.box, self.box)

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,
})