Problem statement
In this Django project, implement the invoice editor as one page carrying a header and its line items together. Build InvoiceForm, InvoiceItemForm and the inline ItemFormSet in app/forms.py, and the invoice_editor view in app/views.py, so both halves are validated before anything is written and the parent and its rows are saved inside one transaction. This keeps a document that is only ever correct as a whole from being stored in pieces.
Requirements
Implement:
- InvoiceForm in app/forms.py
- InvoiceItemForm in app/forms.py
- ItemFormSet in app/forms.py
- invoice_editor in app/views.py
The following files are already implemented and provided: app/models.py, app/urls.py, the invoice book view, the reference_for() helper and the editor template. Product holds name and a unique code. Invoice holds customer, issued_on, note and a unique reference. InvoiceItem holds invoice (a foreign key with related_name="items"), product, quantity, unit_price and discount, and knows its own line_amount() (quantity times unit price) and line_total() (that amount less the discount). app/models.py also exports BILLING_PREFIX (INV), LEDGER_LIMIT (1000.00) and the exception LedgerFull. app/templates/app/invoice_form.html renders form for the header and formset for the item table, with every input in the table namespaced items-<index>-<field>.
Three rules already live on the models and are not yours to add: unit_price carries a model-level validator, so no path into the table can write a line priced below a cent; reference is unique and reference_for() builds it from the invoice's issue date and customer; and InvoiceItem.save() refuses the line that would bill past LEDGER_LIMIT, raising LedgerFull.
1. InvoiceForm - the header
- A ModelForm over Invoice whose Meta declares exactly customer, issued_on, note, in that order. The reference is not among them, and a reference the browser sends anyway is ignored rather than refused.
- issued_on renders as a date input (type="date") and note as a textarea 3 rows tall.
- The three fields are labelled Billed to, Issue date and Invoice note.
2. InvoiceItemForm - one row
- A ModelForm over InvoiceItem whose Meta declares exactly product, quantity, unit_price, discount. The invoice is not one of them, and no parent id is read out of request.POST.
- A field rule: a quantity below 1 is an error on the quantity field.
- A form rule: a discount larger than the row's line amount is an error on that row rather than on one field. A discount exactly equal to the line amount is allowed.
- The model's own price rule arrives without being written, because the form is derived from the model.
3. ItemFormSet - the table
- Built with inlineformset_factory from Invoice, InvoiceItem and InvoiceItemForm, so the rows belong to one parent invoice and a saved row takes its foreign key from the formset rather than from the browser.
- The editor offers exactly two blank rows beyond the rows the invoice already bills, and every saved row can be marked for deletion.
- An invoice must bill at least one item, and a product may appear on only one line of an invoice. Both are reported on the formset as a whole (formset.non_form_errors()), not on a row.
- Neither rule counts a row whose delete box came back ticked, so one submission may delete the only row for a product and add a new row for that same product. Neither rule fires against the rows already stored, so an edit that resubmits the invoice's own rows unchanged is accepted.
- The five messages the page shows, compared character for character:
Quantity must be at least 1. Unit price must be at least 0.01. Discount cannot exceed the line amount. An invoice needs at least one item. Each product may appear on only one line.
4. GET /invoices/new/ and GET /invoices/<pk>/edit/
- Purpose: Display the editor over a new invoice, or over the invoice being edited.
- Response:
- 200 OK: the header published as form and the table as formset. A new invoice gets an unbound header and two blank rows; an existing one shows its stored header and its own rows and no other invoice's.
5. POST /invoices/new/ and POST /invoices/<pk>/edit/
- Purpose: Save the header and its rows as one document.
- Validation: both halves are bound to the submitted data and each 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 invoice-list when both halves are sound, with the header and its rows written - rows ticked for deletion removed, existing rows updated, filled blank rows created.
- 200 OK: the editor re-rendered with the submitted values still in the inputs and each error in its own area, writing nothing at all: not the row at fault, not the sound rows beside it, and not the header. A tampered management-form row count writes nothing either.
- On a new invoice the view stamps reference with reference_for() on the held-back header before the row is written. An invoice that already has a reference keeps it through every later edit.
- The header and all of its rows are written inside one transaction.atomic() block, so when the ledger fills up half way down the table, LedgerFull comes out of the view and the database holds exactly what it held before the submission: no rows, and no header either.
Notes
- Report every rule where it belongs: a one-field rule on its field, a one-row rule on its row, a whole-table rule on the formset. Attaching a cross-row error to a row blames an innocent row, since swapping either of two duplicated rows would fix it.
- An untouched blank row has no cleaned_data at all, and a row marked for deletion is on its way out: neither is an item a cross-row rule should judge.
- Do not catch LedgerFull. The requirement is that nothing is left behind when it is raised, not that the visitor is spared the error.
- Let formset.save() do the writing, or do exactly what it does, deletions included.
- Do not change the template, the models, the URL map or reference_for(), and build the redirect from the route name.
