def make_latex(ctx, template, includefile=None): if 'supplier' not in ctx: ctx['supplier'] = u'ALL' env = latex.get_latex_env(TEMPLATE_PATH) s = latex.get_latex_settings() if includefile is not None: ttpl = env.get_template(includefile) tfilename = os.path.join( s['build_dir'], '{0}_{1}'.format(ctx['supplier'].id, includefile)) clean_file(tfilename) with codecs.open(tfilename, 'w', encoding='utf-8') as fp: fp.write(ttpl.render(**ctx)) ctx['includefile'] = unicode(tfilename).replace(u'\\', u'/') tpl = env.get_template(template) _id = getattr(ctx['supplier'], 'id', 'ALL') filename = os.path.join( s['build_dir'], '{0}_{1}_{2}'.format(_id, ctx['oday'].id, template)) clean_file(filename) with codecs.open(filename, 'w', encoding='utf-8') as fp: fp.write(tpl.render(**ctx)) pdfname, r1, r2 = latex.render_latex_to_pdf(filename) pdf = os.path.split(pdfname)[1] _name = getattr(ctx['supplier'], 'name', u'ALL') printout, created = Printout.objects.get_or_create( order_day=ctx['oday'], internal=_name == u'ALL', company_name=_name) with open(pdfname, 'rb') as fp: content = ContentFile(fp.read()) printout.pdf.save(pdf, content) printout.save() return printout, pdf
def generate_ratings_pdf(req, data=None): companies = Company.objects.select_related().filter( rate=True).order_by('name') companies = h.calculate_ratings(companies) today = date.today() ctx = dict(user=req.user, companies=companies, for_date=unicode(today.strftime('%B %Y'), 'utf-8')) env = latex.get_latex_env(TEMPLATE_PATH) s = latex.get_latex_settings() tpl = env.get_template('company_rating.tex') filename = os.path.join(s['build_dir'], 'Company_Rating.tex') clean_file(filename) with codecs.open(filename, 'w', encoding='utf-8') as fp: fp.write(tpl.render(**ctx)) pdfname, r1, r2 = latex.render_latex_to_pdf(filename) pdf = os.path.split(pdfname)[1] printout, created = PDFPrintout.objects.get_or_create( category=u'Lieferantenbewertung', generated__month=today.month, generated__year=today.year) with open(pdfname, 'rb') as fp: content = ContentFile(fp.read()) printout.pdf.save(pdf, content) printout.save() return dict(size=printout.pdf.size, filename=pdf, url=printout.pdf.url)
def generate_presence_pdf(req, data): user = User.objects.get(id=data['uid']) ctx = get_presence_context(data['gid'], data['year'], data['month']) student_filter = dict(group=ctx['group'], finished=False) student_sort = ['lastname'] if data['cid']: gen_all = False company = Company.objects.get(id=data['cid']) student_filter['company'] = company else: gen_all = True company = Company.objects.get(short_name=u'Alle') student_sort.insert(0, 'company__name') ctx['incl_sup'] = data['incl_sup'] ctx['company'] = company start = date(data['year'], data['month'], 1) end = start + timedelta(days=30) _students = Student.objects.select_related().filter( **student_filter).order_by(*student_sort) students = [x[0] for x in h.get_presence(_students, start, end)] students, k, whole = _prepare_students(students, ctx, data) ctx['students'] = students ctx['k'] = k ctx['whole'] = whole ctx['s'] = latex.get_latex_settings() ctx['schooldays'] = data['sdays'] ctx['instructor'] = unicode(user.userprofile) ctx['course'] = data['course'] ctx['empty'] = False fullname = make_latex(ctx, 'awhl.tex', company) filename = os.path.split(fullname)[1] printout, created = PresencePrintout.objects.get_or_create( company=company, group=ctx['group'], date=start) with open(fullname, 'rb') as fp: content = ContentFile(fp.read()) printout.pdf.save(filename, content) printout.save() ret = {'url': printout.pdf.url, 'name': filename} if gen_all: full_name = make_latex(ctx, 'awhl_einzeln.tex', company) file_name = os.path.split(full_name)[1] pdf = PDFPrintout.objects.create(category=u'Einzel-AWHL') with open(full_name, 'rb') as fp: content = ContentFile(fp.read()) pdf.pdf.save(file_name, content) pdf.save() ret['surl'] = pdf.pdf.url ret['sname'] = file_name return ret
def generate_presence_clean(req, gid, year, month): ctx = get_presence_context(int(gid), int(year), int(month)) try: ctx['students'] = h.sort_students_for_presence(ctx['group'].students) except AttributeError: s = h.get_students(req.user) ctx['students'] = h.sort_students_for_presence(s) ctx['s'] = latex.get_latex_settings() ctx['schooldays'] = u'' ctx['instructor'] = unicode(req.user.userprofile) ctx['course'] = u'' ctx['empty'] = True filename = make_latex(ctx, 'awhl.tex') with open(filename, 'rb') as fp: response = HttpResponse(fp.read(), content_type='application/pdf') return response
def generate_one_pdf(req, data): oday_id = data.get('oday_id') supplier_id = data.get('supplier_id', None) header = data.get('header', u'') oday = OrderDay.objects.get(id=oday_id) s = latex.get_latex_settings() ctx = dict(s=s, oday=oday, user=req.user, header=header) if supplier_id is not None: supplier = Company.objects.get(id=supplier_id) printout, filename = generate_external(supplier, ctx) return dict(size=printout.pdf.size, filename=filename, url=printout.pdf.url) else: printout, filename = generate_internal(ctx) return dict(size=printout.pdf.size, filename=filename, url=printout.pdf.url)
def make_latex(ctx, template, company=None, name=None): env = latex.get_latex_env(TEMPLATE_PATH) s = latex.get_latex_settings() tpl = env.get_template(template) if name is None: if company is not None: name = u'{0}_{1}_{2}'.format(company.short_name, unicode(ctx['group']), template) else: name = u'{0}_{1}'.format(unicode(ctx['group']), template) else: name = u'{0}_{1}'.format(name, template) name = utils.secure_filename(name) filename = os.path.join(s['build_dir'], name) try: os.remove(filename) except: # noqa: E722 pass ctx['BBZLOGO'] = BBZLOGO ctx['ILBLOGO'] = ILBLOGO with codecs.open(filename, 'w', encoding='utf-8') as fp: fp.write(tpl.render(**ctx)) pdfname, r1, r2 = latex.render_latex_to_pdf(filename) return pdfname