我想编写一个模块来覆盖模型对象上的一个约束,但仅仅覆盖约束方法是行不通的。我对方法的覆盖永远不会被调用,因为 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
我也尝试在同一个字段上添加我自己的约束,但随后它调用了两个版本,并且基本模块的约束可能会拒绝保存。