Kevin Veroneau avatar Kevin Veroneau committed 67cecd3

Added ability to add cheque image after adding the cheque.

Comments (0)

Files changed (4)

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

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>
 <tr><td><b>Amount</b></td><td>${{object.amount|floatformat:2}}</td></tr>
 </table>
 {% if not object.transaction %}<input type="submit" value="Assign Transaction" class="btn btn-primary" /></form>{% endif %}
 <p>{{object.memo|linebreaksbr}}</p>
-{% 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 %}
     Cheque
 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/add.py$', 'create', {'model':Cheque, 'form_class':NewChequeForm}, name='add-cheque'),
     url(r'^ChequeBook/(?P<cheque_id>\d+)/assign_transaction.py$', '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.py$', 'upload_cheque', name='upload-cheque'),
     url(r'^Roommate/add.py$', '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+)/edit.py$', 'update', {'model':Roommate}, name='edit-roommate'),
     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)
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.