diff --git a/RIGS/rigboard.py b/RIGS/rigboard.py index f4ee7bd0..1c6d6b5e 100644 --- a/RIGS/rigboard.py +++ b/RIGS/rigboard.py @@ -145,33 +145,26 @@ class EventPrint(generic.View): def get(self, request, pk): object = get_object_or_404(models.Event, pk=pk) template = get_template('RIGS/event_print.xml') - copies = ('TEC', 'Client') merger = PdfFileMerger() - for copy in copies: - context = RequestContext(request, { # this should be outside the loop, but bug in 1.8.2 prevents this - 'object': object, - 'fonts': { - 'opensans': { - 'regular': 'RIGS/static/fonts/OPENSANS-REGULAR.TTF', - 'bold': 'RIGS/static/fonts/OPENSANS-BOLD.TTF', - } - }, - 'copy': copy, - 'current_user': request.user, - }) + context = RequestContext(request, { # this should be outside the loop, but bug in 1.8.2 prevents this + 'object': object, + 'fonts': { + 'opensans': { + 'regular': 'RIGS/static/fonts/OPENSANS-REGULAR.TTF', + 'bold': 'RIGS/static/fonts/OPENSANS-BOLD.TTF', + } + }, + 'quote': True, + 'current_user': request.user, + }) - # context['copy'] = copy # this is the way to do it once we upgrade to Django 1.8.3 + rml = template.render(context) - rml = template.render(context) - buffer = StringIO.StringIO() - - buffer = rml2pdf.parseString(rml) - - merger.append(PdfFileReader(buffer)) - - buffer.close() + buffer = rml2pdf.parseString(rml) + merger.append(PdfFileReader(buffer)) + buffer.close() terms = urllib2.urlopen(settings.TERMS_OF_HIRE_URL) merger.append(StringIO.StringIO(terms.read())) diff --git a/RIGS/signals.py b/RIGS/signals.py index deac4ce7..42bb6504 100644 --- a/RIGS/signals.py +++ b/RIGS/signals.py @@ -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() diff --git a/RIGS/templates/RIGS/event_print.xml b/RIGS/templates/RIGS/event_print.xml index 6d693117..ad441a21 100644 --- a/RIGS/templates/RIGS/event_print.xml +++ b/RIGS/templates/RIGS/event_print.xml @@ -22,21 +22,21 @@ - + - - + + - + @@ -100,10 +100,11 @@ - {% if not invoice %}[{{ copy }} Copy]{% endif %} [Page of ] - [Paperwork generated by {{current_user.name}} | {% now "d/m/Y H:i" %} | {{object.current_version_id}}] + + [Paperwork generated{% if current_user %}by {{current_user.name}} |{% endif %} {% now "d/m/Y H:i" %} | {{object.current_version_id}}] + @@ -118,7 +119,9 @@ {% if not invoice %}[{{ copy }} Copy]{% endif %} [Page of ] - [Paperwork generated by {{current_user.name}} | {% now "d/m/Y H:i" %} | {{object.current_version_id}}] + + [Paperwork generated{% if current_user %}by {{current_user.name}} |{% endif %} {% now "d/m/Y H:i" %} | {{object.current_version_id}}] + @@ -128,4 +131,4 @@ {% include "RIGS/event_print_page.xml" %} - \ No newline at end of file + diff --git a/RIGS/templates/RIGS/event_print_page.xml b/RIGS/templates/RIGS/event_print_page.xml index a7049459..c0a2afa1 100644 --- a/RIGS/templates/RIGS/event_print_page.xml +++ b/RIGS/templates/RIGS/event_print_page.xml @@ -1,59 +1,71 @@ -{% if invoice %} - + - {% endif %} +

N{{ object.pk|stringformat:"05d" }}: '{{ object.name }}'

-

N{{ object.pk|stringformat:"05d" }}: '{{ object.name }}'

- - -{{object.start_date|date:"D jS N Y"}} - - - - - {{ object.description|default_if_none:""|linebreaksbr }} + + {{object.start_date|date:"D jS N Y"}} - - -{% if invoice %} + + + {{ object.description|default_if_none:""|linebreaksbr }} + + - INVOICE - - - - Invoice Number - - {{ invoice.pk|stringformat:"05d" }} - - - - Invoice Date - - {{ invoice.invoice_date|date:"d/m/Y" }} - - - - PO Number - - {{ object.purchase_order|default_if_none:"" }} - - + {% if invoice %} + INVOICE + + + + Invoice Number + + {{ invoice.pk|stringformat:"05d" }} + + + + Invoice Date + + {{ invoice.invoice_date|date:"d/m/Y" }} + + + {% if object.purchase_order %} + + PO Number + + {{ object.purchase_order|default_if_none:"" }} + + + {% endif %} + - + {% elif quote %} + + QUOTE + + + + Quote Date + + {% now "d/m/Y" %} + + + + + {% elif receipt %} + + RECEIPT + + {% endif %}
- -{% endif %} - @@ -205,17 +217,7 @@ £ {{ object.vat|floatformat:2 }} - - - - {% if invoice %} - VAT Registration Number: 170734807 - {% else %} - This contract is not an invoice. - {% endif %} - - - + Total @@ -229,90 +231,67 @@ -{% if not invoice %} - + + {% if not invoice %} + + + Bookings will + not + be confirmed until the event is authorised online. + + + + + 24 Hour Emergency Contacts: 07825 065681 and 07825 065678 + + {% else %} + + + VAT Registration Number: 170734807 + + + {% endif %} + + + - Bookings will - not - be confirmed until payment is received and the contract is signed. + {% if object.authorised %} + + Event authorised online by {{ object.authorisation.name }} ({{ object.authorisation.email }}) at + {{ object.authorisation.last_edited_at }}. + + {% if object.organisation.union_account %} + + + University ID + Account Code + Authorised Amount + + + {{ object.authorisation.uni_id }} + {{ object.authorisation.account_code }} + £ {{ object.authorisation.amount|floatformat:2 }} + + + {% else %} + + + Purchase Order + Authorised Amount + + + {{ object.authorisation.po }} + £ {{ object.authorisation.amount|floatformat:2 }} + + + {% endif %} + {% endif %} - - 24 Hour Emergency Contacts: 07825 065681 and 07825 065678 - - - - + - - To be signed on booking: - - {% if object.organisation.union_account %} - - - I agree that am authorised to sign this invoice. I agree that I am the President/Treasurer of the hirer, or - that I have provided written permission from either the President or Treasurer of the hirer stating that I can - sign for this invoice. - - - - - I have read, understood and fully accepted the current conditions of hire. I agree to return any dry hire - items to TEC PA & Lighting in the same condition at the end of the hire period. - - - - - - Conditions of hire attached and available on the TEC PA & Lighting website. E&OE - - - - - Please return this form directly to TEC PA & Lighting and not the Students' Union Finance Department. - - - - - Account Code - - - - - - {% else %} - - - I, the hirer, have read, understand and fully accept the current conditions of hire. This document forms a - binding contract between TEC PA & Lighting and the hirer, the aforementioned conditions of hire forming - an integral part of it. - - - - - - Conditions of hire attached and available on the TEC PA & Lighting website. E&OE - - - - {% include "RIGS/event_print_signature.xml" %} - - - To be signed on the day of the event/hire: - - - - I, the hirer, have received the goods/services as requested and in good order. I agree to return any dry hire - items to TEC PA & Lighting in a similar condition at the end of the hire period. - - - {% endif %} - - {% include "RIGS/event_print_signature.xml" %} - - {% endif %} - \ No newline at end of file + diff --git a/RIGS/templates/RIGS/eventauthorisation_mic_success.txt b/RIGS/templates/RIGS/eventauthorisation_mic_success.txt index a15391a5..98e1cdb8 100644 --- a/RIGS/templates/RIGS/eventauthorisation_mic_success.txt +++ b/RIGS/templates/RIGS/eventauthorisation_mic_success.txt @@ -1,4 +1,4 @@ -Hi {{object.event.mic.get_full_name}}, +Hi {{object.event.mic.get_full_name|default_if_none:"somebody"}}, Just to let you know your event N{{object.event.pk|stringformat:"05d"}} has been successfully authorised for {{object.amount}} by {{object.name}} as of {{object.last_edited_at}}.