From b01872e3e5cf7baca6cfabc4c2bab306842ab05f Mon Sep 17 00:00:00 2001 From: Hugo LEVY-FALK Date: Tue, 14 Nov 2017 20:00:24 +0100 Subject: [PATCH] Fix #48 : Rename invoice PDF file --- cotisations/tex.py | 27 ++++++++++++++++++--------- cotisations/views.py | 6 +++--- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/cotisations/tex.py b/cotisations/tex.py index 4a83ca67..17d85a13 100644 --- a/cotisations/tex.py +++ b/cotisations/tex.py @@ -21,17 +21,14 @@ # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. from django.template.loader import get_template -from django.template import TemplateDoesNotExist, Context -from django.http import HttpResponse, Http404, HttpResponseNotModified -from django.core.cache import cache +from django.template import Context +from django.http import HttpResponse from django.conf import settings -from django.shortcuts import redirect +from django.utils.text import slugify import tempfile from subprocess import Popen, PIPE import os -import shutil -from hashlib import md5 TEMP_PREFIX = getattr(settings, 'TEX_TEMP_PREFIX', 'render_tex-') @@ -39,9 +36,22 @@ CACHE_PREFIX = getattr(settings, 'TEX_CACHE_PREFIX', 'render-tex') CACHE_TIMEOUT = getattr(settings, 'TEX_CACHE_TIMEOUT', 86400) # 1 day -def render_tex(request,tmp, ctx={}): +def render_invoice(request, ctx={}): + filename = '_'.join([ + 'invoice', + slugify(ctx['asso_name']), + slugify(ctx['dest'].pseudo), + str(ctx['DATE'].year), + str(ctx['DATE'].month), + str(ctx['DATE'].day), + ]) + r = render_tex(request, 'cotisations/factures.tex', ctx) + r['Content-Disposition'] = ''.join(['attachment; filename="',filename,'.pdf"']) + return r + +def render_tex(request, template, ctx={}): context = Context(ctx) - template = get_template('cotisations/factures.tex') + template = get_template(template) rendered_tpl = template.render(context).encode('utf-8') with tempfile.TemporaryDirectory() as tempdir: @@ -55,6 +65,5 @@ def render_tex(request,tmp, ctx={}): with open(os.path.join(tempdir, 'texput.pdf'), 'rb') as f: pdf = f.read() r = HttpResponse(content_type='application/pdf') - #r['Content-Disposition'] = 'attachement; filename=texput.pdf' r.write(pdf) return r diff --git a/cotisations/views.py b/cotisations/views.py index 4dd6487a..1a9b8ed9 100644 --- a/cotisations/views.py +++ b/cotisations/views.py @@ -61,7 +61,7 @@ from .forms import ( SelectClubArticleForm, CreditSoldeForm ) -from .tex import render_tex +from .tex import render_invoice @login_required @@ -182,7 +182,7 @@ def new_facture_pdf(request): tbl.append([art, quantite, art.prix * quantite]) prix_total = sum(a[2] for a in tbl) user = {'name': destinataire, 'room': chambre} - return render_tex(request, 'cotisations/factures.tex', { + return render_invoice(request, { 'DATE': timezone.now(), 'dest': user, 'fid': fid, @@ -233,7 +233,7 @@ def facture_pdf(request, factureid): options, _created = AssoOption.objects.get_or_create() for vente in ventes_objects: ventes.append([vente, vente.number, vente.prix_total]) - return render_tex(request, 'cotisations/factures.tex', { + return render_invoice(request, { 'paid': True, 'fid': facture.id, 'DATE': facture.date,