add validation so not adding allready recipients
This commit is contained in:
parent
c21f565b3c
commit
9dda5851a3
1 changed files with 22 additions and 3 deletions
|
|
@ -1,34 +1,48 @@
|
||||||
|
# Import necessary modules and classes
|
||||||
from odoo import models, api, fields
|
from odoo import models, api, fields
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
# Set up logging
|
||||||
_logger = logging.getLogger(__name__)
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# Define a new class that inherits from 'mail.thread'
|
||||||
class MailThread(models.AbstractModel):
|
class MailThread(models.AbstractModel):
|
||||||
_inherit = 'mail.thread'
|
_inherit = 'mail.thread'
|
||||||
|
|
||||||
|
# Override the '_notify_compute_recipients' method
|
||||||
def _notify_compute_recipients(self, message, msg_vals):
|
def _notify_compute_recipients(self, message, msg_vals):
|
||||||
|
# Call the parent class's method and get the recipients
|
||||||
recipients = super(MailThread, self)._notify_compute_recipients(message, msg_vals)
|
recipients = super(MailThread, self)._notify_compute_recipients(message, msg_vals)
|
||||||
|
|
||||||
|
# Loop through each recipient
|
||||||
for recipient in recipients:
|
for recipient in recipients:
|
||||||
|
# Search for a user with the same partner_id as the recipient
|
||||||
user = self.env['res.users'].search([('partner_id', '=', recipient['id'])], limit=1)
|
user = self.env['res.users'].search([('partner_id', '=', recipient['id'])], limit=1)
|
||||||
|
# If a user is found, search for an employee with the same user_id
|
||||||
if user:
|
if user:
|
||||||
employee = self.env['hr.employee'].search([('user_id', '=', user.id)], limit=1)
|
employee = self.env['hr.employee'].search([('user_id', '=', user.id)], limit=1)
|
||||||
else:
|
else:
|
||||||
employee = False
|
employee = False
|
||||||
|
|
||||||
|
# If an employee is found
|
||||||
if employee:
|
if employee:
|
||||||
employee_id = employee.id
|
employee_id = employee.id
|
||||||
|
# Search for leaves that are validated, within the date range, and belong to the employee
|
||||||
leaves = self.sudo().env['hr.leave'].search([
|
leaves = self.sudo().env['hr.leave'].search([
|
||||||
('state', '=', 'validate'),
|
('state', '=', 'validate'),
|
||||||
('date_from', '<=', fields.Date.today()),
|
('date_from', '<=', fields.Date.today()),
|
||||||
('employee_id', '=', employee_id),
|
('employee_id', '=', employee_id),
|
||||||
('date_to', '>=', fields.Date.today()),
|
('date_to', '>=', fields.Date.today()),
|
||||||
])
|
])
|
||||||
|
# Loop through each leave
|
||||||
for leave in leaves:
|
for leave in leaves:
|
||||||
if leave.employee_id.user_id.partner_id.id == recipient['id'] and leave.alternate_follower_id:
|
# 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
|
||||||
_logger.info(
|
_logger.info(
|
||||||
f"adding {leave.alternate_follower_id.partner_id.name} as follower for {employee.name} while on time off.")
|
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
|
||||||
recipients.append({
|
recipients.append({
|
||||||
'id': leave.alternate_follower_id.partner_id.id,
|
'id': leave.alternate_follower_id.partner_id.id,
|
||||||
'active': True,
|
'active': True,
|
||||||
|
|
@ -37,5 +51,10 @@ class MailThread(models.AbstractModel):
|
||||||
'notif': 'inbox',
|
'notif': 'inbox',
|
||||||
'type': 'user'
|
'type': 'user'
|
||||||
})
|
})
|
||||||
|
else:
|
||||||
|
_logger.info(
|
||||||
|
f"Not adding {leave.alternate_follower_id.partner_id.name} for {employee.name}, All ready "
|
||||||
|
f"a follower.")
|
||||||
|
|
||||||
|
# Return the updated recipients list
|
||||||
return recipients
|
return recipients
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue