2016-07-14 61 views
0

我已经创建了一个Odoo v9自定义应用程序(模块),在我的模块的机型继承了hr.holidays(leaves module),并覆盖create()方法也_check_state_access_right()方法创建方法,我想修改_check_state_access_right()在我的模块中。继承hr.holidays类和压倒一切的Odoo V9

现有的树叶模块的基类 - (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 

当我重写创建方法调用super(hr_holidays,self).create(cr,uid, values,context=context)调用基类叶子模块的创建方法,然后再转到_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方法添加两个国家。

回答

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 

从现在开始,新的方法来更改将在地方,你正在扩大

+0

是的,但随后也它也没有创造纪录,而无需调用t时的旧的被称为他基础hr.holidays类。我想要继承的类在本地创建记录。因为无论何时调用基类,它都会转到_check_state_access_rights方法,然后我的覆盖变得毫无价值。 –

+0

哦,好的...看我的编辑 – danidee

+0

谢谢。为了您的回应。但是由于_check_state_access_rights是一个私有方法,它不会从基类的create方法中调用。基类的create方法将调用它自己的_check_state_access_rights方法,而不是调用我们创建和定制的方法。等待您的宝贵回应。谢谢 –