Help

2

How to override action_approve method?

Avatar
Muhammad Anees

Hii,

I have added one extra level of approval that is substitute employee when any employee creates leave request in Leaves App in statusbar.

state = fields.Selection([
        ('draft', 'To Submit'),
('cancel', 'Cancelled'),
('confirm', 'To Approve'),
('refuse', 'Refused'),
('validate_substitute', 'Substitute Approval'), # Additional level of approval, customization
('validate1', 'Second Approval'),
('validate', 'Approved')])

After substitute employee approval, when clicking on Approve button getting following error..

Leave request must be confirmed ("To Approve") in order to approve it.

This error message is coming from following method in hr_leave.py class.... 

@api.multi
    def action_approve(self):
# if validation_type == 'both': this method is the first approval approval
# if validation_type != 'both': this method calls action_validate() below
if any(holiday.state != 'confirm' for holiday in self):
raise UserError(_('Leave request must be confirmed ("To Approve") in order to approve it.'))
current_employee = self.env['hr.employee'].search([('user_id', '=', self.env.uid)], limit=1)
self.filtered(lambda hol: hol.validation_type == 'both').write({'state': 'validate1', 'first_approver_id': current_employee.id})
self.filtered(lambda hol: not hol.validation_type == 'both').action_validate()
if not self.env.context.get('leave_fast_create'):
self.activity_update()
return True

In this method I want to change 'confirm' to 'validate_substitute' like following

if any(holiday.state != 'validate_substitute' for holiday in self):

How I can achieve it in method overriding? 
Or should I write it complete in my inherited class?

Thanks,

Avatar
Discard
3 Answers
2
Avatar
Niyas Raphy
Best Answer

Hi,

There are two things you can do,

1. To rewrite the entire function by inheriting the corresponding model in your custom module. For this in the python file, inherit the corresponding model and copy the function into it and make necessary changes where ever you needed.


2. This method is to super the original function so that the both the function will get executed. For supering/overriding the existing function, see this:  Overriding method in a model


Example:

class HRLeave(models.Model):
_inherit = 'hr.leave'

@api.multi
def action_approve(self):
res = super(HRLeave, self).action_approve()
# add custom codes here
return res

In this the second method is the recommend one.

Thanks

4 Comments
Avatar
Discard
Avatar
Muhammad Anees
-

Hii @Niyas Raphy.

Thanks for the reply

Here is my code in inherited calss

class HrLeave(models.Model):

_inherit = 'hr.leave'

@api.multi

def action_approve(self):

record = super(HrLeave, self).action_approve()

record['state'] = 'confirm'

logging.warning("=======Called action_approve Method========")

return record

But it is never called...

logging.warning is not printed on terminal

record['state'] = 'confirm' is not set

What I am missing?

Thanks,

Avatar
Niyas Raphy
-

make sure that the python file is called in init file and also make sure that the module contain this code is installed

Avatar
Muhammad Anees
-

Yes, the file is being called in the init. There are some other methods on the file working fine. Module containing this code is also installed.

Avatar
Niyas Raphy
-

This has to be worked, i think debug the code from your side and see.

2
Avatar
Muhammad Anees
Best Answer

Its working

@api.multi
def action_confirm(self): record = super(HrLeave, self).action_confirm() if self.message_main_attachment_id.id is False and self.holiday_status_id.time_type == 'full_day_training': raise ValidationError("Please attach document for leave type: %s" % self.holiday_status_id.name) return record


Avatar
Discard
0
Best Answer

Hello, 

For any method override, you can use a super method.

    @api.multi
def action_approve(self):
res = super(Class Name, self).action_approve()
# add your condition
return res


Avatar
Discard