tickets: Add an interface for processing payments.
authorMatthijs Kooijman <matthijs@stdin.nl>
Tue, 19 Oct 2010 13:28:35 +0000 (15:28 +0200)
committerMatthijs Kooijman <matthijs@stdin.nl>
Tue, 19 Oct 2010 13:28:35 +0000 (15:28 +0200)
tickets/templates/tickets/payments.html [new file with mode: 0644]
tickets/views.py
urls.py

diff --git a/tickets/templates/tickets/payments.html b/tickets/templates/tickets/payments.html
new file mode 100644 (file)
index 0000000..bf41e72
--- /dev/null
@@ -0,0 +1,49 @@
+{% extends "tickets/base.html" %}
+
+{% block content %}
+{% if confirmed %}
+<h1>Betalingen verwerkt</h1>
+{% else %}
+<h1>Betalingen verwerken</h1>
+{% endif %}
+
+{% if confirmed %}
+Onderstaande reserveringen zijn als betaald gemarkeerd en bevestigingen
+zijn verstuurd.
+{% else %}{% if confirm %}
+Druk op "Bevestigen" om de betalingen definitief te maken en een
+bevestiging per email te versturen voor iedere onderstaande reservering.
+{% else %}
+Hieronder staat een lijst van alle onbetaalde reserveringen. Vink de
+reserveringen aan waarvoor de betaling binnen is. Druk op "Verwerken" om
+de betalingen te registreren.
+{% endif %}{% endif %}
+
+{% if not confirmed %}
+<form method="POST">
+{% csrf_token %}
+{{ form.as_p }}
+{% endif %}
+{% if confirm or confirmed %}
+<ul>
+{% for b in bookings %}
+<li>{{ b }}</li>
+{% endfor %}
+</ul>
+<table>
+    <tr><td>Aantal reserveringen:</td><td>{{ bookings|length }}</td></tr>
+    <tr><td>Aantal kaarten:</td><td>{{ numtickets }}</td></tr>
+    <tr><td>Totaalbedrag:</td><td>€{{ amount|floatformat:2}}</td></tr>
+</table>
+{% if confirmed %}
+<a href="{% url payments %}">Meer betalingen verwerken</a>
+{% else %}
+<input type="submit" value="Bevestigen" />
+</form>
+{% endif %}
+{% else %}
+<input type="submit" value="Verwerken" />
+</form>
+{% endif %}
+
+{% endblock %}
index d37674b14a9f77608d25310ecd8efa399f6f8321..f8ec3ba874e1a87e7659420e57ede3e87c9249fc 100644 (file)
@@ -1,5 +1,7 @@
 # Create your views here.
 
+import datetime
+
 import django
 from django.shortcuts import render_to_response
 from django.template import RequestContext
@@ -62,3 +64,63 @@ def book(request):
         return render_to_response('tickets/booked.html', {'booking' : booking}, context_instance=RequestContext(request))
 
     return render_to_response('tickets/bookingform.html', {'form' : f}, context_instance=RequestContext(request))
+
+# These two forms are used for entering payment details. They aren't
+# very different, so it should really be possible to merge them (but
+# initial attempts to add the confirm field dynamically or setting its
+# value dynamically, didn't work out all too well, so leave it at this
+# for now).
+class PaymentForm(django.forms.Form):
+    bookings = django.forms.models.ModelMultipleChoiceField(
+        queryset=Booking.objects.filter(payment__isnull=True).order_by('pk'),
+        widget=django.forms.CheckboxSelectMultiple,
+        label="Reserveringen")
+
+class PaymentConfirmForm(django.forms.Form):
+    bookings = django.forms.models.ModelMultipleChoiceField(
+        queryset=Booking.objects.filter(payment__isnull=True).order_by('pk'),
+        widget=django.forms.MultipleHiddenInput,
+        label="Reserveringen")
+    # This field is used to distinguish these two forms
+    confirm = django.forms.BooleanField(initial=True, widget=django.forms.HiddenInput)
+
+def payments(request):
+    c = {}
+    bookings = None
+    if request.method != "POST":
+        # First step: Just show an empty form
+        f = PaymentForm()
+    elif not 'confirm' in request.POST:
+        # Second step: Process form and show a summary for confirmation
+        f = PaymentForm(request.POST)
+        if f.is_valid():
+            bookings = f.cleaned_data['bookings']
+            # Output a confirmation form
+            f = PaymentConfirmForm()
+            f.initial['bookings'] = bookings
+            c['confirm'] = True
+    else:
+        # Third step: Summary was confirmed, do the work
+        f = PaymentConfirmForm(request.POST)
+        if f.is_valid():
+            bookings = f.cleaned_data['bookings']
+
+            # Do the work
+            for b in bookings:
+                b.payment = datetime.datetime.now()
+                b.save()
+
+            # Don't show the form again
+            f = None
+            c['confirmed'] = True
+
+    # Add the form to show
+    c['form'] = f
+
+    # Add some data about the selected bookings
+    if bookings:
+        c['bookings'] = bookings
+        c['numtickets'] = sum([b.tickets for b in bookings])
+        c['amount'] = sum([b.price for b in bookings])
+
+    return render_to_response('tickets/payments.html', c, context_instance=RequestContext(request))
diff --git a/urls.py b/urls.py
index c40951e351cd9a220888f0ce4ca1fe3b08c36318..0eef5e4f1ecf1372b990d96a1e1a96b6fd0a5f80 100644 (file)
--- a/urls.py
+++ b/urls.py
@@ -5,6 +5,7 @@ admin.autodiscover()
 
 urlpatterns = patterns('',
     (r'^$', 'dorestad-bookings.tickets.views.book'),
+    url(r'^betalingen/$', 'dorestad-bookings.tickets.views.payments', name='payments'),
     # Example:
     # (r'^dorestad1493/', include('dorestad1493.foo.urls')),