Merge pull request 'Add form to add multiple things to a box' (#1) from feature/boxform into master
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 8s
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 7s
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 8s
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 7s
Reviewed-on: #1
This commit is contained in:
@@ -27,7 +27,7 @@ spec:
|
|||||||
mountPath: /data
|
mountPath: /data
|
||||||
containers:
|
containers:
|
||||||
- name: web
|
- name: web
|
||||||
image: git.baumann.gr/adebaumann/labhelper:0.023
|
image: git.baumann.gr/adebaumann/labhelper:0.024
|
||||||
imagePullPolicy: Always
|
imagePullPolicy: Always
|
||||||
ports:
|
ports:
|
||||||
- containerPort: 8000
|
- containerPort: 8000
|
||||||
|
|||||||
24
boxes/forms.py
Normal file
24
boxes/forms.py
Normal 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
|
||||||
|
)
|
||||||
215
boxes/templates/boxes/add_things.html
Normal file
215
boxes/templates/boxes/add_things.html
Normal 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">← 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>
|
||||||
@@ -79,6 +79,8 @@
|
|||||||
<div class="box-info">
|
<div class="box-info">
|
||||||
<strong>Type:</strong> {{ box.box_type.name }}
|
<strong>Type:</strong> {{ box.box_type.name }}
|
||||||
({{ box.box_type.width }} x {{ box.box_type.height }} x {{ box.box_type.length }} mm)
|
({{ 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>
|
</div>
|
||||||
|
|
||||||
{% if things %}
|
{% if things %}
|
||||||
|
|||||||
123
boxes/tests.py
123
boxes/tests.py
@@ -663,3 +663,126 @@ class SearchApiTests(TestCase):
|
|||||||
results = response.json()['results']
|
results = response.json()['results']
|
||||||
self.assertEqual(results[0]['type'], 'Electronics')
|
self.assertEqual(results[0]['type'], 'Electronics')
|
||||||
self.assertEqual(results[0]['box'], 'BOX001')
|
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)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
from django.http import HttpResponse, JsonResponse
|
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
|
from .models import Box, Thing
|
||||||
|
|
||||||
|
|
||||||
@@ -55,3 +56,34 @@ def search_api(request):
|
|||||||
for thing in things
|
for thing in things
|
||||||
]
|
]
|
||||||
return JsonResponse({'results': results})
|
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,
|
||||||
|
})
|
||||||
|
|||||||
@@ -19,12 +19,13 @@ from django.conf.urls.static import static
|
|||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.urls import path
|
from django.urls import path
|
||||||
|
|
||||||
from boxes.views import box_detail, index, search, search_api, thing_detail
|
from boxes.views import add_things, box_detail, index, search, search_api, thing_detail
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', index, name='index'),
|
path('', index, name='index'),
|
||||||
path('box/<str:box_id>/', box_detail, name='box_detail'),
|
path('box/<str:box_id>/', box_detail, name='box_detail'),
|
||||||
path('thing/<int:thing_id>/', thing_detail, name='thing_detail'),
|
path('thing/<int:thing_id>/', thing_detail, name='thing_detail'),
|
||||||
|
path('box/<str:box_id>/add/', add_things, name='add_things'),
|
||||||
path('search/', search, name='search'),
|
path('search/', search, name='search'),
|
||||||
path('search/api/', search_api, name='search_api'),
|
path('search/api/', search_api, name='search_api'),
|
||||||
path('admin/', admin.site.urls),
|
path('admin/', admin.site.urls),
|
||||||
|
|||||||
Reference in New Issue
Block a user