# This file is part of Tryton. The COPYRIGHT file at the top level of # this repository contains the full copyright notices and license terms. import collections from functools import wraps from lxml import etree from trytond.cache import Cache from trytond.exceptions import UserError from trytond.i18n import gettext from trytond.pool import Pool from trytond.pyson import PYSONEncoder from trytond.rpc import RPC, RPCReturnException from trytond.tools import is_instance_method, likify from trytond.transaction import Transaction, check_access, without_check_access from . import fields from .fields import on_change_result from .model import Model __all__ = ['ModelView'] class AccessButtonError(UserError): pass class ButtonActionException(RPCReturnException): "Exception to launch action instead of executing button." def __init__(self, action, value=None): super().__init__() pool = Pool() ModelData = pool.get('ir.model.data') Action = pool.get('ir.action') module, fs_id = action.split('.') action_id = Action.get_action_id( ModelData.get_id(module, fs_id)) self.value = Action(action_id).get_action_value() if value: self.value.update(value) def result(self): return self.value def set_visible(node, fields, loading='eager'): "Set loading to visible fields" if node.tag == 'group': if node.attrib.get('expandable', '1') == '1': for child in node: set_visible(child, fields, loading) elif node.tag == 'notebook': if len(node) > 0: set_visible(node[0], fields, loading) elif node.tag == 'field': if node.attrib['name'] in fields: node.attrib['loading'] = 'eager' else: for child in node: set_visible(child, fields, loading) def on_change(func): @wraps(func) def wrapper(self, *args, **kwargs): result = func(self, *args, **kwargs) assert result is None, func return self wrapper.on_change = True return wrapper class ModelView(Model): """ Define a model with views in Tryton. """ __slots__ = () _fields_view_get_cache = Cache('modelview.fields_view_get') _view_toolbar_get_cache = Cache( 'modelview.view_toolbar_get', context=False) @classmethod def __setup__(cls): super(ModelView, cls).__setup__() cls.__rpc__['fields_view_get'] = RPC(cache=dict(days=1)) cls.__rpc__['view_toolbar_get'] = RPC(cache=dict(days=1)) cls.__rpc__['on_change'] = RPC(instantiate=0, result=on_change_result) cls.__rpc__['on_change_with'] = RPC( instantiate=0, result=on_change_result) cls.__rpc__['on_change_notify'] = RPC(instantiate=0) cls.__rpc__['on_scan_code'] = RPC( instantiate=0, result=on_change_result) cls.__rpc__['autocomplete'] = RPC() cls._buttons = {} fields_ = {} for name in dir(cls): if name.startswith('__'): continue attr = getattr(cls, name) if isinstance(attr, fields.Field): fields_[name] = attr @classmethod def __post_setup__(cls): super(ModelView, cls).__post_setup__() methods = { '_done': set(), 'depends': collections.defaultdict(set), 'depend_methods': collections.defaultdict(set), 'change': collections.defaultdict(set), } cls.__change_buttons = methods['change'] def set_methods(name): if name in methods['_done']: return methods['_done'].add(name) for parent_cls in cls.__mro__: parent_meth = getattr(parent_cls, name, None) if not parent_meth: continue for attr in ['depends', 'depend_methods', 'change']: if isinstance(parent_meth, property): parent_value = getattr(parent_meth.fget, attr, set()) parent_value |= getattr(parent_meth.fset, attr, set()) else: parent_value = getattr(parent_meth, attr, set()) if parent_value: methods[attr][name] |= parent_value def setup_field(field_name, field, attribute): if attribute == 'selection_change_with': if isinstance( getattr(field, 'selection', None), str): function_name = field.selection else: return else: function_name = '%s_%s' % (attribute, field_name) function = getattr(cls, function_name, None) if not function: return set_methods(function_name) setattr(field, attribute, methods['depends'][function_name]) meth_names = list(methods['depend_methods'][function_name]) meth_done = set() while meth_names: meth_name = meth_names.pop() method = getattr(cls, meth_name) assert callable(method) or isinstance(method, property), \ "%s.%s not callable or property" % (cls, meth_name) set_methods(meth_name) setattr(field, attribute, getattr(field, attribute) | methods['depends'][meth_name]) meth_names += list( methods['depend_methods'][meth_name] - meth_done) meth_done.add(meth_name) if (attribute == 'on_change' and not getattr(function, 'on_change', None)): # Decorate on_change to always return self setattr(cls, function_name, on_change(function)) for name, field in cls._fields.items(): for attribute in [ 'on_change', 'on_change_with', 'autocomplete', 'selection_change_with', ]: setup_field(name, field, attribute) # Update __rpc__ for field_name, field in cls._fields.items(): field.set_rpc(cls) for button in cls._buttons: if not is_instance_method(cls, button): cls.__rpc__.setdefault(button, RPC(readonly=False, instantiate=0)) else: cls.__rpc__.setdefault(button, RPC(instantiate=0, result=on_change_result)) meth_names = set() meth_done = set() for parent_cls in cls.__mro__: parent_meth = getattr(parent_cls, button, None) if not parent_meth: continue cls.__change_buttons[button] |= getattr( parent_meth, 'change', set()) meth_names |= getattr(parent_meth, 'change_methods', set()) while meth_names: meth_name = meth_names.pop() method = getattr(cls, meth_name) assert callable(method) or isinstance(method, property), \ "%s.%s not callable or property" % (cls, meth_name) set_methods(meth_name) cls.__change_buttons[button] |= methods['depends'][meth_name] meth_names |= ( methods['depend_methods'][meth_name] - meth_done) meth_done.add(meth_name) for func_name, depends_attr in [ ('on_change_notify', '_on_change_notify_depends'), ('on_scan_code', '_on_scan_code_depends'), ]: set_methods(func_name) setattr(cls, depends_attr, methods['depends'][func_name]) meth_names = list(methods['depend_methods'][func_name]) meth_done = set() while meth_names: meth_name = meth_names.pop() method = getattr(cls, meth_name) assert callable(method) or isinstance(method, property), \ "%s.%s not callable or property" % (cls, meth_name) set_methods(meth_name) setattr( cls, depends_attr, getattr(cls, depends_attr) | methods['depends'][meth_name]) meth_names += list( methods['depend_methods'][meth_name] - meth_done) meth_done.add(meth_name) cls.on_scan_code = on_change(cls.on_scan_code) @classmethod def fields_view_get(cls, view_id=None, view_type='form', level=None): ''' Return a view definition. If view_id is None the first one will be used of view_type. The definition is a dictionary with keys: - model: the model name - type: the type of the view - view_id: the id of the view - arch: the xml description of the view - fields: a dictionary with the definition of each field in the view - field_childs: the name of the childs field for tree ''' pool = Pool() User = pool.get('res.user') key = (User.get_groups(), cls.__name__, view_id, view_type, level) result = cls._fields_view_get_cache.get(key) if result: return result result = {'model': cls.__name__} pool = Pool() View = pool.get('ir.ui.view') if view_id: view = View(view_id) else: domain = [ ('model', '=', cls.__name__), ['OR', ('inherit', '=', None), ('inherit.model', '!=', cls.__name__), ], ] views = View.search(domain) views = [v for v in views if v.rng_type == view_type] if views: view = views[0] view_id = view.id else: view = None # if a view was found if view: result = view.view_get(model=cls.__name__) # otherwise, build some kind of default view else: if view_type == 'form': res = cls.fields_get() xml = '''''' \ '''
''' for i in res: if i in ('create_uid', 'create_date', 'write_uid', 'write_date', 'id', 'rec_name'): continue if res[i]['type'] not in ('one2many', 'many2many'): xml += '