def _merge(self, target: etree.HtmlElement, pos: str, element: etree.HtmlElement): if pos == 'append': for child in element: target.append(child) elif pos == 'insert': for child in reversed(element): target.insert(0, etree.fromstring(etree.tostring(child))) elif pos == 'before': parent = target.getparent() idx = parent.index(target) for child in reversed(element): parent.insert(idx, etree.fromstring(etree.tostring(child))) elif pos == 'after': parent = target.getparent() idx = parent.index(target) + 1 for child in reversed(element): parent.insert(idx, etree.fromstring(etree.tostring(child))) elif pos == 'attributes': for child in element: target.attrib[child.attrib['name']] = child.text elif pos == 'replace': p = target.getparent() idx = p.index(target) p.remove(target) for child in element: p.insert(idx, etree.fromstring(etree.tostring(child)))
def xpath(self, source, element): pos = element.attrib.get('position') expr = element.attrib.get('expr') target = source if expr: target = target.xpath(expr)[0] if pos == 'append': for child in element: target.append(etree.fromstring(etree.tostring(child))) elif pos == 'insert': for child in reversed(element): target.insert(0, etree.fromstring(etree.tostring(child))) elif pos == 'before': parent = target.getparent() idx = parent.index(target) for child in reversed(element): parent.insert(idx, etree.fromstring(etree.tostring(child))) elif pos == 'after': parent = target.getparent() idx = parent.index(target) + 1 for child in reversed(element): parent.insert(idx, etree.fromstring(etree.tostring(child))) elif pos == 'attributes': for child in element: target.attrib[child.attrib['name']] = child.text elif pos == 'replace': p = target.getparent() idx = p.index(target) p.remove(target) for child in element: p.insert(idx, etree.fromstring(etree.tostring(child)))
def get_view_info(self, view_type, view=None, toolbar=False): View = app['ui.view'] model = app['ir.model'] if view is None: view = list(View.objects.filter(mode='primary', view_type=view_type, model=self._meta.name)) if view: view = view[0] elif isinstance(view, (int, str)): view = View.objects.get(view) if view: xml_content = view.get_xml(model=self) r = { 'content': etree.tostring(xml_content, encoding='utf-8').decode('utf-8'), 'fields': self.get_fields_info(view_type=view_type, xml=xml_content) } else: content = self._get_default_view(view_type=view_type) r = { 'content': content, 'fields': self.get_fields_info(view_type=view_type, xml=content), } if toolbar and view_type != 'search': bindings = app['ir.action'].get_bindings(self._meta.name) r['toolbar'] = { 'print': [action.serialize() for action in bindings['print'] if view_type == 'list' or not action.multiple], 'action': [action.serialize() for action in bindings['action'] if view_type == 'list' or not action.multiple], } return r
def xpath(self, source, element): pos = element.attrib.get('position') expr = element.attrib.get('expr') target = source logger.critical('xpath %s %s' % (expr, self.template_name)) logger.critical(etree.tostring(element)) if expr: target = target.xpath(expr)[0] self._merge(target, pos, element)
def serialize(self, *args, **kwargs): data = super(ReportAction, self).serialize(*args, **kwargs) model = None if self.model: model = app[self.model] rep_type = None if self.view and self.view.template_name: rep_type = self.view.template_name.rsplit('.', 1)[1] if rep_type == 'jinja2': templ = app.report_env.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 = app[model_name] data['fields'] = model.get_fields_info(xml) data['content'] = doc if rep_type != 'jinja2': 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 = app[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') else: data['content'] = '<params/>' return data
def _admin_get_view_info(cls, view_type, view=None, toolbar=False): View = apps['ui.view'] model = apps['content.type'] if view is None: view = View.objects.filter(mode='primary', view_type=view_type, model=cls._meta.name).first() elif isinstance(view, (int, str)): view = View.objects.get(pk=view) if view: xml_content = view.get_xml(cls, { 'request': cls.env.request, 'opts': model._meta }) r = { 'template': etree.tostring(xml_content, encoding='utf-8').decode('utf-8'), 'fields': cls.admin_get_fields_info(view_type=view_type, xml=xml_content) } else: content = cls._admin_get_default_view(view_type=view_type) r = { 'template': content, 'fields': cls.admin_get_fields_info(view_type=view_type, xml=content), } if toolbar and view_type != 'search': bindings = apps['ui.action'].get_bindings(cls._meta.name) r['toolbar'] = { 'print': [ action.to_dict() for action in bindings['print'] if view_type == 'list' or not action.multiple ], 'action': [ action.to_dict() for action in bindings['action'] if view_type == 'list' or not action.multiple ], } return r
def get_content(self, model=None): xml = etree.tostring(self.get_xml(model)) return xml
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
def get_content(self, model): return etree.tostring(self.get_xml(model))