Commits

Luke Plant committed dac12fa

Admin interface for ChequePayment

Comments (0)

Files changed (2)

cciw/bookings/admin.py

 from django import forms
 
 from cciw.bookings.forms import FixPriceMixin
-from cciw.bookings.models import Price, BookingAccount, Booking
+from cciw.bookings.models import Price, BookingAccount, Booking, ChequePayment
 from cciw.cciwmain.common import get_thisyear
 from cciw.utils.views import close_window_response
 
 
 account_autocomplete_field = \
     lambda: ModelChoiceField('account',
-                             label='Account name',
+                             label='Account',
                              widget=AccountAutoCompleteWidget('account',
                                                               attrs={'size':'70'}))
 
                'shelved']}),
         )
 
+
+class ChequePaymentAdminForm(forms.ModelForm):
+
+    account = account_autocomplete_field()
+
+    class Meta:
+        model = ChequePayment
+
+    def clean(self):
+        retval = super(ChequePaymentAdminForm, self).clean()
+        if self.instance is not None and self.instance.id is not None:
+            raise forms.ValidationError("Cheque payments cannot be changed "
+                                        "after being created. If an error was made, "
+                                        "delete this record and create a new one. ")
+        return retval
+
+
+class ChequePaymentAdmin(admin.ModelAdmin):
+    list_display = ['account', 'amount', 'created']
+    form = ChequePaymentAdminForm
+    fieldsets = [(None,
+                  {'fields':
+                       ['account', 'amount', 'created']})]
+
+    def get_readonly_fields(self, request, obj=None):
+        if obj is not None:
+            return ['account', 'amount', 'created']
+        else:
+            return []
+
+
 admin.site.register(Price, PriceAdmin)
 admin.site.register(BookingAccount, BookingAccountAdmin)
 admin.site.register(Booking, BookingAdmin)
+admin.site.register(ChequePayment, ChequePaymentAdmin)

cciw/bookings/models.py

         return u"<Payment: %s to %s from %s>" % (self.amount, self.account, self.origin)
 
 
+class ChequePaymentManager(models.Manager):
+    use_for_related_fields = True
+
+    def get_query_set(self):
+        return super(ChequePaymentManager, self).get_query_set().select_related('account')
+
+
 class ChequePayment(models.Model):
     amount = models.DecimalField(decimal_places=2, max_digits=10)
     account = models.ForeignKey(BookingAccount)
     created = models.DateTimeField(default=datetime.now)
 
+    objects = ChequePaymentManager()
+
+    def __unicode__(self):
+        return u"Cheque payment of £%s from %s" % (self.amount, self.account)
+
     def save(self, **kwargs):
         if self.id is not None:
             raise Exception("ChequePayment cannot be edited after it has been saved to DB")
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.