diff --git a/bemade_time_off_follower/__manifest__.py b/bemade_time_off_follower/__manifest__.py index 1cb95c3..cf1f371 100644 --- a/bemade_time_off_follower/__manifest__.py +++ b/bemade_time_off_follower/__manifest__.py @@ -1,22 +1,15 @@ -# -*- coding: utf-8 -*- { "name": "Time Off Alternative Follower", - "version": "17.0.0.0.1", + "version": "17.0.0.0.2", "category": "Extra Tools", - 'summary': 'Add Alternative Follower When Receiving Message While On Time Off', - "description": """ - Add Alternative Follower When Receiving Message While On Time Off - """, + "summary": "Add Alternative Follower When Receiving Message While On Time Off", "author": "Bemade", - 'website': 'https://www.bemade.org', - "depends": [ - 'hr_holidays', - 'mail' - ], + "website": "https://www.bemade.org", + "depends": ["hr_holidays", "mail"], "data": [ - 'views/hr_leave_views.xml', + "views/hr_leave_views.xml", ], "auto_install": False, "installable": True, - 'license': 'OPL-1' + "license": "LGPL-3", } diff --git a/bemade_time_off_follower/models/mail_thread.py b/bemade_time_off_follower/models/mail_thread.py index c205b33..d35567f 100644 --- a/bemade_time_off_follower/models/mail_thread.py +++ b/bemade_time_off_follower/models/mail_thread.py @@ -8,12 +8,14 @@ _logger = logging.getLogger(__name__) # Define a new class that inherits from 'mail.thread' class MailThread(models.AbstractModel): - _inherit = 'mail.thread' + _inherit = "mail.thread" - # Override the '_notify_compute_recipients' method - def _notify_compute_recipients(self, message, msg_vals): + # Override the '_notify_get_recipients' method + def _notify_get_recipients(self, message, msg_vals, **kwargs): # 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 now = fields.Datetime.now() @@ -22,10 +24,14 @@ class MailThread(models.AbstractModel): 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: - 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: employee = False @@ -33,33 +39,46 @@ class MailThread(models.AbstractModel): if employee: 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([ - ('state', '=', 'validate'), - ('date_from', '<=', now), - ('date_to', '>', now), - ('employee_id', '=', employee_id), - ]) + leaves = ( + self.sudo() + .env["hr.leave"] + .search( + [ + ("state", "=", "validate"), + ("date_from", "<=", now), + ("date_to", ">", now), + ("employee_id", "=", employee_id), + ] + ) + ) # Loop through each leave for leave in leaves: # 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 _logger.info( 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 - 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' - }) + 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", + } + ) else: _logger.info( 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 recipients diff --git a/bemade_time_off_follower/tests/__init__.py b/bemade_time_off_follower/tests/__init__.py new file mode 100644 index 0000000..d10f37d --- /dev/null +++ b/bemade_time_off_follower/tests/__init__.py @@ -0,0 +1 @@ +from . import test_mail_thread diff --git a/bemade_time_off_follower/tests/test_mail_thread.py b/bemade_time_off_follower/tests/test_mail_thread.py new file mode 100644 index 0000000..4d5f085 --- /dev/null +++ b/bemade_time_off_follower/tests/test_mail_thread.py @@ -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)