Add test
This commit is contained in:
parent
7fbda2ba23
commit
326def22e5
2 changed files with 57 additions and 89 deletions
121
cms/pdf.py
121
cms/pdf.py
|
|
@ -1,24 +1,23 @@
|
|||
import os
|
||||
import tempfile
|
||||
|
||||
from django.contrib.staticfiles.finders import find
|
||||
from django.conf import settings
|
||||
from django.contrib.staticfiles.finders import find
|
||||
|
||||
from reportlab.lib import colors
|
||||
from reportlab.lib.enums import TA_LEFT, TA_CENTER
|
||||
from reportlab.lib.pagesizes import A4, landscape
|
||||
from reportlab.lib.styles import ParagraphStyle
|
||||
from reportlab.lib.units import cm
|
||||
from reportlab.lib.styles import ParagraphStyle as ps
|
||||
from reportlab.pdfgen import canvas
|
||||
|
||||
from reportlab.platypus import (Frame, FrameBreak, Flowable, NextPageTemplate,
|
||||
Paragraph, PageTemplate, Preformatted, Spacer,
|
||||
SimpleDocTemplate, Table, TableStyle
|
||||
)
|
||||
|
||||
style_normal = ps(name='CORPS', fontName='Helvetica', fontSize=9, alignment=TA_LEFT)
|
||||
style_bold = ps(name='CORPS', fontName='Helvetica-Bold', fontSize=10, alignment=TA_LEFT)
|
||||
style_footer = ps(name='CORPS', fontName='Helvetica', fontSize=7, alignment=TA_CENTER)
|
||||
style_normal = ParagraphStyle(name='CORPS', fontName='Helvetica', fontSize=9, alignment=TA_LEFT)
|
||||
style_bold = ParagraphStyle(name='CORPS', fontName='Helvetica-Bold', fontSize=10, alignment=TA_LEFT)
|
||||
style_footer = ParagraphStyle(name='CORPS', fontName='Helvetica', fontSize=7, alignment=TA_CENTER)
|
||||
|
||||
LOGO_EPC = find('img/logo_EPC.png')
|
||||
LOGO_ESNE = find('img/logo_ESNE.png')
|
||||
|
|
@ -42,10 +41,8 @@ class HorLine(Flowable):
|
|||
class EpcBaseDocTemplate(SimpleDocTemplate):
|
||||
points = '.' * 93
|
||||
|
||||
def __init__(self, filename, section='', title='', orientation=A4):
|
||||
def __init__(self, filename, section='', subject='', orientation=A4):
|
||||
path = os.path.join(tempfile.gettempdir(), filename)
|
||||
self.section = section
|
||||
self.title = title
|
||||
super().__init__(
|
||||
path,
|
||||
pagesize=orientation,
|
||||
|
|
@ -56,9 +53,11 @@ class EpcBaseDocTemplate(SimpleDocTemplate):
|
|||
id='first_table', showBoundary=0, leftPadding=0 * cm
|
||||
)
|
||||
self.story = []
|
||||
#self.title = title
|
||||
self.section = section
|
||||
self.subject = subject
|
||||
|
||||
def header(self, canvas, doc):
|
||||
# Logos
|
||||
canvas.saveState()
|
||||
canvas.drawImage(
|
||||
LOGO_EPC, doc.leftMargin, doc.height - 1.5 * cm, 5 * cm, 3 * cm, preserveAspectRatio=True
|
||||
|
|
@ -67,13 +66,14 @@ class EpcBaseDocTemplate(SimpleDocTemplate):
|
|||
LOGO_ESNE, doc.width - 2.5 * cm, doc.height - 1.2 * cm, 5 * cm, 3 * cm, preserveAspectRatio=True
|
||||
)
|
||||
|
||||
# Section and subject
|
||||
x = doc.leftMargin
|
||||
y = doc.height - 2.5 * cm
|
||||
canvas.setFont('Helvetica-Bold', 10)
|
||||
canvas.line(x, y, x + doc.width, y)
|
||||
y -= 0.4 * cm
|
||||
canvas.drawString(x, y, self.section)
|
||||
canvas.drawRightString(x + doc.width, y, self.title)
|
||||
canvas.drawRightString(x + doc.width, y, self.subject)
|
||||
y -= 0.2 * cm
|
||||
canvas.line(x, y, x + doc.width, y)
|
||||
|
||||
|
|
@ -81,35 +81,34 @@ class EpcBaseDocTemplate(SimpleDocTemplate):
|
|||
canvas.line(doc.leftMargin, 1 * cm, doc.width + doc.leftMargin, 1 * cm)
|
||||
footer = Paragraph(settings.PDF_FOOTER_TEXT, style_footer)
|
||||
w, h = footer.wrap(doc.width, doc.bottomMargin)
|
||||
footer.drawOn(canvas, doc.leftMargin, h )
|
||||
footer.drawOn(canvas, doc.leftMargin, h)
|
||||
canvas.restoreState()
|
||||
|
||||
def later_header(self, canvas, doc):
|
||||
# Footer
|
||||
canvas.saveState()
|
||||
canvas.line(doc.leftMargin, 1 * cm, doc.width + doc.leftMargin, 1 * cm)
|
||||
footer = Paragraph(settings.PDF_FOOTER_TEXT, style_footer)
|
||||
w, h = footer.wrap(doc.width, doc.bottomMargin)
|
||||
footer.drawOn(canvas, doc.leftMargin, h)
|
||||
canvas.restoreState()
|
||||
|
||||
|
||||
def formating(self, text):
|
||||
return Preformatted(text, style_normal, maxLineLength=25)
|
||||
def formating(self, text, len=25):
|
||||
return Preformatted(text, style_normal, maxLineLength=len)
|
||||
|
||||
def normal_template_page(self):
|
||||
first_page_table_frame = Frame(self.leftMargin, self.bottomMargin, self.width + 1 * cm, self.height - 5 * cm,
|
||||
id='first_table', showBoundary=0, leftPadding=0 * cm)
|
||||
later_pages_table_frame = Frame(self.leftMargin, self.bottomMargin, self.width + 1 * cm, self.height - 2 * cm,
|
||||
later_pages_table_frame = Frame(self.leftMargin, self.bottomMargin, self.width + 1 * cm, self.height - 5 * cm,
|
||||
id='later_table', showBoundary=0, leftPadding=0 * cm)
|
||||
# Page template
|
||||
first_page = PageTemplate(id='FirstPage', frames=[first_page_table_frame], onPage=self.header)
|
||||
later_pages = PageTemplate(id='LaterPages', frames=[later_pages_table_frame], onPage=self.later_header)
|
||||
later_pages = PageTemplate(id='LaterPages', frames=[later_pages_table_frame], onPage=self.header)
|
||||
self.addPageTemplates([first_page, later_pages])
|
||||
self.story = [NextPageTemplate(['*', 'LaterPages'])]
|
||||
|
||||
def six_semester_template_page(self):
|
||||
frame_title = Frame(self.leftMargin, 24*cm, self.width, 1*cm, showBoundary=0, leftPadding=0)
|
||||
frame_size = (7 * cm, 6 * cm,)
|
||||
w, h = (7.5 * cm, 6.5 * cm,)
|
||||
|
||||
x = [self.leftMargin, 11 * cm] * 3
|
||||
|
|
@ -117,7 +116,7 @@ class EpcBaseDocTemplate(SimpleDocTemplate):
|
|||
frames = [Frame(x[f], y[f], width=w, height=h, showBoundary=0, leftPadding=0) for f in range(6)]
|
||||
# Frame for total periods
|
||||
frames.append(Frame(self.leftMargin, self.bottomMargin, self.width, 1.5 * cm, leftPadding=0))
|
||||
frames.insert(0,frame_title)
|
||||
frames.insert(0, frame_title)
|
||||
# Page template
|
||||
frame_page = PageTemplate(id='FirstPage', frames=frames, onPage=self.header)
|
||||
self.addPageTemplates(frame_page)
|
||||
|
|
@ -148,58 +147,6 @@ class NumberedCanvas(canvas.Canvas):
|
|||
self.setFont("Helvetica", 7)
|
||||
self.drawString(self._pagesize[0] / 2, 1 * cm, "Page {0} de {1}".format(self._pageNumber, page_count))
|
||||
|
||||
"""
|
||||
class EpcBaseDocTemplate(SimpleDocTemplate):
|
||||
def __init__(self, filename, title='', pagesize=A4):
|
||||
super().__init__(filename, pagesize=pagesize, _pageBreakQuick=0, lefMargin=1.5 * cm, bottomMargin=1.5 * cm,
|
||||
topMargin=1.5 * cm, rightMargin=1.5 * cm)
|
||||
self.story = []
|
||||
self.title = title
|
||||
|
||||
def header(self, canvas, doc):
|
||||
|
||||
canvas.saveState()
|
||||
canvas.drawImage(LOGO_EPC, doc.leftMargin, doc.height - 0.5 * cm, 5 * cm, 3 * cm, preserveAspectRatio=True)
|
||||
canvas.drawImage(LOGO_ESNE, doc.width - 2 * cm, doc.height - 0.5 * cm, 5 * cm, 3 * cm,
|
||||
preserveAspectRatio=True)
|
||||
canvas.line(doc.leftMargin, doc.height - 0.5 * cm, doc.width + doc.leftMargin, doc.height - 0.5 * cm)
|
||||
canvas.drawString(doc.leftMargin, doc.height - 1.1 * cm, FILIERE)
|
||||
canvas.drawRightString(doc.width + doc.leftMargin, doc.height - 1.1 * cm, self.title)
|
||||
canvas.line(doc.leftMargin, doc.height - 1.3 * cm, doc.width + doc.leftMargin, doc.height - 1.3 * cm)
|
||||
canvas.restoreState()
|
||||
|
||||
def later_header(self, canvas, doc):
|
||||
canvas.saveState()
|
||||
canvas.line(doc.leftMargin, doc.height + 1 * cm, doc.width + doc.leftMargin, doc.height + 1 * cm)
|
||||
canvas.drawString(doc.leftMargin, doc.height + 0.5 * cm, FILIERE)
|
||||
canvas.drawRightString(doc.width + doc.leftMargin, doc.height + 0.5 * cm, self.title)
|
||||
canvas.line(doc.leftMargin, doc.height + 0.2 * cm, doc.width + doc.leftMargin, doc.height + 0.2 * cm)
|
||||
canvas.restoreState()
|
||||
|
||||
def normal_template_page(self):
|
||||
first_page_table_frame = Frame(self.leftMargin, self.bottomMargin, self.width + 1 * cm, self.height - 4 * cm,
|
||||
id='first_table', showBoundary=0, leftPadding=0 * cm)
|
||||
later_pages_table_frame = Frame(self.leftMargin, self.bottomMargin, self.width + 1 * cm, self.height - 2 * cm,
|
||||
id='later_table', showBoundary=0, leftPadding=0 * cm)
|
||||
# Page template
|
||||
first_page = PageTemplate(id='FirstPage', frames=[first_page_table_frame], onPage=self.header)
|
||||
later_pages = PageTemplate(id='LaterPages', frames=[later_pages_table_frame], onPage=self.later_header)
|
||||
self.addPageTemplates([first_page, later_pages])
|
||||
self.story = [NextPageTemplate(['*', 'LaterPages'])]
|
||||
|
||||
def six_semester_template_page(self):
|
||||
frame_size = (8 * cm, 6.5 * cm,)
|
||||
w, h = frame_size
|
||||
|
||||
x = [self.leftMargin, 12 * cm] * 3
|
||||
y = [17 * cm, 17 * cm, 10 * cm, 10 * cm, 3 * cm, 3 * cm]
|
||||
frames = [Frame(x[f], y[f], width=w, height=h, showBoundary=0, leftPadding=0) for f in range(6)]
|
||||
# Frame for total periods
|
||||
frames.append(Frame(self.leftMargin, self.bottomMargin, self.width, 1.5 * cm, leftPadding=0))
|
||||
# Page template
|
||||
frame_page = PageTemplate(id='FirstPage', frames=frames, onPage=self.header)
|
||||
self.addPageTemplates(frame_page)
|
||||
"""
|
||||
|
||||
class ModuleDescriptionPdf(EpcBaseDocTemplate):
|
||||
"""
|
||||
|
|
@ -219,7 +166,7 @@ class ModuleDescriptionPdf(EpcBaseDocTemplate):
|
|||
for sc in c.souscompetence_set.all():
|
||||
str_sous_competence += '- {0} (voir {1})\n'.format(sc.nom, c.code)
|
||||
|
||||
str_res = ' \n'.join(['- {0}'.format(c.nom) for c in module.ressource_set.all()])
|
||||
str_res = ' \n'.join(['- {0}'.format(c.nom) for c in module.ressource_set.all()]) # for future use
|
||||
|
||||
str_objectif = ' \n'.join(['- {0}'.format(c.nom) for c in module.objectif_set.all()])
|
||||
|
||||
|
|
@ -247,14 +194,13 @@ class ModuleDescriptionPdf(EpcBaseDocTemplate):
|
|||
formated_data.append(
|
||||
[
|
||||
Preformatted(foo[0], style_normal, maxLineLength=15),
|
||||
Preformatted(foo[1], style_normal, maxLineLength=97),
|
||||
Preformatted(foo[1], style_normal, maxLineLength=85),
|
||||
]
|
||||
)
|
||||
|
||||
t = Table(data=formated_data, colWidths=[4 * cm, 13 * cm])
|
||||
t.hAlign = TA_LEFT
|
||||
t.setStyle(
|
||||
TableStyle(
|
||||
t.setStyle(tblstyle=TableStyle(
|
||||
[
|
||||
('SIZE', (0, 0), (-1, -1), 7),
|
||||
('ALIGN', (0, 0), (-1, -1), 'LEFT'),
|
||||
|
|
@ -273,7 +219,7 @@ class FormationPlanPdf(EpcBaseDocTemplate):
|
|||
"""
|
||||
|
||||
def __init__(self, filename):
|
||||
super().__init__(filename, 'Filière EDS','Plan de formation', landscape(A4))
|
||||
super().__init__(filename, 'Filière EDS', 'Plan de formation', landscape(A4))
|
||||
self.normal_template_page()
|
||||
|
||||
def formating(self, el1='', length=40):
|
||||
|
|
@ -281,7 +227,7 @@ class FormationPlanPdf(EpcBaseDocTemplate):
|
|||
return Preformatted(el1, style_normal, maxLineLength=length)
|
||||
|
||||
def produce(self, domain, process):
|
||||
|
||||
print(domain[0], process[0])
|
||||
data = [
|
||||
['Domaines', 'Processus', 'Sem1', 'Sem2', 'Sem3', 'Sem4', 'Sem5', 'Sem6'],
|
||||
[self.formating(domain[0]), self.formating(process[0], 60), 'M01', '', '', '', '', ''],
|
||||
|
|
@ -303,8 +249,7 @@ class FormationPlanPdf(EpcBaseDocTemplate):
|
|||
data=data, colWidths=[7 * cm, 9 * cm, 1.5 * cm, 1.5 * cm, 1.5 * cm, 1.5 * cm, 1.5 * cm, 1.5 * cm],
|
||||
spaceBefore=0.5 * cm, spaceAfter=1 * cm
|
||||
)
|
||||
t.setStyle(
|
||||
TableStyle(
|
||||
t.setStyle(tblstyle=TableStyle(
|
||||
[
|
||||
('SIZE', (0, 0), (-1, -1), 8),
|
||||
('FONT', (0, 0), (-1, 0), 'Helvetica-Bold'),
|
||||
|
|
@ -369,7 +314,7 @@ class PeriodSemesterPdf(EpcBaseDocTemplate):
|
|||
"""
|
||||
|
||||
def __init__(self, filename):
|
||||
super().__init__(filename, 'Filière EDS','Périodes de formation')
|
||||
super().__init__(filename, 'Filière EDS', 'Périodes de formation')
|
||||
self.six_semester_template_page()
|
||||
|
||||
def produce(self, context):
|
||||
|
|
@ -382,14 +327,14 @@ class PeriodSemesterPdf(EpcBaseDocTemplate):
|
|||
value = getattr(line, 'sem{0}'.format(sem))
|
||||
data.append([line.nom, '{0} h.'.format(value)])
|
||||
t = Table(data=data, colWidths=[6 * cm, 1 * cm], spaceBefore=0 * cm, spaceAfter=0.5 * cm, hAlign=TA_LEFT,
|
||||
style=[
|
||||
('ALIGN', (0, 0), (0, -1), 'LEFT'),
|
||||
('ALIGN', (1, 0), (1, -1), 'RIGHT'),
|
||||
('LINEBELOW', (0, 0), (1, 0), 1, colors.black),
|
||||
('SIZE', (0, 0), (-1, -1), 8),
|
||||
('FONT', (0, 0), (-1, 0), 'Helvetica-Bold'),
|
||||
]
|
||||
)
|
||||
style=[
|
||||
('ALIGN', (0, 0), (0, -1), 'LEFT'),
|
||||
('ALIGN', (1, 0), (1, -1), 'RIGHT'),
|
||||
('LINEBELOW', (0, 0), (1, 0), 1, colors.black),
|
||||
('SIZE', (0, 0), (-1, -1), 8),
|
||||
('FONT', (0, 0), (-1, 0), 'Helvetica-Bold'),
|
||||
]
|
||||
)
|
||||
self.story.append(t)
|
||||
self.story.append(FrameBreak())
|
||||
|
||||
|
|
|
|||
25
cms/tests.py
25
cms/tests.py
|
|
@ -1,3 +1,26 @@
|
|||
from django.test import TestCase
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import User
|
||||
from django.core import mail
|
||||
from django.test import TestCase, override_settings
|
||||
from django.urls import reverse
|
||||
# Create your tests here.
|
||||
|
||||
class PdfTestCase(TestCase):
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
|
||||
User.objects.create_superuser('me', 'me@example.org', 'mepassword')
|
||||
|
||||
def setUp(self):
|
||||
self.client.login(username='me', password='mepassword')
|
||||
|
||||
def test_plan_pdf(self):
|
||||
response = self.client.get(reverse('plan-pdf'))
|
||||
self.assertEqual(
|
||||
response['Content-Disposition'],
|
||||
'attachment; filename="plan_formation.pdf"'
|
||||
)
|
||||
self.assertEqual(response['Content-Type'], 'application/pdf')
|
||||
self.assertGreater(len(response.content), 200)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue