Problem statement
In this Django project, implement group registration as one submission instead of one attendee at a time. Build the AttendeeFormSet class and the attendee_group view in app/views.py so the whole group is validated together and written inside one transaction. This stops a typo or a full room from leaving half a group registered and the other half lost.
Requirements
Implement:
- AttendeeFormSet in app/views.py
- attendee_group in app/views.py
The following files are already implemented and provided: app/models.py, app/forms.py, app/urls.py and the template. Attendee holds name (CharField(max_length=60)) and email (EmailField) and is ordered by name. AttendeeForm is the ModelForm for one row, those two fields and their labels. app/models.py also holds CAPACITY = 6 and the exception SeatsSoldOut, which Attendee.save() raises when writing one more attendee would take the room past CAPACITY. app/templates/app/attendees.html renders formset from the context: the management form first, then one block per form in it, each with its hidden id, its two fields, a delete box on every row that is already saved, and its own error area.
1. AttendeeFormSet - the formset class
- Built from AttendeeForm with modelformset_factory over Attendee.
- The sheet offers exactly three blank rows on top of the attendees already registered: a blank page declares three rows, a page over two saved attendees declares five, two of them existing.
- Every saved row can be marked for deletion.
- Do not set max_num: the room's capacity is the database's business, not the page's.
2. GET /attendees/
- Purpose: Display the group sheet.
- Response:
- 200 OK: the template rendered with the formset published in the context as formset, unbound, over the registered attendees, every row built from the given AttendeeForm.
3. POST /attendees/
- Purpose: Register or amend the whole group in one submission.
- Validation: the batch is asked once whether it is valid. Do not loop over the forms calling is_valid() or save() one row at a time to decide what to keep.
- Response:
- 302 Found: redirect to the route named attendee-group when every row is valid, with the batch written - rows ticked for deletion removed, existing rows updated, filled blank rows created.
- 200 OK: the sheet re-rendered with the submitted values still in the inputs and each error on the row that earned it, with nothing saved at all: not the invalid row, and not the valid rows on either side of it.
- A submission whose management form is missing or carries a nonsense row count is invalid as a whole and writes nothing, rather than being half read.
4. All or nothing
- The whole batch is written inside one transaction.atomic() block, so when the room fills up half way through a group, SeatsSoldOut comes out of the view and the database holds exactly what it held before the submission.
- Let formset.save() do the writing, or do exactly what it does, deletions included, inside the atomic block.
Notes
- Do not catch SeatsSoldOut. The requirement is that nothing is left behind when it is raised, not that the visitor is spared the error.
- The capacity check cannot be made before the batch starts, because how full the room is depends on the rows written just before.
- Build the redirect from the route name rather than writing the path as a string.
- Do not change the template, the form, the model or the URL map, and do not add a field to AttendeeForm.
