def get(self, invoice_form=None, invoice_item_forms=[InvoiceItemForm(index=1)]): if invoice_form == None: # Initialize the Invoice form clients = self._client_manager.listify_clients() currencies = CurrencyManager.listify_currencies() languages = LanguageManager.listify_languages() invoice_form = InvoiceForm(clients, currencies, languages) return render_template( self, "invoice_create.html", {"invoice_form": invoice_form, "invoice_item_forms": invoice_item_forms} )
def post(self): commit = False clients = self._client_manager.listify_clients() currencies = CurrencyManager.listify_currencies() languages = LanguageManager.listify_languages() invoice_form = InvoiceForm(data=self.request.POST, clients=clients, currencies=currencies, languages=languages) items = invoice_form.fields["items"].to_python(self.request.POST["invoice-items"]) or 0 max_item_index = int(self.request.POST["h-last-invoice-item-index"]) or 0 invoice_item_forms = [ InvoiceItemForm(index + 1, self.request.POST) for index in range(0, max_item_index) if self.request.POST.has_key("%i_invoice_item-description" % (index + 1)) ] if items > max_item_index: commit = False elif items == 0: commit = False elif invoice_form.is_valid(): # Validates the invoice items commit = True for form in invoice_item_forms: if not form.is_valid(): commit = False break if commit: self._invoice_manager.create( client_id=invoice_form.cleaned_data["client"], currency_id=invoice_form.cleaned_data["currency"], invoice_no=invoice_form.cleaned_data["invoice_no"], invoice_date=invoice_form.cleaned_data["invoice_date"], sale_date=invoice_form.cleaned_data["sale_date"], ) for invoice_item in invoice_item_forms: self._invoice_manager.add_invoice_item( description=invoice_item.cleaned_data["description"], quantity=invoice_item.cleaned_data["quantity"], unit_price=invoice_item.cleaned_data["unit_price"], ) self._invoice_manager.save() else: self.get(invoice_form, invoice_item_forms)