Update PDF templates and enable sending of PDF via email.

PDFs now state QUOTE, INVOICE or RECEIPT.
Single copy and all but INVOICE includes terms of hire.
This commit is contained in:
Tom Price
2017-04-10 22:45:27 +01:00
parent 5d17d642ec
commit 391d9ef28f
5 changed files with 186 additions and 165 deletions

View File

@@ -1,33 +1,79 @@
import reversion
from django.conf import settings
import cStringIO as StringIO
import re
import urllib2
from io import BytesIO
import reversion
from PyPDF2 import PdfFileReader, PdfFileMerger
from django.conf import settings
from django.core.mail import EmailMessage
from django.template.loader import get_template
from z3c.rml import rml2pdf
from RIGS import models
def send_eventauthorisation_success_email(instance):
# Generate PDF first to prevent context conflicts
context = {
'object': instance.event,
'fonts': {
'opensans': {
'regular': 'RIGS/static/fonts/OPENSANS-REGULAR.TTF',
'bold': 'RIGS/static/fonts/OPENSANS-BOLD.TTF',
}
},
'receipt': True,
'current_user': False,
}
template = get_template('RIGS/event_print.xml')
merger = PdfFileMerger()
rml = template.render(context)
buffer = rml2pdf.parseString(rml)
merger.append(PdfFileReader(buffer))
buffer.close()
terms = urllib2.urlopen(settings.TERMS_OF_HIRE_URL)
merger.append(StringIO.StringIO(terms.read()))
merged = BytesIO()
merger.write(merged)
# Produce email content
context = {
'object': instance,
}
subject = "N%05d | %s - Event Authorised" % (instance.event.pk, instance.event.name)
client_email = EmailMessage(
"N%05d | %s - Event Authorised".format(instance.event.pk, instance.event.name),
subject,
get_template("RIGS/eventauthorisation_client_success.txt").render(context),
to=[instance.email]
)
escapedEventName = re.sub('[^a-zA-Z0-9 \n\.]', '', instance.event.name)
client_email.attach('N%05d - %s - RECEIPT.pdf' % (instance.event.pk, escapedEventName),
merged.getvalue(),
'application/pdf'
)
if instance.event.mic:
mic_email_address = instance.event.mic.email
else:
mic_email_address = settings.AUTHORISATION_NOTIFICATION_ADDRESS
mic_email = EmailMessage(
"N%05d | %s - Event Authorised".format(instance.event.pk, instance.event.name),
subject,
get_template("RIGS/eventauthorisation_mic_success.txt").render(context),
to=[mic_email_address]
)
# Now we have both emails successfully generated, send them out
client_email.send()
mic_email.send()