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()
+