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_cover(): cover = { 'student': student, 'exam': exam, 'question': question, 'place': 'M439' } body = render_to_string('ipho_exam/tex/exam_cover.tex', RequestContext(HttpRequest(), cover)) question_pdf = _compile_tex(body, []) bgenerator = iphocode.QuestionBarcodeGen(exam, question, student, qcode='C') page = pdf.add_barcode(question_pdf, bgenerator) with open('test_cover.pdf', 'wb') as pdf_file: pdf_file.write(page)
def compile_question(qml_trans, pdf_name='test_question'): ext_resources = [] 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, 'Country'), 'exam_name': u'{}'.format(exam.name), 'code': u'{}{}'.format(question.code, question.position), 'title': u'{} - {}'.format(exam.name, question.name), 'is_answer': True, 'document': qml_trans, } body = render_to_string('ipho_exam/tex/exam_question.tex', RequestContext(HttpRequest(), context)) question_pdf = _compile_tex(body, ext_resources) bgenerator = iphocode.QuestionBarcodeGen(exam, question, student) page = pdf.add_barcode(question_pdf, bgenerator) with open('{}.pdf'.format(pdf_name), 'wb') as pdf_file: pdf_file.write(page)
def compile_graph(): pages = 3 context = { 'polyglossia': 'english', 'polyglossia_options': '', 'font': fonts.ipho['notosans'], 'extraheader': '', 'exam_name': u'{}'.format(exam.name), 'code': u'W2', 'title': u'{} - {}'.format(exam.name, question.name), 'is_answer': True, 'pages': list(range(pages)), } body = render_to_string('ipho_exam/tex/exam_graph.tex', RequestContext(HttpRequest(), context)) question_pdf = _compile_tex( body, [tex.TemplateExport('ipho_exam/tex_resources/ipho2016.cls')]) bgenerator = iphocode.QuestionBarcodeGen(exam, question, student, qcode='W') page = pdf.add_barcode(question_pdf, bgenerator) with open('test_graph.pdf', 'wb') as pdf_file: pdf_file.write(page)
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 add_barcode(compiled_pdf, bgenerator): question_pdf, meta = compiled_pdf doc_pdf = pdf.add_barcode(question_pdf, bgenerator) meta['barcode_num_pages'] = meta['num_pages'] meta['barcode_base'] = bgenerator.base return doc_pdf, meta