bemade-addons/bemade_time_off_follower/models/mail_thread.py

66 lines
2.9 KiB
Python
Raw Normal View History

2023-11-16 09:01:22 -05:00
# Import necessary modules and classes
2023-07-22 07:25:40 -04:00
from odoo import models, api, fields
import logging
2023-11-16 09:01:22 -05:00
# Set up logging
2023-07-22 07:25:40 -04:00
_logger = logging.getLogger(__name__)
2023-11-16 09:01:22 -05:00
# Define a new class that inherits from 'mail.thread'
2023-07-22 07:25:40 -04:00
class MailThread(models.AbstractModel):
_inherit = 'mail.thread'
2023-11-16 09:01:22 -05:00
# Override the '_notify_compute_recipients' method
2023-07-22 07:25:40 -04:00
def _notify_compute_recipients(self, message, msg_vals):
2023-11-16 09:01:22 -05:00
# Call the parent class's method and get the recipients
2023-07-22 07:25:40 -04:00
recipients = super(MailThread, self)._notify_compute_recipients(message, msg_vals)
2023-11-16 09:01:22 -05:00
# Get the current datetime
now = fields.Datetime.now()
# Loop through each recipient
2023-07-22 07:25:40 -04:00
for recipient in recipients:
2023-11-16 09:01:22 -05:00
# Search for a user with the same partner_id as the recipient
2023-07-22 07:25:40 -04:00
user = self.env['res.users'].search([('partner_id', '=', recipient['id'])], limit=1)
2023-11-16 09:01:22 -05:00
# If a user is found, search for an employee with the same user_id
2023-07-22 07:25:40 -04:00
if user:
employee = self.env['hr.employee'].search([('user_id', '=', user.id)], limit=1)
2023-11-16 09:01:22 -05:00
else:
employee = False
2023-07-22 07:25:40 -04:00
2023-11-16 09:01:22 -05:00
# If an employee is found
2023-07-22 07:25:40 -04:00
if employee:
employee_id = employee.id
2023-11-16 09:01:22 -05:00
# Search for leaves that are validated, within the date range, and belong to the employee
2023-07-22 07:25:40 -04:00
leaves = self.sudo().env['hr.leave'].search([
('state', '=', 'validate'),
2023-11-16 09:01:22 -05:00
('date_from', '<=', now),
('date_to', '>', now),
2023-07-22 07:25:40 -04:00
('employee_id', '=', employee_id),
])
2023-11-16 09:01:22 -05:00
# Loop through each leave
2023-07-22 07:25:40 -04:00
for leave in leaves:
2023-11-16 09:01:22 -05:00
# If the leave has an alternate follower and the follower is not already in the recipients list
if leave.alternate_follower_id and leave.alternate_follower_id.partner_id.id not in recipients:
# Log the addition of the alternate follower
2023-07-22 07:25:40 -04:00
_logger.info(
2023-11-16 09:01:22 -05:00
f"Adding {leave.alternate_follower_id.partner_id.name} as follower for {employee.name} "
f"while on time off.")
# Add the alternate follower to the recipients list
2023-07-22 07:25:40 -04:00
recipients.append({
'id': leave.alternate_follower_id.partner_id.id,
'active': True,
'share': False,
'groups': leave.alternate_follower_id.groups_id.ids,
'notif': 'inbox',
'type': 'user'
})
2023-11-16 09:01:22 -05:00
else:
_logger.info(
f"Not adding {leave.alternate_follower_id.partner_id.name} for {employee.name}, All ready "
f"a follower.")
2023-07-22 07:25:40 -04:00
2023-11-16 09:01:22 -05:00
# Return the updated recipients list
2023-07-22 07:25:40 -04:00
return recipients