Add zipped PDF export of class lists

This commit is contained in:
alazo 2018-06-16 17:45:16 +02:00 committed by Claude Paroz
parent 22563a823c
commit 386d34f63b
4 changed files with 104 additions and 1 deletions

View file

@ -528,3 +528,83 @@ class MentorCompensationPdfForm(CompensationForm, EpcBaseDocTemplate):
self.add_accounting_stamp(self.MENTOR_MANDAT)
self.build(self.story)
class KlassListPDF(EpcBaseDocTemplate):
"""
Génération des rôles de classes en pdf.
"""
def __init__(self, klass):
self.klass = klass
filename = slugify('{0}'.format(klass.name)) + '.pdf'
super().__init__(filename)
self.addPageTemplates([
PageTemplate(id='FirstPage', frames=[self.page_frame], onPage=self.header),
])
def produce(self, klass):
data = [
['Rôle de classe : {0}'.format(klass.name),
'La Chaux-de-Fonds, le {0}'.format(django_format(date.today(), 'j F Y'))
]
]
t = Table(
data, colWidths=[9 * cm, 9 * cm], rowHeights=(0.4 * cm), hAlign=TA_LEFT, spaceAfter=1 * cm
)
t.setStyle(TableStyle(
[
('ALIGN', (0, 0), (0, 0), 'LEFT'),
('ALIGN', (1, 0), (1, 0), 'RIGHT'),
('VALIGN', (0, 0), (-1, -1), 'TOP'),
('FONTSIZE', (0, 0), (0, 0), 12),
('FONT', (0, 0), (0, 0), 'Helvetica-Bold'),
('FONTSIZE', (1, 0), (1, 0), 8),
('FONT', (1, 0), (1, 0), 'Helvetica'),
]
))
self.story.append(t)
data = []
for index, student in enumerate(klass.student_set.all().filter(archived=False).order_by('last_name', 'first_name')):
data.append(['{0}.'.format(index + 1),
'{0} {1}'.format(student.last_name, student.first_name),
student.street,
student.pcode_city,
student.mobile])
data.append(['', ' Form./Employeur:', student.instructor or ''])
data.append([''])
data.append([''])
t = Table(
data[0:52], colWidths=[1 * cm, 5 * cm, 5 * cm, 5 * cm, 2 * cm], rowHeights=(0.4 * cm), hAlign=TA_LEFT
)
t.setStyle(TableStyle(
[
('ALIGN', (0, 0), (0, -1), 'RIGHT'),
('ALIGN', (1, 0), (-1, -1), 'LEFT'),
('VALIGN', (0, 0), (-1, -1), 'TOP'),
('FONTSIZE', (0, 0), (-1, -1), 8),
]
))
self.story.append(t)
if len(data) > 52:
self.story.append(PageBreak())
self.story.append(Paragraph("Rôle de classe {0}".format(klass.name), style_bold_title))
self.story.append(Spacer(0, 2 * cm))
t = Table(
data[52:], colWidths=[1 * cm, 5 * cm, 5 * cm, 5 * cm, 2 * cm], rowHeights=(0.4 * cm), hAlign=TA_LEFT
)
t.setStyle(TableStyle(
[
('ALIGN', (0, 0), (0, -1), 'RIGHT'),
('ALIGN', (1, 0), (-1, -1), 'LEFT'),
('VALIGN', (0, 0), (-1, -1), 'TOP'),
('FONTSIZE', (0, 0), (-1, -1), 8),
]
))
self.story.append(t)
self.build(self.story)

View file

@ -34,7 +34,7 @@ from .models import (
Klass, Section, Option, Student, Teacher, Corporation, CorpContact, Course, Period,
Training, Availability
)
from .pdf import ExpertEdeLetterPdf, UpdateDataFormPDF, MentorCompensationPdfForm
from .pdf import ExpertEdeLetterPdf, UpdateDataFormPDF, MentorCompensationPdfForm, KlassListPDF
from .utils import is_int
@ -1042,6 +1042,27 @@ def print_mentor_ede_compensation_form(request, pk):
return response
def print_klass_list(request):
query = Klass.objects.all(
).annotate(num_students=Count(Case(When(student__archived=False, then=1)))
).filter(num_students__gt=0
).order_by('section', 'name')
filename = 'archive_RolesDeClasses.zip'
path = os.path.join(tempfile.gettempdir(), filename)
with zipfile.ZipFile(path, mode='w', compression=zipfile.ZIP_DEFLATED) as filezip:
for klass in query:
pdf = KlassListPDF(klass)
pdf.produce(klass)
filezip.write(pdf.filename)
with open(filezip.filename, mode='rb') as fh:
response = HttpResponse(fh.read(), content_type='application/zip')
response['Content-Disposition'] = 'attachment; filename="{0}"'.format(filename)
return response
GENERAL_EXPORT_FIELDS = [
('Num_Ele', 'ext_id'),
('Nom_Ele', 'last_name'),