Kevin Veroneau  committed 67cecd3

Added ability to add cheque image after adding the cheque.

  • Participants
  • Parent commits fffb63b

Comments (0)

Files changed (4)

File pcbank/

     class Meta:
         model = Cheque
         exclude = ('transaction',)
+class UploadChequeForm(forms.ModelForm):
+    class Meta:
+        model = Cheque
+        fields = ('image',)

File pcbank/templates/pcbank/cheque_detail.html

 {% extends "pcbank/base.html" %}
 {% load pcbank_extras %}
+{% load url from future %}
 {% block body %}
-{% if not object.transaction %}<form action="{% url assign-transaction object.number %}" method="post">{% csrf_token %}{% endif %}
+{% if not object.transaction %}<form action="{% url 'assign-transaction' object.number %}" method="post">{% csrf_token %}{% endif %}
 <table class="table table-condensed">
 <tr><td><b>Cheque #</b></td><td>{{object.number}}</td></tr>
 {% if not object.transaction %}<input type="submit" value="Assign Transaction" class="btn btn-primary" /></form>{% endif %}
-{% if object.image %}<img src="{{object.image.url}}"/>{% endif %}
+{% if object.image %}<img src="{{object.image.url}}"/>{% else %}
+<form action="{% url 'upload-cheque' object.number %}" method="post" enctype="multipart/form-data">{% csrf_token %}{{form.as_p}}<input class="btn btn-primary" type="submit" value="Upload Cheque"/></form>
+{% endif %}
 {% endblock %}

File pcbank/

 from pcbank.forms import NewBillForm, NewChequeForm
 from pcbank.feeds import BankingBookmarks
+from pcbank.views import ChequeDetail
 urlpatterns = patterns('pcbank.views',
     (r'^$', 'overview'),
     url(r'^ChequeBook/$', 'show_list', {'queryset':Cheque.objects.all()}, name='list-cheques'),
     url(r'^ChequeBook/$', 'create', {'model':Cheque, 'form_class':NewChequeForm}, name='add-cheque'),
     url(r'^ChequeBook/(?P<cheque_id>\d+)/$', 'assign_transaction', name='assign-transaction'),
-    url(r'^ChequeBook/(?P<slug>\d+)/$', 'details', {'queryset':Cheque.objects.all(), 'slug_field':'number'}, name='view-cheque'),
+    url(r'^ChequeBook/(?P<slug>\d+)/$', ChequeDetail.as_view(), name='view-cheque'),
+    url(r'^ChequeBook/(?P<cheque_id>\d+)/$', 'upload_cheque', name='upload-cheque'),
     url(r'^Roommate/$', 'create', {'model':Roommate}, name='add-roommate'),
     url(r'^Roommate/$', 'show_list', {'queryset':Roommate.objects.filter(active=True)}, name='list-roommate'),
     url(r'^Roommate/(?P<object_id>\d+)/$', 'update', {'model':Roommate}, name='edit-roommate'),

File pcbank/

 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)
 def overview(req):
 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
     return redirect(cheque)
+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)
+    return redirect(cheque)