time_off_follower: fix a bug due to badly named method override + add test

This commit is contained in:
Marc Durepos 2024-07-31 15:28:40 -04:00
parent ead9bc6520
commit 5a53548693
4 changed files with 113 additions and 36 deletions

View file

@ -1,22 +1,15 @@
# -*- coding: utf-8 -*-
{ {
"name": "Time Off Alternative Follower", "name": "Time Off Alternative Follower",
"version": "17.0.0.0.1", "version": "17.0.0.0.2",
"category": "Extra Tools", "category": "Extra Tools",
'summary': 'Add Alternative Follower When Receiving Message While On Time Off', "summary": "Add Alternative Follower When Receiving Message While On Time Off",
"description": """
Add Alternative Follower When Receiving Message While On Time Off
""",
"author": "Bemade", "author": "Bemade",
'website': 'https://www.bemade.org', "website": "https://www.bemade.org",
"depends": [ "depends": ["hr_holidays", "mail"],
'hr_holidays',
'mail'
],
"data": [ "data": [
'views/hr_leave_views.xml', "views/hr_leave_views.xml",
], ],
"auto_install": False, "auto_install": False,
"installable": True, "installable": True,
'license': 'OPL-1' "license": "LGPL-3",
} }

View file

@ -8,12 +8,14 @@ _logger = logging.getLogger(__name__)
# Define a new class that inherits from 'mail.thread' # 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 # Override the '_notify_get_recipients' method
def _notify_compute_recipients(self, message, msg_vals): def _notify_get_recipients(self, message, msg_vals, **kwargs):
# Call the parent class's method and get the recipients # 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_get_recipients(
message, msg_vals, **kwargs
)
# Get the current datetime # Get the current datetime
now = fields.Datetime.now() now = fields.Datetime.now()
@ -22,10 +24,14 @@ class MailThread(models.AbstractModel):
for recipient in recipients: for recipient in recipients:
# Search for a user with the same partner_id as the recipient # 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 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
@ -33,33 +39,46 @@ class MailThread(models.AbstractModel):
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 # Search for leaves that are validated, within the date range, and belong to the employee
leaves = self.sudo().env['hr.leave'].search([ leaves = (
('state', '=', 'validate'), self.sudo()
('date_from', '<=', now), .env["hr.leave"]
('date_to', '>', now), .search(
('employee_id', '=', employee_id), [
]) ("state", "=", "validate"),
("date_from", "<=", now),
("date_to", ">", now),
("employee_id", "=", employee_id),
]
)
)
# Loop through each leave # Loop through each leave
for leave in leaves: for leave in leaves:
# If the leave has an alternate follower and the follower is not already in the recipients list # 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: if (
leave.alternate_follower_id
and leave.alternate_follower_id.partner_id.id not in recipients
):
# Log the addition of the alternate follower # 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} " f"Adding {leave.alternate_follower_id.partner_id.name} as follower for {employee.name} "
f"while on time off.") f"while on time off."
)
# Add the alternate follower to the recipients list # Add the alternate follower to the recipients list
recipients.append({ recipients.append(
'id': leave.alternate_follower_id.partner_id.id, {
'active': True, "id": leave.alternate_follower_id.partner_id.id,
'share': False, "active": True,
'groups': leave.alternate_follower_id.groups_id.ids, "share": False,
'notif': 'inbox', "groups": leave.alternate_follower_id.groups_id.ids,
'type': 'user' "notif": "inbox",
}) "type": "user",
}
)
else: else:
_logger.info( _logger.info(
f"Not adding {leave.alternate_follower_id.partner_id.name} for {employee.name}, All ready " f"Not adding {leave.alternate_follower_id.partner_id.name} for {employee.name}, All ready "
f"a follower.") f"a follower."
)
# Return the updated recipients list # Return the updated recipients list
return recipients return recipients

View file

@ -0,0 +1 @@
from . import test_mail_thread

View file

@ -0,0 +1,64 @@
from odoo.tests import TransactionCase, Form, tagged
from datetime import datetime, timedelta
@tagged("-at_install", "post_install")
class TestMailThread(TransactionCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
def test_alternate_recipient_is_notified(self):
empl_1, empl_2 = self.env["hr.employee"].create(
[
{"name": "Test 1"},
{"name": "Test 2"},
]
)
user_1, user_2 = self.env["res.users"].create(
[
{
"name": "Test 1",
"login": "test1",
"employee_ids": [(6, 0, [empl_1.id])],
"notification_type": "inbox",
},
{
"name": "Test 2",
"login": "test2",
"employee_ids": [(6, 0, [empl_2.id])],
"notification_type": "inbox",
},
]
)
leave = self.env["hr.leave"].create(
{
"employee_id": user_1.employee_id.id,
"request_date_from": datetime.now() - timedelta(days=1),
"request_date_to": datetime.now() + timedelta(days=1),
"holiday_type": "employee",
"holiday_status_id": self.env.ref(
"hr_holidays.holiday_status_unpaid"
).id,
"alternate_follower_id": user_2.id,
}
)
leave.action_approve()
if leave.state == "validate1":
leave.action_validate()
comment = self.env.ref("mail.mt_comment")
partner = empl_1.user_partner_id
partner.message_subscribe([partner.id], [comment.id])
body = f"@{user_1.name} Something something"
message_id = partner.message_post(
body=body,
message_type="comment",
subtype_id=comment.id,
)
notifications = self.env["mail.notification"].search(
[
["res_partner_id", "=", empl_2.user_partner_id.id],
]
)
self.assertEqual(len(notifications), 2)