From 003a00f74cfb36a083a7620714ead1ebb29d8f04 Mon Sep 17 00:00:00 2001 From: Tollef Fog Heen Date: Wed, 1 Aug 2012 20:37:05 +0200 Subject: [PATCH] Add InvoiceUpdateForm class --- invoice/forms.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/invoice/forms.py b/invoice/forms.py index 438b6b3..62b7768 100644 --- a/invoice/forms.py +++ b/invoice/forms.py @@ -9,3 +9,43 @@ class ClientForm(ModelFormMixin, ModelForm): class InvoiceForm(ModelFormMixin, ModelForm): class Meta: model = Invoice + +class InvoiceUpdateForm(ModelFormMixin, ModelForm): + class Meta: + model = Invoice + fields = ('status',) + + def is_valid(self): + print "\n".join(sorted(dir(self))) + print self.has_changed() + print self.get_initial() + print self.changed_data + + for c in self.changed_data: + if c != 'comment' and c != 'status': + if not c in self.errors: + self.errors[c] = [] + self.errors[c].append('Field is not allowed to be changed') + f = 'status' + if f in self.changed_data: + if self.initial[f] == 'N': + # Valid next states are Sent and Paid + if self.data[f] not in [ 'S', 'P' ]: + if not f in self.errors: + self.errors[f] = [] + self.errors[f].append('Invalid next state') + + if self.initial[f] == 'S': + # Valid next states is Paid + if self.data[f] not in [ 'P' ]: + if not f in self.errors: + self.errors[f] = [] + self.errors[f].append('Invalid next state') + + if self.initial[f] == 'P': + if not f in self.errors: + self.errors[f] = [] + self.errors[f].append('Invoice paid, no next state') + + return super(InvoiceUpdateForm, self).is_valid() + -- 2.39.5