]> err.no Git - peojumk/commitdiff
Add InvoiceUpdateForm class
authorTollef Fog Heen <tfheen@err.no>
Wed, 1 Aug 2012 18:37:05 +0000 (20:37 +0200)
committerTollef Fog Heen <tfheen@err.no>
Wed, 1 Aug 2012 18:37:05 +0000 (20:37 +0200)
invoice/forms.py

index 438b6b33e2372114cf88e695a1c5bd5f3778391c..62b7768c4171460ac52ddf1f965c53316df113ac 100644 (file)
@@ -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()
+