1 # Create your views here.
4 from django.shortcuts import render_to_response
5 from django.template import RequestContext
7 from models import Booking
9 class BookingForm(django.forms.ModelForm):
14 def confirm_booking(booking):
15 from django.core.mail import EmailMessage
17 context = {'booking' : booking}
19 rendered = django.template.loader.render_to_string('tickets/booked.eml', context)
20 (headers, body) = rendered.strip().split('\n\n', 1)
22 # Turn the headers into a dict so EmailMessage can turn them into a
23 # string again. Bit pointless, but it works.
24 # Perhaps we should just use python email stuff directly. OTOH, we
25 # still always need to parse for the From header.
28 # If no From header is present, let EmailMessage do the default
31 for header in headers.split('\n'):
32 (field, value) = header.split(':')
35 elif (field == 'Subject'):
38 # Don't put From and Subject in the dict, else they'll be
40 headers_dict[field] = value
43 # Only setting the From address through headers won't set the
44 # envelope address right.
45 from_email = from_email,
49 headers = headers_dict
54 if request.method == "POST":
55 f = BookingForm(request.POST)
61 confirm_booking(booking)
62 return render_to_response('tickets/booked.html', {'booking' : booking}, context_instance=RequestContext(request))
64 return render_to_response('tickets/bookingform.html', {'form' : f}, context_instance=RequestContext(request))