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_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_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 = [] if cover is not None: body = render_to_string('ipho_exam/tex/exam_cover.tex', RequestContext(HttpRequest(), cover)) compile_task = tasks.compile_tex.s(body, []) q = questions[0] s = student_languages[0].student bgenerator = iphocode.QuestionBarcodeGen(q.exam, q, s, qcode='C') barcode_task = tasks.add_barcode.s(bgenerator) all_tasks.append(celery.chain(compile_task, barcode_task)) 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 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)) compile_task = tasks.compile_tex.s(body, ext_resources) else: compile_task = tasks.serve_pdfnode.s(trans.node.pdf.read()) if question.is_answer_sheet(): bgenerator = iphocode.QuestionBarcodeGen( question.exam, question, sl.student) barcode_task = tasks.add_barcode.s(bgenerator) all_tasks.append(celery.chain(compile_task, barcode_task)) else: all_tasks.append(compile_task) 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)) compile_task = tasks.compile_tex.s(body, [ tex.TemplateExport('ipho_exam/tex_resources/ipho2016.cls') ]) bgenerator = iphocode.QuestionBarcodeGen(question.exam, question, sl.student, qcode='W') barcode_task = tasks.add_barcode.s(bgenerator) all_tasks.append(celery.chain(compile_task, barcode_task)) exam_id = question.exam.pk position = question.position filename = u'{}_EXAM-{}-{}.pdf'.format(sl.student.code, exam_id, position) chord_task = celery.chord(all_tasks, tasks.concatenate_documents.s(filename)) if commit: final_task = celery.chain(chord_task, tasks.identity_args.s(), tasks.commit_compiled_exam.s()) task = final_task else: task = chord_task return task
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 compile_question(question, language, logo_file): 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, 'STATIC_PATH': '.' } body = render_to_string('ipho_exam/tex/exam_question.tex', RequestContext(HttpRequest(), context)).replace( u'/srv/exam_tools/app/static/noto', u'.') try: exam_code = question.exam.code position = question.position question_code = question.code folder = u'Translation-{}{}-{}-{}-{}'.format( exam_code, position, question_code, language.delegation.name, language.name.replace(u' ', u'_')) base_folder = folder folder = os.path.join(BASE_PATH, folder) try: os.makedirs(folder) except OSError: pass with open(os.path.join(folder, 'question.tex'), 'w') as f: for r in REPLACEMENTS: body = body.replace(*r) f.write(body) for e in ext_resources: e.save(dirname=folder) # replacing font path in ipho2016.cls with open(os.path.join(folder, 'ipho2016.cls'), 'r') as f: tex_cls = f.read() for r in REPLACEMENTS: tex_cls = tex_cls.replace(*r) with open(os.path.join(folder, 'ipho2016.cls'), 'w') as f: f.write(tex_cls) used_fonts = [ v for k, v in list(fonts.ipho[language.font].items()) if isinstance(v, str) and v.endswith('.ttf') ] used_fonts.extend([ v for k, v in list(fonts.ipho['notosans'].items()) if isinstance(v, str) and v.endswith('.ttf') ]) used_fonts = set(used_fonts) for f in used_fonts: source = os.path.join('/srv/exam_tools/app/static/noto', f) shutil.copyfile(source, os.path.join(folder, f)) for f in os.listdir(folder): if f.endswith('.pdf.svg'): os.remove(os.path.join(folder, f)) shutil.copyfile('/srv/exam_tools/static/' + logo_file, os.path.join(folder, logo_file)) shutil.make_archive(folder, 'zip', root_dir=BASE_PATH, base_dir=base_folder) except Exception as e: print('ERROR', e)
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