Debian Codegraph @

addons/hr_skills_survey/models/survey_user.py

raw
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 from dateutil.relativedelta import relativedelta
4
5 from odoo import fields, models
6 from odoo.fields import Domain
7 from odoo.tools import html2plaintext
8
9
10 class SurveyUser_Input(models.Model):
11 _inherit = 'survey.user_input'
12
13 def _mark_done(self):
14 """ Will add certification to employee's resume if
15 - The survey is a certification
16 - The user is linked to an employee
17 - The user succeeded the test """
18
19 super()._mark_done()
20
21 certification_user_inputs = self.filtered(lambda user_input: user_input.survey_id.certification and user_input.scoring_success)
22 user_inputs_by_partner = certification_user_inputs.grouped('partner_id')
23 employees = self.env['hr.employee'].search(
24 [('user_id.partner_id', 'in', certification_user_inputs.partner_id.ids)])
25 resume_lines = self.env['hr.resume.line'].search(
26 Domain.OR(
27 Domain('employee_id', '=', employee.id)
28 & Domain('survey_id', 'in', user_inputs_by_partner[employee.user_id.partner_id].survey_id.ids)
29 for employee in employees
30 ))
31 resume_survey_by_ids = resume_lines.grouped(
32 lambda resume_line: (resume_line.employee_id, resume_line.survey_id))
33 line_type = self.env.ref('hr_skills_survey.resume_type_certification', raise_if_not_found=False)
34
35 lines_to_create = []
36 today = fields.Date.today()
37 for employee in employees:
38 for user_input in user_inputs_by_partner[employee.user_id.partner_id]:
39 survey = user_input.survey_id
40 date_start = today
41 validity_month = survey.certification_validity_months
42 resume_line_vals = {
43 'employee_id': employee.id,
44 'name': survey.title,
45 'date_start': date_start,
46 'date_end': date_start + relativedelta(months=validity_month) if validity_month else False,
47 'description': html2plaintext(survey.description) if survey.description else '',
48 'line_type_id': line_type.id if line_type else False,
49 'survey_id': survey.id,
50 }
51 if existing_resume_survey := resume_survey_by_ids.get((employee, survey)):
52 existing_resume_survey.write(resume_line_vals)
53 else:
54 lines_to_create.append(resume_line_vals)
55 self.env['hr.resume.line'].create(lines_to_create)