| 1 |
|
| 2 |
|
| 3 |
from datetime import timedelta |
| 4 |
from odoo import fields, models, _ |
| 5 |
from odoo.exceptions import UserError |
| 6 |
|
| 7 |
|
| 8 |
class HrApplicant(models.Model): |
| 9 |
_inherit = "hr.applicant" |
| 10 |
|
| 11 |
survey_id = fields.Many2one('survey.survey', related='job_id.survey_id', string="Survey", readonly=True) |
| 12 |
response_ids = fields.One2many('survey.user_input', 'applicant_id', string="Responses") |
| 13 |
|
| 14 |
def action_print_survey(self): |
| 15 |
""" If response is available then print this response otherwise print survey form (print template of the survey) """ |
| 16 |
self.ensure_one() |
| 17 |
sorted_interviews = self.response_ids\ |
| 18 |
.filtered(lambda i: i.survey_id == self.survey_id)\ |
| 19 |
.sorted(lambda i: i.create_date, reverse=True) |
| 20 |
if not sorted_interviews: |
| 21 |
action = self.survey_id.action_print_survey() |
| 22 |
action['target'] = 'new' |
| 23 |
return action |
| 24 |
|
| 25 |
answered_interviews = sorted_interviews.filtered(lambda i: i.state == 'done') |
| 26 |
if answered_interviews: |
| 27 |
action = self.survey_id.action_print_survey(answer=answered_interviews[0]) |
| 28 |
action['target'] = 'new' |
| 29 |
return action |
| 30 |
action = self.survey_id.action_print_survey(answer=sorted_interviews[0]) |
| 31 |
action['target'] = 'new' |
| 32 |
return action |
| 33 |
|
| 34 |
def action_send_survey(self): |
| 35 |
self.ensure_one() |
| 36 |
|
| 37 |
|
| 38 |
if not self.partner_id: |
| 39 |
if not self.partner_name: |
| 40 |
raise UserError(_('Please provide an applicant name.')) |
| 41 |
self.partner_id = self.env['res.partner'].sudo().create({ |
| 42 |
'is_company': False, |
| 43 |
'name': self.partner_name, |
| 44 |
'email': self.email_from, |
| 45 |
'phone': self.partner_phone, |
| 46 |
}) |
| 47 |
|
| 48 |
self.survey_id.check_validity() |
| 49 |
template = self.env.ref('hr_recruitment_survey.mail_template_applicant_interview_invite', raise_if_not_found=False) |
| 50 |
local_context = dict( |
| 51 |
default_applicant_id=self.id, |
| 52 |
default_partner_ids=self.partner_id.ids, |
| 53 |
default_survey_id=self.survey_id.id, |
| 54 |
default_use_template=bool(template), |
| 55 |
default_template_id=template and template.id or False, |
| 56 |
default_email_layout_xmlid='mail.mail_notification_light', |
| 57 |
default_deadline=fields.Datetime.now() + timedelta(days=15) |
| 58 |
) |
| 59 |
|
| 60 |
return { |
| 61 |
'type': 'ir.actions.act_window', |
| 62 |
'name': _("Send an interview"), |
| 63 |
'view_mode': 'form', |
| 64 |
'res_model': 'survey.invite', |
| 65 |
'target': 'new', |
| 66 |
'context': local_context, |
| 67 |
} |