slide_channel.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import fields, models, _
  4. from odoo.tools import html2plaintext
  5. class SlideChannelPartner(models.Model):
  6. _inherit = 'slide.channel.partner'
  7. def _recompute_completion(self):
  8. res = super(SlideChannelPartner, self)._recompute_completion()
  9. partner_has_completed = {
  10. channel_partner.partner_id.id: channel_partner.channel_id for channel_partner in self
  11. if channel_partner.completed}
  12. employees = self.env['hr.employee'].sudo().search(
  13. [('user_id.partner_id', 'in', list(partner_has_completed.keys()))])
  14. if employees:
  15. HrResumeLine = self.env['hr.resume.line'].sudo()
  16. line_type = self.env.ref('hr_skills_slides.resume_type_training', raise_if_not_found=False)
  17. line_type_id = line_type and line_type.id
  18. for employee in employees:
  19. channel = partner_has_completed[employee.user_id.partner_id.id]
  20. already_added = HrResumeLine.search([
  21. ("employee_id", "in", employees.ids),
  22. ("channel_id", "=", channel.id),
  23. ("line_type_id", "=", line_type_id),
  24. ("display_type", "=", "course")
  25. ])
  26. if not already_added:
  27. HrResumeLine.create({
  28. 'employee_id': employee.id,
  29. 'name': channel.name,
  30. 'date_start': fields.Date.today(),
  31. 'date_end': fields.Date.today(),
  32. 'description': html2plaintext(channel.description),
  33. 'line_type_id': line_type_id,
  34. 'display_type': 'course',
  35. 'channel_id': channel.id
  36. })
  37. return res
  38. def _send_completed_mail(self):
  39. super()._send_completed_mail()
  40. for scp in self:
  41. if self.env.user.employee_ids:
  42. msg = _('The employee has completed the course <a href="%(link)s">%(course)s</a>',
  43. link=scp.channel_id.website_url,
  44. course=scp.channel_id.name)
  45. self.env.user.employee_id.message_post(body=msg)
  46. class Channel(models.Model):
  47. _inherit = 'slide.channel'
  48. def _action_add_members(self, target_partners, **member_values):
  49. res = super()._action_add_members(target_partners, **member_values)
  50. for channel in self:
  51. channel._message_employee_chatter(
  52. _('The employee subscribed to the course <a href="%(link)s">%(course)s</a>', link=channel.website_url, course=channel.name),
  53. target_partners)
  54. return res
  55. def _remove_membership(self, partner_ids):
  56. res = super()._remove_membership(partner_ids)
  57. partners = self.env['res.partner'].browse(partner_ids)
  58. for channel in self:
  59. channel._message_employee_chatter(
  60. _('The employee left the course <a href="%(link)s">%(course)s</a>', link=channel.website_url, course=channel.name),
  61. partners)
  62. return res
  63. def _message_employee_chatter(self, msg, partners):
  64. for partner in partners:
  65. employee = partner.user_ids.sudo().filtered(
  66. lambda u: u.employee_id and (not partner.company_id or u.employee_id.company_id == partner.company_id)
  67. ).employee_id
  68. if employee:
  69. employee.sudo().message_post(body=msg)