Beispiel #1
0
def _convert_action(action, data=None, context=None, config=None):
    if config is None:
        config = proteus.config.get_config()
    if data is None:
        data = {}
    else:
        data = data.copy()

    if 'type' not in (action or {}):
        return None

    data['action_id'] = action['id']
    if action['type'] == 'ir.action.act_window':
        from .pyson import PYSONDecoder

        action.setdefault('pyson_domain', '[]')
        ctx = {
            'active_model': data.get('model'),
            'active_id': data.get('id'),
            'active_ids': data.get('ids', []),
        }
        ctx.update(config.context)
        ctx['_user'] = config.user
        decoder = PYSONDecoder(ctx)
        action_ctx = decoder.decode(action.get('pyson_context') or '{}')
        ctx.update(action_ctx)
        ctx.update(context)
        action_ctx.update(context)
        if 'date_format' not in action_ctx:
            action_ctx['date_format'] = config.context.get(
                'locale', {}).get('date', '%x')

        ctx['context'] = ctx
        decoder = PYSONDecoder(ctx)
        domain = decoder.decode(action['pyson_domain'])

        res_model = action.get('res_model', data.get('res_model'))
        res_id = action.get('res_id', data.get('res_id'))
        Model_ = Model.get(res_model)
        with config.set_context(action_ctx):
            if res_id is None:
                return Model_.find(domain)
            else:
                return [Model_(id_) for id_ in res_id]
    elif action['type'] == 'ir.action.wizard':
        kwargs = {
            'action': action,
            'config': config,
            'context': context,
            }
        if 'model' in data:
            Model_ = Model.get(data['model'])
            kwargs['models'] = [Model_(id_) for id_ in data.get('ids', [])]
        return Wizard(action['wiz_name'], **kwargs)
    elif action['type'] == 'ir.action.report':
        ActionReport = Report(action['report_name'], context=context)
        return ActionReport.execute(data=data)
    elif action['type'] == 'ir.action.url':
        return action.get('url')
Beispiel #2
0
 def find(self, condition=None, offset=0, limit=None, order=None):
     'Returns records matching condition taking into account list domain'
     from .pyson import PYSONDecoder
     decoder = PYSONDecoder(_EvalEnvironment(self.parent))
     Relation = Model.get(self.model_name, self.parent._config)
     if condition is None:
         condition = []
     field_domain = decoder.decode(self.domain)
     add_remove_domain = (decoder.decode(self.add_remove)
         if self.add_remove else [])
     new_domain = [field_domain, add_remove_domain, condition]
     with Relation._config.set_context(self._get_context()):
         return Relation.find(new_domain, offset, limit, order)
Beispiel #3
0
 def find(self, condition=None, offset=0, limit=None, order=None):
     'Returns records matching condition taking into account list domain'
     from .pyson import PYSONDecoder
     decoder = PYSONDecoder(_EvalEnvironment(self.parent))
     Relation = Model.get(self.model_name, self.parent._config)
     if condition is None:
         condition = []
     field_domain = decoder.decode(self.domain)
     add_remove_domain = (decoder.decode(self.add_remove)
         if self.add_remove else [])
     new_domain = [field_domain, add_remove_domain, condition]
     with Relation._config.set_context(self._get_context()):
         return Relation.find(new_domain, offset, limit, order)
Beispiel #4
0
 def __get__(self, instance, owner):
     from .pyson import PYSONDecoder
     relation = Model.get(self.definition['relation'], instance._config)
     value = super(One2ManyDescriptor, self).__get__(instance, owner)
     if not isinstance(value, ModelList):
         ctx = instance._context.copy() if instance._context else {}
         if self.definition.get('context'):
             decoder = PYSONDecoder(_EvalEnvironment(instance))
             ctx.update(decoder.decode(self.definition.get('context')))
         with instance._config.set_context(ctx):
             value = ModelList(self.definition, (relation(id)
                     for id in value or []), instance, self.name)
         instance._values[self.name] = value
     return value
Beispiel #5
0
 def __get__(self, instance, owner):
     from .pyson import PYSONDecoder
     relation = Model.get(self.definition['relation'], instance._config)
     value = super(One2ManyDescriptor, self).__get__(instance, owner)
     if not isinstance(value, ModelList):
         ctx = instance._context.copy() if instance._context else {}
         if self.definition.get('context'):
             decoder = PYSONDecoder(_EvalEnvironment(instance))
             ctx.update(decoder.decode(self.definition.get('context')))
         with instance._config.set_context(ctx):
             value = ModelList(self.definition, (relation(id)
                     for id in value or []), instance, self.name)
         instance._values[self.name] = value
     return value
Beispiel #6
0
 def __get__(self, instance, owner):
     from .pyson import PYSONDecoder
     Relation = Model.get(self.definition['relation'], instance._config)
     value = super(One2ManyDescriptor, self).__get__(instance, owner)
     if not isinstance(value, ModelList):
         ctx = instance._context.copy() if instance._context else {}
         if self.definition.get('context'):
             decoder = PYSONDecoder(_EvalEnvironment(instance))
             ctx.update(decoder.decode(self.definition.get('context')))
         config = Relation._config
         with config.reset_context(), config.set_context(ctx):
             # JCA : Instantiate function O2M, which are read as dicts
             # rather than ids
             value = ModelList(
                 self.definition,
                 (Relation(**id) if isinstance(id, dict) else Relation(id)
                  for id in value or []), instance, self.name)
         instance._values[self.name] = value
     return value
Beispiel #7
0
    def _on_change(self, names):
        'Call on_change for field'
        # Import locally to not break installation
        from proteus.pyson import PYSONDecoder

        values = {}
        for name in names:
            definition = self._fields[name]
            on_change = definition.get('on_change')
            if not on_change:
                continue
            if isinstance(on_change, str):
                definition['on_change'] = on_change = PYSONDecoder().decode(
                    on_change)
            values.update(self._on_change_args(on_change))
        if values:
            context = self._context
            changes = getattr(self._proxy, 'on_change')(values, names, context)
            for change in changes:
                self._set_on_change(change)

        values = {}
        fieldnames = set(names)
        to_change = set()
        later = set()
        for field, definition in self._fields.items():
            on_change_with = definition.get('on_change_with')
            if not on_change_with:
                continue
            if not fieldnames & set(on_change_with):
                continue
            if to_change & set(on_change_with):
                later.add(field)
                continue
            to_change.add(field)
            values.update(self._on_change_args(on_change_with))
        if to_change:
            context = self._context
            changes = getattr(self._proxy, 'on_change_with')(values,
                                                             list(to_change),
                                                             context)
            self._set_on_change(changes)
        for field in later:
            on_change_with = self._fields[field]['on_change_with']
            values = self._on_change_args(on_change_with)
            context = self._context
            result = getattr(self._proxy, 'on_change_with_%s' % field)(values,
                                                                       context)
            self._on_change_set(field, result)

        if self._parent:
            self._parent._changed.add(self._parent_field_name)
            self._parent._on_change([self._parent_field_name])
Beispiel #8
0
def _convert_action(action, data=None, context=None, config=None):
    if config is None:
        config = proteus.config.get_config()
    if data is None:
        data = {}
    else:
        data = data.copy()

    if 'type' not in (action or {}):
        return None

    data['action_id'] = action['id']
    if action['type'] == 'ir.action.act_window':
        from .pyson import PYSONDecoder

        action.setdefault('pyson_domain', '[]')
        ctx = {
            'active_model': data.get('model'),
            'active_id': data.get('id'),
            'active_ids': data.get('ids', []),
        }
        ctx.update(config.context)
        ctx['_user'] = config.user
        decoder = PYSONDecoder(ctx)
        action_ctx = decoder.decode(action.get('pyson_context') or '{}')
        ctx.update(action_ctx)
        ctx.update(context)
        action_ctx.update(context)
        if 'date_format' not in action_ctx:
            action_ctx['date_format'] = config.context.get(
                'locale', {}).get('date', '%x')

        ctx['context'] = ctx
        decoder = PYSONDecoder(ctx)
        domain = decoder.decode(action['pyson_domain'])

        res_model = action.get('res_model', data.get('res_model'))
        res_id = action.get('res_id', data.get('res_id'))
        Model_ = Model.get(res_model)
        with config.set_context(action_ctx):
            if res_id is None:
                return Model_.find(domain)
            else:
                return [Model_(id_) for id_ in res_id]
    elif action['type'] == 'ir.action.wizard':
        kwargs = {
            'action': action,
            'config': config,
            'context': context,
            }
        if 'model' in data:
            Model_ = Model.get(data['model'])
            kwargs['models'] = [Model_(id_) for id_ in data.get('ids', [])]
        return Wizard(action['wiz_name'], **kwargs)
    elif action['type'] == 'ir.action.report':
        ActionReport = Report(action['report_name'], context=context)
        return ActionReport.execute(data=data)
    elif action['type'] == 'ir.action.url':
        return action.get('url')
Beispiel #9
0
 def _get_context(self):
     from .pyson import PYSONDecoder
     decoder = PYSONDecoder(_EvalEnvironment(self.parent))
     ctx = self.parent._context.copy() if self.parent._context else {}
     ctx.update(decoder.decode(self.context) if self.context else {})
     return ctx
Beispiel #10
0
 def _get_context(self):
     from .pyson import PYSONDecoder
     decoder = PYSONDecoder(_EvalEnvironment(self.parent))
     ctx = self.parent._context.copy() if self.parent._context else {}
     ctx.update(decoder.decode(self.context) if self.context else {})
     return ctx