I have created a custom applications (module) on Odoo v9
, that inherits hr.holidays(leaves module)
in models of my module, and overrides create()
method and also _check_state_access_right()
method as I want to modify _check_state_access_right()
in my module.
Existing leaves module base class - (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
When I override create method it calls super(hr_holidays,self).create(cr,uid, values,context=context)
that calls the create method of the base class of leaves module and then again goes to _check_state_access_right()
method of base class and my functionality fails. Please have a look to the code and suggest me whether I can create records without calling 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
Ultimately I want to add two more states in the _check_state_access_rights
method by making changes in the custom module I made rather than making any changes in the code of existing leave module.