X-Git-Url: https://git.stderr.nl/gitweb?p=matthijs%2Fprojects%2Fdorestad-bookings.git;a=blobdiff_plain;f=tickets%2Fviews.py;h=f8ec3ba874e1a87e7659420e57ede3e87c9249fc;hp=d37674b14a9f77608d25310ecd8efa399f6f8321;hb=f608a07f83a17210ea58324ea27ff03a30bd7135;hpb=4943904718321f6a6ef9f1a75db12195dafbd5fa diff --git a/tickets/views.py b/tickets/views.py index d37674b..f8ec3ba 100644 --- a/tickets/views.py +++ b/tickets/views.py @@ -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))