From 01a2560740f7dc2c402e2c53d88e078a1655cdcb Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Wed, 8 Jan 2020 11:23:52 +0100 Subject: [PATCH] Fixed getting candidate PDF summary --- candidats/pdf.py | 6 ++---- candidats/tests.py | 2 +- candidats/views.py | 19 ++++++++++--------- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/candidats/pdf.py b/candidats/pdf.py index e519649..71d22f4 100644 --- a/candidats/pdf.py +++ b/candidats/pdf.py @@ -4,7 +4,6 @@ from reportlab.lib.units import cm from reportlab.platypus import PageTemplate, Paragraph, Spacer, Table, TableStyle from django.utils.dateformat import format as django_format -from django.utils.text import slugify from stages.pdf import EpcBaseDocTemplate, LOGO_EPC, LOGO_ESNE, style_normal, style_bold from .models import ( @@ -17,9 +16,8 @@ class InscriptionSummaryPDF(EpcBaseDocTemplate): """ PDF for summary of inscription """ - def __init__(self, candidate, **kwargs): - filename = slugify('{0}_{1}'.format(candidate.last_name, candidate.first_name)) + '.pdf' - super().__init__(filename, **kwargs) + def __init__(self, out, **kwargs): + super().__init__(out, **kwargs) self.addPageTemplates([ PageTemplate(id='FirstPage', frames=[self.page_frame], onPage=self.header) ]) diff --git a/candidats/tests.py b/candidats/tests.py index f0b44a5..db4b71a 100644 --- a/candidats/tests.py +++ b/candidats/tests.py @@ -370,7 +370,7 @@ tél. 032 886 33 00 'attachment; filename="dupond_henri.pdf"' ) self.assertEqual(response['Content-Type'], 'application/pdf') - self.assertGreater(len(response.content), 200) + self.assertGreater(int(response['Content-Length']), 1000) def test_export_candidates(self): ede = Section.objects.create(name='EDE') diff --git a/candidats/views.py b/candidats/views.py index 15bb8fd..8e37b84 100644 --- a/candidats/views.py +++ b/candidats/views.py @@ -1,12 +1,14 @@ +import io import os from django.conf import settings from django.contrib import messages -from django.http import HttpResponse -from django.shortcuts import redirect +from django.http import FileResponse +from django.shortcuts import get_object_or_404, redirect from django.template import loader from django.urls import reverse, reverse_lazy from django.utils import timezone +from django.utils.text import slugify from stages.views.base import EmailConfirmationBaseView from candidats.models import Candidate, Interview @@ -170,11 +172,10 @@ def inscription_summary(request, pk): """ Print a PDF summary of inscription """ - candidat = Candidate.objects.get(pk=pk) - pdf = InscriptionSummaryPDF(candidat) + candidat = get_object_or_404(Candidate, pk=pk) + buff = io.BytesIO() + pdf = InscriptionSummaryPDF(buff) pdf.produce(candidat) - - with open(pdf.filename, mode='rb') as fh: - response = HttpResponse(fh.read(), content_type='application/pdf') - response['Content-Disposition'] = 'attachment; filename="{0}"'.format(os.path.basename(pdf.filename)) - return response + filename = slugify('{0}_{1}'.format(candidat.last_name, candidat.first_name)) + '.pdf' + buff.seek(0) + return FileResponse(buff, as_attachment=True, filename=filename)