覆盖OpenERP约束

时间:2013-04-18 16:34:24

标签: python openerp

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

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

我找到了hr.attendance constraint

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

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

1 个答案:

答案 0 :(得分:3)

我找到了一个bug on launchpad,它描述了我遇到的问题和解决方法。事实证明,你现在可以覆盖约束,但它非常繁琐。您必须在相同字段上声明自己的约束,并使用相同的函数名称作为基本模块。然后,只有这样它才会调用你的约束而不是基本版本。

此处my module会覆盖操作的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'])]