Source

django-pim / pcbank / views.py

Diff from to

pcbank/views.py

     Cheque
 from decimal import Decimal
 from django.shortcuts import render_to_response, redirect
-from pcbank.forms import NewBillForm
+from pcbank.forms import NewBillForm, UploadChequeForm
 from django.template import RequestContext
 import datetime
 from django.contrib.auth.decorators import login_required
 from django.http import HttpResponse
 from django.utils import simplejson
+from django.views.generic.detail import DetailView
+from django.contrib import messages
+
+class SuperMixin(object):
+    def dispatch(self, request, *args, **kwargs):
+        if not request.user.is_superuser:
+            raise PermissionDenied
+        return super(SuperMixin, self).dispatch(request, *args, **kwargs)
+
+class ChequeDetail(SuperMixin, DetailView):
+    model = Cheque
+    slug_field = 'number'
+    def get_context_data(self, **kwargs):
+        kwargs.update({'form':UploadChequeForm()})
+        return super(ChequeDetail, self).get_context_data(**kwargs)
 
 @login_required
 def overview(req):
 
 @login_required
 def assign_transaction(req, cheque_id):
+    if not req.user.is_superuser:
+        raise PermissionDenied
     cheque = Cheque.objects.get(number=cheque_id)
     transaction = Transaction.objects.get(pk=req.POST.get('transaction_id', 0))
     cheque.transaction = transaction
     cheque.save()
     return redirect(cheque)
+
+@login_required
+def upload_cheque(req, cheque_id):
+    if not req.user.is_superuser:
+        raise PermissionDenied
+    cheque = Cheque.objects.get(number=cheque_id)
+    if req.method == 'GET':
+        return redirect(cheque)
+    form = UploadChequeForm(req.POST, req.FILES, instance=cheque)
+    if not form.is_valid():
+        messages.error(req, "There was an error while uploading the image, try again.")
+        return redirect(cheque)
+    form.save()
+    return redirect(cheque)