def _compile_tex(body, ext_resources): try: return pdf.compile_tex(body, ext_resources) except pdf.TexCompileException as err: print(err) print(err.log) raise
def generate_extra_sheets(student, question, startnum, npages, template_name='exam_blank.tex'): context = { 'polyglossia': 'english', 'polyglossia_options': '', 'font': fonts.ipho['notosans'], 'exam_name': u'{}'.format(question.exam.name), 'code': u'{}{}'.format('Z', question.position), 'pages': list(range(npages)), 'startnum': startnum + 1, } body = render_to_string('ipho_exam/tex/{}'.format(template_name), RequestContext(HttpRequest(), context)) question_pdf = pdf.compile_tex( body, [tex.TemplateExport('ipho_exam/tex_resources/ipho2016.cls')]) bgenerator = iphocode.QuestionBarcodeGen(question.exam, question, student, qcode='Z', startnum=startnum) doc_pdf = pdf.add_barcode(question_pdf, bgenerator) return doc_pdf
def compile_tex(body, ext_resources, filename='question.pdf', etag=None): if etag is None: etag = md5(body.encode('utf8')).hexdigest() doc_pdf = pdf.compile_tex(body, ext_resources) meta = { 'etag': etag, 'filename': filename, 'num_pages': pdf.get_num_pages(doc_pdf), 'barcode_num_pages': 0, 'barcode_base': None, } return doc_pdf, meta
def compile_question(question, language): print('Prepare', question, 'in', language) try: trans = qquery.latest_version(question.pk, language.pk) except: print('NOT-FOUND') return trans_content, ext_resources = trans.qml.make_tex() for r in ext_resources: if isinstance(r, tex.FigureExport): r.lang = language ext_resources.append( tex.TemplateExport('ipho_exam/tex_resources/ipho2016.cls')) context = { 'polyglossia': language.polyglossia, 'polyglossia_options': language.polyglossia_options, 'font': fonts.ipho[language.font], 'extraheader': language.extraheader, 'lang_name': u'{} ({})'.format(language.name, language.delegation.country), 'exam_name': u'{}'.format(question.exam.name), 'code': u'{}{}'.format(question.code, question.position), 'title': u'{} - {}'.format(question.exam.name, question.name), 'is_answer': question.is_answer_sheet(), 'document': trans_content.encode('utf-8'), } body = render_to_string('ipho_exam/tex/exam_question.tex', RequestContext(HttpRequest(), context)) print('Compile...') try: question_pdf = pdf.compile_tex(body, ext_resources) exam_code = question.exam.code position = question.position question_code = question.code filename = u'TRANSLATION-{}{}-{}-{}-{}.pdf'.format( exam_code, position, question_code, language.delegation.name, language.name.replace(u' ', u'_')) with open(filename, 'wb') as fp: fp.write(question_pdf) print(filename, 'DONE') except Exception as e: print('ERROR') print(e)
def compile_stud_exam_question(questions, student_languages, cover=None, commit=False): all_tasks = [] all_docs = [] if cover is not None: body = render_to_string('ipho_exam/tex/exam_cover.tex', RequestContext(HttpRequest(), cover)) question_pdf = pdf.compile_tex(body, []) q = questions[0] s = student_languages[0].student bgenerator = iphocode.QuestionBarcodeGen(q.exam, q, s, qcode='C') page = pdf.add_barcode(question_pdf, bgenerator) all_docs.append(page) for question in questions: for sl in student_languages: if question.is_answer_sheet() and not sl.with_answer: continue print('Prepare', question, 'in', sl.language) trans = qquery.latest_version( question.pk, sl.language.pk ) ## TODO: simplify latest_version, because question and language are already in memory trans_content, ext_resources = trans.qml.make_tex() for r in ext_resources: if isinstance(r, tex.FigureExport): r.lang = sl.language ext_resources.append( tex.TemplateExport('ipho_exam/tex_resources/ipho2016.cls')) context = { 'polyglossia': sl.language.polyglossia, 'polyglossia_options': sl.language.polyglossia_options, 'font': fonts.ipho[sl.language.font], 'extraheader': sl.language.extraheader, 'lang_name': u'{} ({})'.format(sl.language.name, sl.language.delegation.country), 'exam_name': u'{}'.format(question.exam.name), 'code': u'{}{}'.format(question.code, question.position), 'title': u'{} - {}'.format(question.exam.name, question.name), 'is_answer': question.is_answer_sheet(), 'document': trans_content, } body = render_to_string('ipho_exam/tex/exam_question.tex', RequestContext(HttpRequest(), context)) print('Compile', question, sl.language) question_pdf = pdf.compile_tex(body, ext_resources) if question.is_answer_sheet(): bgenerator = iphocode.QuestionBarcodeGen( question.exam, question, sl.student) page = pdf.add_barcode(question_pdf, bgenerator) all_docs.append(page) else: all_docs.append(question_pdf) if question.is_answer_sheet() and question.working_pages > 0: context = { 'polyglossia': 'english', 'polyglossia_options': '', 'font': fonts.ipho['notosans'], 'extraheader': '', # 'lang_name' : u'{} ({})'.format(sl.language.name, sl.language.delegation.country), 'exam_name': u'{}'.format(question.exam.name), 'code': u'{}{}'.format('W', question.position), 'title': u'{} - {}'.format(question.exam.name, question.name), 'is_answer': question.is_answer_sheet(), 'pages': list(range(question.working_pages)), } body = render_to_string('ipho_exam/tex/exam_blank.tex', RequestContext(HttpRequest(), context)) question_pdf = pdf.compile_tex(body, [ tex.TemplateExport('ipho_exam/tex_resources/ipho2016.cls') ]) bgenerator = iphocode.QuestionBarcodeGen(question.exam, question, sl.student, qcode='W') page = pdf.add_barcode(question_pdf, bgenerator) all_docs.append(page) exam_id = question.exam.pk position = question.position filename = u'{}_EXAM-{}-{}.pdf'.format(sl.student.code, exam_id, position) final_doc = pdf.concatenate_documents(all_docs) with open(filename, 'wb') as fp: fp.write(final_doc) print(filename, 'DONE')
def student_exam_document(questions, student_languages, cover=None, job_task=None): meta = {} meta['num_pages'] = 0 meta['barcode_num_pages'] = 0 meta['barcode_base'] = '' meta['etag'] = '' meta['filename'] = '' all_barcodes = [] all_docs = [] if cover is not None: body = render_to_string('ipho_exam/tex/exam_cover.tex', RequestContext(HttpRequest(), cover)) question_pdf = pdf.compile_tex(body, []) q = questions[0] s = student_languages[0].student bgenerator = iphocode.QuestionBarcodeGen(q.exam, q, s, qcode='C') page = pdf.add_barcode(question_pdf, bgenerator) doc_pages = pdf.get_num_pages(page) meta['num_pages'] += doc_pages meta['barcode_num_pages'] += doc_pages all_barcodes.append(bgenerator.base) all_docs.append(page) for question in questions: for sl in student_languages: if question.is_answer_sheet() and not sl.with_answer: continue print('Prepare {} in {}.'.format(question, sl.language)) trans = qquery.latest_version( question.pk, sl.language.pk ) ## TODO: simplify latest_version, because question and language are already in memory if not trans.lang.is_pdf: trans_content, ext_resources = trans.qml.make_tex() for r in ext_resources: if isinstance(r, tex.FigureExport): r.lang = sl.language ext_resources.append( tex.TemplateExport('ipho_exam/tex_resources/ipho2016.cls')) context = { 'polyglossia': sl.language.polyglossia, 'polyglossia_options': sl.language.polyglossia_options, 'font': fonts.ipho[sl.language.font], 'extraheader': sl.language.extraheader, 'lang_name': u'{} ({})'.format(sl.language.name, sl.language.delegation.country), 'exam_name': u'{}'.format(question.exam.name), 'code': u'{}{}'.format(question.code, question.position), 'title': u'{} - {}'.format(question.exam.name, question.name), 'is_answer': question.is_answer_sheet(), 'document': trans_content, } body = render_to_string('ipho_exam/tex/exam_question.tex', RequestContext(HttpRequest(), context)) print('Compile {} {}.'.format(question, sl.language)) question_pdf = pdf.compile_tex(body, ext_resources) else: question_pdf = trans.node.pdf.read() doc_pages = pdf.get_num_pages(question_pdf) meta['num_pages'] += doc_pages if question.is_answer_sheet(): bgenerator = iphocode.QuestionBarcodeGen( question.exam, question, sl.student) page = pdf.add_barcode(question_pdf, bgenerator) meta['barcode_num_pages'] += doc_pages all_barcodes.append(bgenerator.base) all_docs.append(page) else: all_docs.append(question_pdf) if question.is_answer_sheet() and question.working_pages > 0: context = { 'polyglossia': 'english', 'polyglossia_options': '', 'font': fonts.ipho['notosans'], 'extraheader': '', # 'lang_name' : u'{} ({})'.format(sl.language.name, sl.language.delegation.country), 'exam_name': u'{}'.format(question.exam.name), 'code': u'{}{}'.format('W', question.position), 'title': u'{} - {}'.format(question.exam.name, question.name), 'is_answer': question.is_answer_sheet(), 'pages': list(range(question.working_pages)), } body = render_to_string('ipho_exam/tex/exam_blank.tex', RequestContext(HttpRequest(), context)) question_pdf = pdf.compile_tex(body, [ tex.TemplateExport('ipho_exam/tex_resources/ipho2016.cls') ]) bgenerator = iphocode.QuestionBarcodeGen(question.exam, question, sl.student, qcode='W') page = pdf.add_barcode(question_pdf, bgenerator) doc_pages = pdf.get_num_pages(page) meta['num_pages'] += doc_pages meta['barcode_num_pages'] += doc_pages all_barcodes.append(bgenerator.base) all_docs.append(page) exam_id = question.exam.pk exam_code = question.exam.code position = question.position if all_same(all_barcodes): meta['barcode_base'] = all_barcodes[0] or None else: meta['barcode_base'] = ','.join(all_barcodes) filename = u'{}_EXAM-{}-{}.pdf'.format(sl.student.code, exam_id, position) final_doc = pdf.concatenate_documents(all_docs) meta['filename'] = filename meta['etag'] = md5(final_doc).hexdigest() if job_task is not None: try: doc_task = DocumentTask.objects.get(task_id=job_task) doc = doc_task.document api_url = settings.SITE_URL + reverse('api-exam:document-detail', kwargs=dict(pk=doc.pk)) r = requests.patch( api_url, allow_redirects=False, headers={'ApiKey': settings.EXAM_TOOLS_API_KEYS['PDF Worker']}, files={'file': (meta['filename'], final_doc)}, data={ 'num_pages': meta['num_pages'], 'barcode_num_pages': meta['barcode_num_pages'], 'barcode_base': meta['barcode_base'], }) r.raise_for_status() # or, if r.status_code == requests.codes.ok: doc_task.delete() print('Doc committed: {} {}{}'.format(sl.student.code, exam_code, position)) except DocumentTask.DoesNotExist: pass return final_doc, meta
def compile_all(): for delegation in Delegation.objects.exclude( name=settings.OFFICIAL_DELEGATION): students = Student.objects.filter(delegation=delegation).values( 'id', 'pk', 'code', 'first_name', 'last_name') vid = 'F' points_per_student = [] for student in students: stud_exam_points_list = Marking.objects.filter( version=vid, student=student['id'] ).values('marking_meta__question').annotate( exam_points=Sum('points')).values('exam_points').order_by( 'marking_meta__question__exam', 'marking_meta__question') total = sum([ st_points['exam_points'] for st_points in stud_exam_points_list if st_points['exam_points'] is not None ]) points_per_student.append((student, stud_exam_points_list, total)) exams = MarkingMeta.objects.filter( question__exam__hidden=False).values('question__exam').annotate( exam_points=Sum('max_points')).values( 'question__exam__code', 'question__position', 'question__exam__name', 'exam_points').order_by('question__exam', 'question').distinct() results = u'' results += u'\\vspace{2em}\n\\begin{center}\n' results += u'\\begin{tabular}{' + ''.join(['p{3cm}'] + ['p{1cm}'] * (len(exams) + 1)) + '}\n' for i, ex in enumerate(exams): results += u' & ' results += u'\\textbf{{{}-{}}}'.format(ex['question__exam__code'], ex['question__position']) results += u' & \\textbf{Total} \\\\\n' for (student, stud_exam_points_list, total) in points_per_student: results += u'{} & '.format(student['code']) for p in stud_exam_points_list: results += u'{} & '.format(p['exam_points']) results += u'{} \\\\\n'.format(total) results += u'\\end{tabular}\n' results += u'\\end{center}\n' context = { 'polyglossia': 'english', 'polyglossia_options': '', 'font': fonts.ipho['notosans'], 'extraheader': '', 'delegation': delegation, 'results': results, } body = render_to_string('ipho_marking/tex/exam_points.tex', RequestContext(HttpRequest(), context)) with open(u'FINALPOINTS-{}.tex'.format(delegation.name), 'w') as fp: fp.write(body) continue filename = u'FINALPOINTS-{}.pdf'.format(delegation.name) print('Compile', filename) question_pdf = pdf.compile_tex(body, []) #question_pdf = pdf.compile_tex(body, ext_resources) print('Compile Success') with open(filename, 'wb') as fp: fp.write(question_pdf) print(filename, 'DONE')