2016-07-14 42 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方法,而不是調用我們創建和定製的方法。等待您的寶貴迴應。謝謝 –