def _build_python_domain(self, cr, uid, trigger, domain, context):
        """Updated filters based on old or dynamic values, or Python operators"""
        domain = domain or []
        operator_obj = self.pool.get('sartre.operator')
        old_values = context.get('old_values', {})
        arg_values = context.get('arg_values', {})
        model_obj = self.pool.get(trigger.model_id.model)
        if 'active_object_ids' in context:
            all_active_object_ids = context['active_object_ids']
        else:
            all_active_object_ids = model_obj.search(cr, uid, [], context=context)
        fields_list = self.get_fields_to_save_old_values(cr, SUPERUSER_ID, [trigger.id])
        current_values = _get_browse_record_dict(model_obj, cr, uid, all_active_object_ids, fields_list, context=context)
        for index, condition in enumerate(domain):
            if self._is_dynamic_filter(cr, uid, condition, model_obj, context):
                active_object_ids = all_active_object_ids[:]

                field, operator_symbol, other_value = condition
                fields_list = field.replace('OLD_', '').split('.')
                field, remote_field = fields_list[0], len(fields_list) > 1 and '.'.join(fields_list[1:]) or ''
                try:
                    operator_inst, opposite_operator = operator_obj._get_operator(cr, uid, operator_symbol)
                except Exception:
                    raise orm.except_orm(_('Warning!'), _("The operator %s doesn't exist!") % operator_symbol)
                dynamic_other_value = other_value and re.match('(\[\[.+?\]\])', str(other_value))
                for object_ in self.pool.get(trigger.model_id.model).browse(cr, uid, active_object_ids, context):
                    if dynamic_other_value:
                        other_value = _get_id_from_browse_record(eval(str(dynamic_other_value.group()[2:-2]).strip(), {
                            'uid': uid,
                            'object': object_,
                            'context': context,
                            'time': time,
                            'relativedelta': relativedelta,
                        }))
                    current_field_value = current_values.get(object_.id, {}).get(field)
                    old_field_value = old_values.get(object_.id, {}).get(field)
                    arg_value = arg_values.get(field)
                    if remote_field:
                        current_field_value = _get_id_from_browse_record(getattr(current_field_value, remote_field))
                        old_field_value = _get_id_from_browse_record(old_field_value and getattr(old_field_value, remote_field))
                    localdict = {'selected_field_value': 'OLD_' in condition[0] and old_field_value or current_field_value,
                                 'current_field_value': current_field_value,
                                 'old_field_value': old_field_value,
                                 'other_value': other_value,
                                 'arg_value': arg_value}
                    if operator_inst:
                        exec operator_inst.expression in localdict
                    if bool(opposite_operator) == bool(localdict.get('result', opposite_operator)):
                        active_object_ids.remove(object_.id)
                domain[index] = ('id', 'in', active_object_ids)
        return domain
 def trigger_method(*args, **kwargs):
     # Get arguments
     obj, cr, uid, ids, field_name, vals, context = _get_args(
         original_method, args, kwargs)
     method_name = _get_original_method_name(original_method)
     context['trigger'] = method_name
     trigger_obj = obj.pool.get('sartre.trigger')
     trigger_ids = []
     if trigger_obj \
             and (method_name != 'write' or vals):  # To avoid to execute action if write({})
         # Case: trigger on function
         calculation_method = False
         if method_name in (
                 'get',
                 'set') and original_method.im_class == fields.function:
             calculation_method = method_name
             method_name = 'function'
         # Search triggers
         trigger_ids = trigger_obj.check_method_based_triggers(
             obj, cr, uid, method_name, field_name, calculation_method)
         # Save old values if triggers exist
         if trigger_ids:
             fields_list = trigger_obj.get_fields_to_save_old_values(
                 cr, 1, trigger_ids)
             context.update({
                 'active_object_ids':
                 ids,
                 'old_values':
                 _get_browse_record_dict(obj, cr, uid, ids, fields_list,
                                         context),
                 'arg_values':
                 vals,
             })
             # Case: trigger on unlink
             if method_name == 'unlink':
                 trigger_obj.run_now(cr, uid, trigger_ids, context=context)
     # Execute original method
     result = original_method(*args, **kwargs)
     # Run triggers if exists
     if trigger_obj and trigger_ids and method_name != 'unlink':
         # Case: trigger on create
         if method_name == 'create':
             context['active_object_ids'] = [result]
         trigger_obj.run_now(cr, uid, trigger_ids, context=context)
     return result
Ejemplo n.º 3
0
 def trigger_method(*args, **kwargs):
     # Get arguments
     obj, cr, uid, ids, field_name, vals, context = _get_args(original_method, args, kwargs)
     method_name = _get_original_method_name(original_method)
     context["trigger"] = method_name
     trigger_obj = obj.pool.get("sartre.trigger")
     trigger_ids = []
     if trigger_obj and (method_name != "write" or vals):  # To avoid to execute action if write({})
         # Case: trigger on function
         calculation_method = False
         if method_name in ("get", "set") and original_method.im_class == fields.function:
             calculation_method = method_name
             method_name = "function"
         # Search triggers
         trigger_ids = trigger_obj.check_method_based_triggers(
             obj, cr, uid, method_name, field_name, calculation_method
         )
         # Save old values if triggers exist
         if trigger_ids:
             fields_list = trigger_obj.get_fields_to_save_old_values(cr, 1, trigger_ids)
             context.update(
                 {
                     "active_object_ids": ids,
                     "old_values": _get_browse_record_dict(obj, cr, uid, ids, fields_list, context),
                     "arg_values": vals,
                 }
             )
             # Case: trigger on unlink
             if method_name == "unlink":
                 trigger_obj.run_now(cr, uid, trigger_ids, context=context)
     # Execute original method
     result = original_method(*args, **kwargs)
     # Run triggers if exists
     if trigger_obj and trigger_ids and method_name != "unlink":
         # Case: trigger on create
         if method_name == "create":
             context["active_object_ids"] = [result]
         trigger_obj.run_now(cr, uid, trigger_ids, context=context)
     return result
Ejemplo n.º 4
0
    def _build_python_domain(self, cr, uid, trigger, domain, context):
        """Updated filters based on old or dynamic values, or Python operators"""
        domain = domain or []
        operator_obj = self.pool.get('sartre.operator')
        old_values = context.get('old_values', {})
        arg_values = context.get('arg_values', {})
        model_obj = self.pool.get(trigger.model_id.model)
        if 'active_object_ids' in context:
            all_active_object_ids = context['active_object_ids']
        else:
            all_active_object_ids = model_obj.search(cr,
                                                     uid, [],
                                                     context=context)
        fields_list = self.get_fields_to_save_old_values(
            cr, SUPERUSER_ID, [trigger.id])
        current_values = _get_browse_record_dict(model_obj,
                                                 cr,
                                                 uid,
                                                 all_active_object_ids,
                                                 fields_list,
                                                 context=context)
        for index, condition in enumerate(domain):
            if self._is_dynamic_filter(cr, uid, condition, model_obj, context):
                active_object_ids = all_active_object_ids[:]

                field, operator_symbol, other_value = condition
                fields_list = field.replace('OLD_', '').split('.')
                field, remote_field = fields_list[0], len(
                    fields_list) > 1 and '.'.join(fields_list[1:]) or ''
                try:
                    operator_inst, opposite_operator = operator_obj._get_operator(
                        cr, uid, operator_symbol)
                except Exception:
                    raise orm.except_orm(
                        _('Warning!'),
                        _("The operator %s doesn't exist!") % operator_symbol)
                dynamic_other_value = other_value and re.match(
                    '(\[\[.+?\]\])', str(other_value))
                for object_ in self.pool.get(trigger.model_id.model).browse(
                        cr, uid, active_object_ids, context):
                    if dynamic_other_value:
                        other_value = _get_id_from_browse_record(
                            eval(
                                str(dynamic_other_value.group()[2:-2]).strip(),
                                {
                                    'uid': uid,
                                    'object': object_,
                                    'context': context,
                                    'time': time,
                                    'relativedelta': relativedelta,
                                }))
                    current_field_value = current_values.get(object_.id,
                                                             {}).get(field)
                    old_field_value = old_values.get(object_.id, {}).get(field)
                    arg_value = arg_values.get(field)
                    if remote_field:
                        current_field_value = _get_id_from_browse_record(
                            getattr(current_field_value, remote_field))
                        old_field_value = _get_id_from_browse_record(
                            old_field_value
                            and getattr(old_field_value, remote_field))
                    localdict = {
                        'selected_field_value':
                        'OLD_' in condition[0] and old_field_value
                        or current_field_value,
                        'current_field_value':
                        current_field_value,
                        'old_field_value':
                        old_field_value,
                        'other_value':
                        other_value,
                        'arg_value':
                        arg_value
                    }
                    if operator_inst:
                        exec operator_inst.expression in localdict
                    if bool(opposite_operator) == bool(
                            localdict.get('result', opposite_operator)):
                        active_object_ids.remove(object_.id)
                domain[index] = ('id', 'in', active_object_ids)
        return domain