|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# © 2016 Therp BV <http://therp.nl> |
| 3 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
| 4 | +from lxml import etree |
| 5 | +from openerp import api, models, tools |
| 6 | + |
| 7 | + |
| 8 | +class UnquoteObject(str): |
| 9 | + def __getattr__(self, name): |
| 10 | + return UnquoteObject('%s.%s' % (self, name)) |
| 11 | + |
| 12 | + def __repr__(self): |
| 13 | + return self |
| 14 | + |
| 15 | + def __call__(self, *args, **kwargs): |
| 16 | + return UnquoteObject( |
| 17 | + '%s(%s)' % ( |
| 18 | + self, |
| 19 | + ','.join( |
| 20 | + [ |
| 21 | + UnquoteObject( |
| 22 | + a if not isinstance(a, basestring) |
| 23 | + else "'%s'" % a |
| 24 | + ) |
| 25 | + for a in args |
| 26 | + ] + |
| 27 | + [ |
| 28 | + '%s=%s' % (UnquoteObject(k), v) |
| 29 | + for (k, v) in kwargs.iteritems() |
| 30 | + ] |
| 31 | + ) |
| 32 | + ) |
| 33 | + ) |
| 34 | + |
| 35 | + |
| 36 | +class UnquoteEvalObjectContext(tools.misc.UnquoteEvalContext): |
| 37 | + def __missing__(self, key): |
| 38 | + return UnquoteObject(key) |
| 39 | + |
| 40 | + |
| 41 | +class IrUiView(models.Model): |
| 42 | + _inherit = 'ir.ui.view' |
| 43 | + |
| 44 | + @api.model |
| 45 | + def apply_inheritance_specs(self, source, specs_tree, inherit_id): |
| 46 | + for specs, handled_by in self._iter_inheritance_specs(specs_tree): |
| 47 | + source = handled_by(source, specs, inherit_id) |
| 48 | + return source |
| 49 | + |
| 50 | + @api.model |
| 51 | + def _iter_inheritance_specs(self, spec): |
| 52 | + if spec.tag == 'data': |
| 53 | + for child in spec: |
| 54 | + for node, handler in self._iter_inheritance_specs(child): |
| 55 | + yield node, handler |
| 56 | + return |
| 57 | + if spec.get('position') == 'attributes': |
| 58 | + for child in spec: |
| 59 | + node = etree.Element(spec.tag, **spec.attrib) |
| 60 | + node.insert(0, child) |
| 61 | + yield node, self._get_inheritance_handler_attributes( |
| 62 | + child |
| 63 | + ) |
| 64 | + return |
| 65 | + yield spec, self._get_inheritance_handler(spec) |
| 66 | + |
| 67 | + @api.model |
| 68 | + def _get_inheritance_handler(self, node): |
| 69 | + handler = super(IrUiView, self).apply_inheritance_specs |
| 70 | + if hasattr( |
| 71 | + self, 'inheritance_handler_%s' % node.tag |
| 72 | + ): |
| 73 | + handler = getattr( |
| 74 | + self, |
| 75 | + 'inheritance_handler_%s' % node.tag |
| 76 | + ) |
| 77 | + return handler |
| 78 | + |
| 79 | + @api.model |
| 80 | + def _get_inheritance_handler_attributes(self, node): |
| 81 | + handler = super(IrUiView, self).apply_inheritance_specs |
| 82 | + if hasattr( |
| 83 | + self, 'inheritance_handler_attributes_%s' % node.get('operation') |
| 84 | + ): |
| 85 | + handler = getattr( |
| 86 | + self, |
| 87 | + 'inheritance_handler_attributes_%s' % node.get('operation') |
| 88 | + ) |
| 89 | + return handler |
| 90 | + |
| 91 | + @api.model |
| 92 | + def inheritance_handler_attributes_python_dict( |
| 93 | + self, source, specs, inherit_id |
| 94 | + ): |
| 95 | + """Implement |
| 96 | + <$node position="attributes"> |
| 97 | + <attribute name="$attribute" operation="python_dict" key="$key"> |
| 98 | + $keyvalue |
| 99 | + </attribute> |
| 100 | + </$node>""" |
| 101 | + node = self.locate_node(source, specs) |
| 102 | + for attribute_node in specs: |
| 103 | + python_dict = tools.safe_eval( |
| 104 | + node.get(attribute_node.get('name')) or '{}', |
| 105 | + UnquoteEvalObjectContext() |
| 106 | + ) |
| 107 | + python_dict[attribute_node.get('key')] = UnquoteObject( |
| 108 | + attribute_node.text |
| 109 | + ) |
| 110 | + node.attrib[attribute_node.get('name')] = str(python_dict) |
| 111 | + return source |
| 112 | + |
| 113 | + @api.model |
| 114 | + def inheritance_handler_xpath(self, source, specs, inherit_id): |
| 115 | + if not specs.get('position') == 'move': |
| 116 | + return super(IrUiView, self).apply_inheritance_specs( |
| 117 | + source, specs, inherit_id |
| 118 | + ) |
| 119 | + node = self.locate_node(source, specs) |
| 120 | + target_node = self.locate_node( |
| 121 | + source, etree.Element(specs.tag, expr=specs.get('target')) |
| 122 | + ) |
| 123 | + target_node.append(node) |
| 124 | + return source |
0 commit comments