1

我想编写一个模块来覆盖模型对象上的一个约束,但仅仅覆盖约束方法是行不通的。我对方法的覆盖永远不会被调用,因为 OpenERP 使用它自己的继承机制。

我正在尝试在模块中使有关登录/退出操作的规则更加灵活,hr_timesheet_sheet以便员工可以在今天登录后记录前一天的工作时间。为此,我想覆盖字段hr.attendance上的约束action并允许任何更改,然后在时间表级别执行检查,以确保所有出勤操作都符合逻辑顺序。

我找到了hr.attendance约束

def _altern_si_so(self, cr, uid, ids, context=None):
    """ Alternance sign_in/sign_out check.
        Previous (if exists) must be of opposite action.
        Next (if exists) must be of opposite action.
    """
    for att in self.browse(cr, uid, ids, context=context):
        # search and browse for first previous and first next records
        prev_att_ids = self.search(cr, uid, [('employee_id', '=', att.employee_id.id), ('name', '<', att.name), ('action', 'in', ('sign_in', 'sign_out'))], limit=1, order='name DESC')
        next_add_ids = self.search(cr, uid, [('employee_id', '=', att.employee_id.id), ('name', '>', att.name), ('action', 'in', ('sign_in', 'sign_out'))], limit=1, order='name ASC')
        prev_atts = self.browse(cr, uid, prev_att_ids, context=context)
        next_atts = self.browse(cr, uid, next_add_ids, context=context)
        # check for alternance, return False if at least one condition is not satisfied
        if prev_atts and prev_atts[0].action == att.action: # previous exists and is same action
            return False
        if next_atts and next_atts[0].action == att.action: # next exists and is same action
            return False
        if (not prev_atts) and (not next_atts) and att.action != 'sign_in': # first attendance must be sign_in
            return False
    return True

_constraints = [(_altern_si_so, 'Error: Sign in (resp. Sign out) must follow Sign out (resp. Sign in)', ['action'])]

我尝试在我的模块中覆盖约束方法,但我的版本从未被调用。

def _altern_si_so(self, cr, uid, ids):
    """ Implementing this logic at the attendance level doesn't work, so
    we skip it, and check at the whole time sheet level. 's all good!"""
    return True

我也尝试在同一个字段上添加我自己的约束,但随后它调用了两个版本,并且基本模块的约束可能会拒绝保存。

4

1 回答 1

3

在启动板上发现了一个错误,描述了我遇到的问题和修复。事实证明,您现在可以覆盖约束,但这非常繁琐。您必须在与基本模块具有相同函数名称的同一字段上声明自己的约束。然后,只有这样,它才会调用您的约束而不是基本版本。

这是我的模块,它覆盖了hr_attendance对操作的约束并允许任意组合。

class hr_attendance(osv.osv):
    _inherit = 'hr.attendance'

    def _altern_si_so(self, cr, uid, ids):
        """ Implementing this logic at the attendance level doesn't work, so
        we skip it, and check at the whole time sheet level. 's all good!"""
        return True

    _constraints = [(_altern_si_so, 
                     'Error: You should never see this message.', 
                     ['action'])]
于 2013-04-18T16:34:23.550 回答