1 # Create your views here.
4 from django.shortcuts import render_to_response
6 from models import Booking
8 class BookingForm(django.forms.ModelForm):
12 def confirm_booking(booking):
13 from django.core.mail import EmailMessage
15 context = {'booking' : booking}
17 rendered = django.template.loader.render_to_string('tickets/booked.eml', context)
18 (headers, body) = rendered.strip().split('\n\n', 1)
20 # Turn the headers into a dict so EmailMessage can turn them into a
21 # string again. Bit pointless, but it works.
22 # Perhaps we should just use python email stuff directly. OTOH, we
23 # still always need to parse for the From header.
26 # If no From header is present, let EmailMessage do the default
29 for header in headers.split('\n'):
30 (field, value) = header.split(':')
33 elif (field == 'Subject'):
36 # Don't put From and Subject in the dict, else they'll be
38 headers_dict[field] = value
41 # Only setting the From address through headers won't set the
42 # envelope address right.
43 from_email = from_email,
47 headers = headers_dict
52 if request.method == "POST":
53 f = BookingForm(request.POST)
59 confirm_booking(booking)
60 return render_to_response('tickets/booked.html', {'booking' : booking})
62 return render_to_response('tickets/bookingform.html', {'form' : f})