8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-26 22:52:26 +00:00

Merge branch 'rename_invoice_pdf_file' into 'master'

Fix #48 : Rename invoice PDF file

Closes #48

See merge request federez/re2o!33
This commit is contained in:
Gabriel Detraz 2017-11-14 20:29:47 +01:00
commit f4c7d2098f
2 changed files with 21 additions and 12 deletions

View file

@ -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

View file

@ -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,