def render(self, context): from orun.template.loader import get_template context['env'] = apps context['_'] = gettext context['exec_query'] = exec_query context['query'] = query context['exec_scalar'] = exec_scalar context['models'] = apps context['ref'] = ref if self.view_type in ('dashboard', 'report'): context['db'] = {'connection': connection} if settings.DEBUG and self.template_name: # context['ref'] = g.env.ref templ = self.template_name.split(':')[-1] if self.view_type in ('dashboard', 'report'): content = get_template(self.template_name) if isinstance(Template, str): return Template(content).render(context) else: return content.render(context) return apps.report_env.get_or_select_template(templ).render( **context) return loader.get_template(templ).render(context) if self.view_type in ('dashboard', 'report') and self.template_name: content = get_template(self.template_name) return content.render(context) # return apps.report_env.from_string(self.content).render(**context) if self.view_type == 'dashboard': return self.content return Template(self.content).render(context)
def _export_report(self, format='pdf', params=None, where=None): qs = model = None if self.model: model = apps[self.model] qs = model.objects.all() _params = defaultdict(list) rep_type = None if self.view and self.view.template_name: rep_type = self.view.template_name.rsplit('.', 1)[1] if rep_type == 'pug': xml = self.view.to_string() elif rep_type == 'rep': xml = self.view.get_xml(model) report_file = xml.attrib['file'] with open( loader.get_template(report_file).template.filename, 'rb') as f: xml = f.read() else: xml = self.view.get_xml(model) report_file = xml.attrib['file'] if rep_type == 'xml': with open( loader.get_template(report_file).template.filename, 'rb') as f: xml = f.read() rep_type = report_file.rsplit('.', 1)[1] engine = get_engine(REPORT_ENGINES[rep_type]) fname = uuid.uuid4().hex + '.pdf' output_path = os.path.join(settings.REPORT_PATH, fname) rep = engine.export( xml, connection=ConnectionProxy(connection), # company=g.user.user_company, name=self.name, template='admin/reports/base.jinja2', company=apps['auth.user'].objects.get(pk=1).user_company, format=format, model=model, query=qs, report_title=self.name, params=params, where=where, output_file=output_path, ) if not isinstance(rep, (dict, str)): return rep if rep: if not isinstance(rep, str): rep = rep.export(format=format) out_file = '/web/reports/' + os.path.basename(rep) print('outfile', out_file) return { 'open': out_file, 'name': self.name, }
def _get_content(self, context): if self.view_type == 'report': templ = loader.get_template(self.template_name.split(':')[-1]) else: templ = loader.get_template(self.template_name.split(':')[-1]) # templ = apps.jinja_env.get_or_select_template(self.template_name.split(':')[-1]) return templ.render(context) res = open(templ.template.filename, encoding='utf-8').read() return res
def render(cls, request): from orun.template.loader import get_template from orun.contrib.admin.models.ui import exec_query, exec_scalar, ref, query ctx = cls.get_context(request) ctx['env'] = apps ctx['_'] = gettext ctx['exec_query'] = exec_query ctx['query'] = query ctx['exec_scalar'] = exec_scalar ctx['models'] = apps ctx['ref'] = ref return { 'content': get_template(cls.template_name).render(ctx), }
def to_dict(self, *args, **kwargs): from lxml import etree data = super(ReportAction, self).to_dict(*args, **kwargs) model = None if self.model: model = apps[self.model] rep_type = None if self.view and self.view.template_name: rep_type = self.view.template_name.rsplit('.', 1)[1] engine = get_engine(REPORT_ENGINES[rep_type]) if rep_type == 'jinja2': templ = loader.get_template(self.view.template_name) params = templ.blocks.get('params') if params: ctx = templ.new_context({}) doc = ''.join(params(ctx)) if not model: xml = etree.fromstring(doc) model_name = xml.attrib.get('model') if model_name: model = apps[model_name] data['fields'] = model.get_fields_info(xml) data['content'] = doc elif rep_type == 'pug': templ = loader.find_template(self.view.template_name) with open(templ, 'r', encoding='utf-8') as f: params = engine.extract_params(f.read()) if params is not None: print(params.tostring()) data['content'] = params.tostring() else: if rep_type == 'xml': templ = loader.find_template(self.view.template_name) with open(templ, 'r', encoding='utf-8') as f: xml = f.read() else: xml = self.view._get_content({}) if isinstance(xml, str): xml = etree.fromstring(xml) # xml = self.view.get_xml(model) if model: data['fields'] = model.get_fields_info(xml=xml) params = xml.find('params') if params is not None: if xml.tag == 'report' and 'model' in xml.attrib: params.attrib['model'] = xml.attrib['model'] if not model: model = apps[xml.attrib['model']] data['fields'] = model.get_fields_info(params) # model = app[model] # for field in params: # if field.tag == 'field' and 'name' in field.attrib: # fld = model._meta.fields[field.attrib['name']] xml = params data['content'] = etree.tostring( xml, encoding='utf-8').decode('utf-8') if 'content' not in data: data['content'] = '<params/>' return data