This question has been flagged
1 Reply
4532 Views

Dears,

I want to change the following create method behavior (The code part I want to change is in bold) :

class hr_evaluation_interview(osv.Model): 
_name = 'hr.evaluation.interview'
_inherit = 'mail.thread'
_rec_name = 'user_to_review_id'
_description = 'Appraisal Interview'
def create(self, cr, uid, vals, context=None):
    phase_obj = self.pool.get('hr_evaluation.plan.phase')
    survey_id = phase_obj.read(cr, uid, vals.get('phase_id'), fields=['survey_id'], context=context)['survey_id'][0]
    if vals.get('user_id'):
        user_obj = self.pool.get('res.users')
        partner_id = user_obj.read(cr, uid, vals.get('user_id'), fields=['partner_id'], context=context)['partner_id'][0]

    else:
        partner_id = None
    user_input_obj = self.pool.get('survey.user_input')
    if not vals.get('deadline'):
        vals['deadline'] = (datetime.now() + timedelta(days=28)).strftime(DF)
        ret = user_input_obj.create(cr, uid, {'survey_id': survey_id,
            'deadline': vals.get('deadline'),
            'type': 'link',
            'partner_id': partner_id}, context=context)
         vals['request_id'] = ret
    return super(hr_evaluation_interview, self).create(cr, uid, vals, context=context)

It's the hr_evaluation_interview create method, I want when the interview request is created, the partner_id field receive a different value than it receives now.

The partner_id receives the user_id (user related to the hr evaluator), I want it to be the evaluated employee (interviewd_id).

So I did the following using inheritance (changed code is in bold) :

class HrEvaluationInterview(osv.Model): 
_inherit = 'hr.evaluation.interview'
_columns = {
'interviewd_id': fields.many2one('hr.employee','Employee evaluated'),
}
def create(self, cr, uid, vals, context=None):
    phase_obj = self.pool.get('hr_evaluation.plan.phase')
    survey_id = phase_obj.read(cr, uid, vals.get('phase_id'), fields=['survey_id'], context=context)['survey_id'][0]
    employee_obj = self.pool.get('hr.employee')
    us_id = employee_obj.read(cr, uid, vals.get('interviewd_id'), fields=['user_id'], context=context)['user_id'][0]
    if vals.get('user_id'):
        user_obj = self.pool.get('res.users')
        partner_id = user_obj.read(cr, uid, us_id, fields=['partner_id'], context=context)['partner_id'][0]

    else:
        partner_id = None
        user_input_obj = self.pool.get('survey.user_input')
    if not vals.get('deadline'):
        vals['deadline'] = (datetime.now() + timedelta(days=28)).strftime(DF)
    ret = user_input_obj.create(cr, uid, {'survey_id': survey_id,
        'deadline': vals.get('deadline'),
        'type': 'link',
        'partner_id': partner_id}, context=context)
    vals['request_id'] = ret
    return super(HrEvaluationInterview, self).create(cr, uid, vals, context=context)

The problem is that the behavior didn't change, it keeps doing the same things did by the parent method. Maybe it's because the return I'm doing, can you advise me something ?

Thanks a lot.

Avatar
Discard
Best Answer

Hi @Yassine TEIMI

You need to bypass the super method in your return by changing this:

return super(HrEvaluationInterview, self).create(cr, uid, vals, context=context)

to this:

return osv.Model.create(self, cr, uid, vals, context=context)

This have the drawback that it will skip all the next create method overrides that could be executed by the super call but it will work

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

A better way to do the changes that you need is like:

class HrEvaluationInterview(osv.Model): 
_inherit = 'hr.evaluation.interview'
_columns = {
'interviewd_id': fields.many2one('hr.employee','Employee evaluated'),
}

def create(self, cr, uid, vals, context=None): 
us_id = self.pool.get('hr.employee').read(cr, uid, vals.get('interviewd_id'), fields=['user_id'], context=context)['user_id'][0]
vals['user_id'] = us_id
return super(HrEvaluationInterview, self).create(cr, uid, vals, context=context)

The approach is to change the vals['user_id'] so the code of original create method get another partner based on the changed user_id. With your old code you left the user_id in vals intact and that can lead to others errors, but if what you need to do is to have the same user_id but a different partner_id then left that as is. This is just another solution

Avatar
Discard
Author

Indeed that's what I was looking for, the create method first override is now replaced correctly. it creates just one time not twice. Thanks. But, I still have a problem with partner_id field it doesn't get the correct value (the same as old one) I'm looking for why, because the main purpose of this override is to change the partner_id received value.

Did you mean to change the partner_id passed to the create of the survey.user_input??

Author

Yes, I've checked it, it works fine now, partner_id with the correct value. Thanks a lot Axel.

Happy to help

Author

Just a final question, it will work if I use self.create(cr, uid, vals, context) instead of osv.Model.create(self,cr,uid, vals, context) ?

I think that it will not work, but you are free to try it

Author

Okay, Thanks.

I used before to call self.create and lead me to very rare behavior like recursive call to the same create method, but if that works for you, go ahead

see the answer update for another approach

Author

Yes, that's what i was thinking, the create method recursiveness, because you call the same method, and it gives an infinite calling loop. Thank you for the second appraoch, indeed, it's a better one, I'll test it. Cheers.