From 12a60e1f508d1a35b530e04d3cb9881324e4655e Mon Sep 17 00:00:00 2001 From: FreneticScribbler Date: Fri, 9 Oct 2020 12:09:12 +0100 Subject: [PATCH] Correct handling of spaces in paperwork filenames Also normalises display of Invoice IDs. Partial fix for #391. --- RIGS/finance.py | 11 +++++++---- RIGS/models.py | 6 +++++- RIGS/rigboard.py | 6 ++---- RIGS/templates/event_print.xml | 4 +--- RIGS/templates/invoice_detail.html | 29 +++++++++++------------------ RIGS/templates/invoice_list.html | 2 +- templates/partials/button.html | 2 +- 7 files changed, 28 insertions(+), 32 deletions(-) diff --git a/RIGS/finance.py b/RIGS/finance.py index 41633738..cd6f8db7 100644 --- a/RIGS/finance.py +++ b/RIGS/finance.py @@ -57,6 +57,11 @@ class InvoiceDetail(generic.DetailView): model = models.Invoice template_name = 'invoice_detail.html' + def get_context_data(self, **kwargs): + context = super(InvoiceDetail, self).get_context_data(**kwargs) + context['page_title'] = "Invoice {} ({})".format(self.object.display_id, self.object.invoice_date.strftime("%d/%m/%Y")) + return context + class InvoicePrint(generic.View): def get(self, request, pk): @@ -74,6 +79,7 @@ class InvoicePrint(generic.View): }, 'invoice': invoice, 'current_user': request.user, + 'filename': 'Invoice {} for {} {}.pdf'.format(invoice.display_id, object.display_id, re.sub(r'[^a-zA-Z0-9 \n\.]', '', object.name)) } rml = template.render(context) @@ -82,11 +88,8 @@ class InvoicePrint(generic.View): pdfData = buffer.read() - escapedEventName = re.sub(r'[^a-zA-Z0-9 \n\.]', '', object.name) - response = HttpResponse(content_type='application/pdf') - response['Content-Disposition'] = "filename=Invoice %05d - N%05d | %s.pdf" % ( - invoice.pk, invoice.event.pk, escapedEventName) + response['Content-Disposition'] = 'filename="{}"'.format(context['filename']) response.write(pdfData) return response diff --git a/RIGS/models.py b/RIGS/models.py index c7642a61..e054b4ff 100644 --- a/RIGS/models.py +++ b/RIGS/models.py @@ -580,11 +580,15 @@ class Invoice(models.Model, RevisionMixin): @property def activity_feed_string(self): - return "#{} for Event {}".format(self.pk, "N%05d" % self.event.pk) + return "#{} for Event {}".format(self.display_id, "N%05d" % self.event.pk) def __str__(self): return "%i: %s (%.2f)" % (self.pk, self.event, self.balance) + @property + def display_id(self): + return "{:05d}".format(self.pk) + class Meta: ordering = ['-invoice_date'] diff --git a/RIGS/rigboard.py b/RIGS/rigboard.py index 3d106cc9..07ff78ff 100644 --- a/RIGS/rigboard.py +++ b/RIGS/rigboard.py @@ -195,6 +195,7 @@ class EventPrint(generic.View): }, 'quote': True, 'current_user': request.user, + 'filename': 'Event {} {} {}.pdf'.format(object.display_id, re.sub(r'[^a-zA-Z0-9 \n\.]', '', object.name), object.start_date) } rml = template.render(context) @@ -209,10 +210,7 @@ class EventPrint(generic.View): merger.write(merged) response = HttpResponse(content_type='application/pdf') - - escapedEventName = re.sub(r'[^a-zA-Z0-9 \n\.]', '', object.name) - - response['Content-Disposition'] = "filename=N%05d | %s.pdf" % (object.pk, escapedEventName) + response['Content-Disposition'] = 'filename="{}"'.format(context['filename']) response.write(merged.getvalue()) return response diff --git a/RIGS/templates/event_print.xml b/RIGS/templates/event_print.xml index 13e073e7..74ff42a8 100644 --- a/RIGS/templates/event_print.xml +++ b/RIGS/templates/event_print.xml @@ -3,7 +3,7 @@ {% load static %} - + @@ -97,8 +97,6 @@ info@nottinghamtec.co.uk Phone: (0115) 846 8720 - - [Page of ] diff --git a/RIGS/templates/invoice_detail.html b/RIGS/templates/invoice_detail.html index 2fea7cd2..5074c989 100644 --- a/RIGS/templates/invoice_detail.html +++ b/RIGS/templates/invoice_detail.html @@ -1,27 +1,20 @@ {% extends 'base_rigs.html' %} - -{% block title %}Invoice {{ object.pk }}{% endblock %} +{% load button from filters %} {% block content %}
-
-
-

Invoice {{ object.pk }} ({{ object.invoice_date|date:"d/m/Y"}})

-
- +
- - - - - - - + + + + + + + {% button 'print' url='invoice_print' pk=object.pk %}
diff --git a/RIGS/templates/invoice_list.html b/RIGS/templates/invoice_list.html index 7e2bfe7b..81ab4fbb 100644 --- a/RIGS/templates/invoice_list.html +++ b/RIGS/templates/invoice_list.html @@ -23,7 +23,7 @@ {% for invoice in invoice_list %} - {{ invoice.pk }}
+ {{ invoice.display_id }}
{% if invoice.void %}(VOID){% elif invoice.is_closed %}(PAID){% else %}(O/S){% endif %} N{{ invoice.event.pk|stringformat:"05d" }}: {{ invoice.event.name }}
{{ invoice.event.get_status_display }}{% if not invoice.event.mic %}, No MIC{% endif %} diff --git a/templates/partials/button.html b/templates/partials/button.html index 7a093e37..d69117bc 100644 --- a/templates/partials/button.html +++ b/templates/partials/button.html @@ -1,7 +1,7 @@ {% if submit %} {% elif pk %} - {{ text }} + {{ text }} {% else %} {{ text }} {% endif %}