继承hr.holidays类并覆盖Odoo v9

时间:2016-07-14 11:30:22

标签: python oop inheritance openerp

我在Odoo v9上创建了一个自定义应用程序(模块),它在我的模块模型中继承hr.holidays(leaves module),并覆盖create()方法和_check_state_access_right()方法。想在我的模块中修改_check_state_access_right()

现有的叶子模块基类 - ( hr_holidays.py

class hr_holidays(osv.osv):
    _name = "hr.holidays"
    _description = "Leave"
    _order = "type desc, date_from desc"
    _inherit = ['mail.thread', 'ir.needaction_mixin']


    def _check_state_access_right(self, cr, uid, vals, context=None):
        if vals.get('state') and vals['state'] not in ['draft', 'confirm', 'cancel'] and not self.pool['res.users'].has_group(cr, uid, 'base.group_hr_user'):
            return False
        return True

    def create(self, cr, uid, values, context=None):
        """ Override to avoid automatic logging of creation """
        if context is None:
            context = {}
        employee_id = values.get('employee_id', False)
        context = dict(context, mail_create_nolog=True, mail_create_nosubscribe=True)
        if not self._check_state_access_right(cr, uid, values, context):
            raise AccessError(_('You cannot set a leave request as \'%s\'. Contact a human resource manager.') % values.get('state'))
        if not values.get('name'):
            employee_name = self.pool['hr.employee'].browse(cr, uid, employee_id, context=context).name
            holiday_type = self.pool['hr.holidays.status'].browse(cr, uid, values.get('holiday_status_id'), context=context).name
            values['name'] = _("%s on %s") % (employee_name, holiday_type)
        hr_holiday_id = super(hr_holidays, self).create(cr, uid, values, context=context)
        self.add_follower(cr, uid, [hr_holiday_id], employee_id, context=context)
        return hr_holiday_id

当我覆盖create方法时,它调用super(hr_holidays,self).create(cr,uid, values,context=context)调用leaves模块的基类的create方法,然后再次转到基类的_check_state_access_right()方法,我的功能失败。请查看代码并建议我是否可以在不调用super(hr_holidays, self)的情况下创建记录。

class HrHolidaysCustom(osv.osv):
    _name = 'hr.holidays'
    _inherit= 'hr.holidays' 

    def _check_state_access_right_new(self, vals):
        if vals.get('state') and vals['state'] not in ['draft', 'doa', 'reroute', 'confirm', 'cancel'] and not self.pool['res.users'].has_group(request.cr, request.uid, 'base.group_hr_user') : #
            return False
        return True 

    @api.model
    def create(self, values):
        """ Override to add states in method _check_state_access_rights() """
        cr, uid, context = request.cr, request.uid, request.context
        if context is None:
            context = {}
        employee_id = values.get('employee_id', False)
        context = dict(context, mail_create_nolog=True, mail_create_nosubscribe=True)
        if not self._check_state_access_right_new(values):
            print self._check_state_access_right_new(values)
            raise AccessError(_('You cannot set a leave request as \'%s\'. Contact a human resource manager.') % values.get('state'))
        if not values.get('name'):
            employee_name = self.pool['hr.employee'].browse(cr, uid, employee_id, context=context).name
            holiday_type = self.pool['hr.holidays.status'].browse(cr, uid, values.get('holiday_status_id'), context=context).name            
            values['name'] = ("%s on %s") % (employee_name, holiday_type)
        hr_holiday_id = super(HrHolidaysCustom, self).create(values)
        return hr_holiday_id

最后,我想在_check_state_access_rights方法中添加另外两个状态,方法是对我自定义模块进行更改,而不是对现有离开模块的代码进行任何更改。

1 个答案:

答案 0 :(得分:0)

摆脱_name = 'hr.holidays',如果您只是想使用_inhereit='hr.holidays'覆盖或添加属性,那么

要正确覆盖类中的新方法,方法名称必须相同,因此在HrHolidaysCustom类中将方法定义更改为

 def _check_state_access_right(self, vals):
        if vals.get('state') and vals['state'] not in ['draft', 'doa', 'reroute', 'confirm', 'cancel'] and not self.pool['res.users'].has_group(request.cr, request.uid, 'base.group_hr_user') : #
            return False
        return True

从现在开始,将调用新方法代替您正在扩展的旧方法