Commit 3cbaa1e7 authored by Laury Bueno's avatar Laury Bueno
Browse files

[WIP/Certification] Add a temporary strategy to get user data for PDF certificates

parent d175a141
...@@ -250,8 +250,66 @@ class CourseCertificationDetailView(DetailView): ...@@ -250,8 +250,66 @@ class CourseCertificationDetailView(DetailView):
class CourseCertificationDetailPDF(View): class CourseCertificationDetailPDF(View):
def get(self, request, **kwargs): def get(self, request, **kwargs):
certificate = CourseCertification.objects.get(link_hash=kwargs['slug'])
if not certificate.course_student.can_emmit_receipt():
raise Http404
context = {}
if certificate:
context['object'] = certificate
context['cert_template'] = CertificateTemplate.objects.get(
course=certificate.course_student.course,
document_type=certificate.type,
)
# Interpolate data into text string
# {Nome} : certificate.student (name)
# {CPF} : certificate.student.cpf)
# {MODULO} : certificate.course.name)
# {CONTRATO} : context['cert_template'].workspace.name)
# {NUM_UNIDADES} : str(certificate.course_total_units))
# {HORAS} : str(certificate.course.course_load)
# {TURMA} : certificate.course_student.get_current_class().name)
# {DATA_INICIO} : certificate.course_student.start_date
# {DATA_FIM} : certificate.course_student.finish_date
if '{MODULO}' in context['cert_template'].text:
context['cert_template'].text = context['cert_template'].text.replace(
'{MODULO}',
certificate.course.name,
)
if '{CPF}' in context['cert_template'].text:
context['cert_template'].text = context['cert_template'].text.replace(
'{CPF}',
certificate.student.cpf,
)
# Prepare start and finish dates
start_date = '{0}/{1}/{2}'.format(
certificate.course_student.start_date.day,
certificate.course_student.start_date.month,
certificate.course_student.start_date.year
)
finish_date = '{0}/{1}/{2}'.format(
certificate.course_student.finish_date.day,
certificate.course_student.finish_date.month,
certificate.course_student.finish_date.year
)
context['cert_template'].text = context['cert_template'].text\
.replace('{NOME}', certificate.student.get_full_name())\
.replace('{CONTRATO}', context['cert_template'].workspace.name)\
.replace('{NUM_UNIDADES}', str(certificate.course_total_units))\
.replace('{HORAS}', str(certificate.course.course_load))\
.replace('{TURMA}', certificate.course_student.get_current_class().name)\
.replace('{DATA_INICIO}', start_date)\
.replace('{DATA_FIM}', finish_date)
template = get_template('certificate_print.html') template = get_template('certificate_print.html')
html = template.render() html = template.render(context)
response = BytesIO() response = BytesIO()
pdf = pisa.pisaDocument(BytesIO(html.encode("UTF-8")), response) pdf = pisa.pisaDocument(BytesIO(html.encode("UTF-8")), response)
if not pdf.err: if not pdf.err:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment