From 7b648bb0e1feec6390a6d64b61b4196492ecb241 Mon Sep 17 00:00:00 2001 From: Ricardoalso Date: Tue, 16 Jul 2024 16:28:04 +0200 Subject: [PATCH] raise AccessError if no employee is found for current user --- hr_attendance_overtime/models/hr_employee.py | 5 ++++- .../tests/test_attendance_overtime.py | 13 +++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/hr_attendance_overtime/models/hr_employee.py b/hr_attendance_overtime/models/hr_employee.py index 70e0b74e..c845dfeb 100644 --- a/hr_attendance_overtime/models/hr_employee.py +++ b/hr_attendance_overtime/models/hr_employee.py @@ -5,7 +5,8 @@ from dateutil.relativedelta import relativedelta from pytz import timezone, utc -from odoo import SUPERUSER_ID, api, fields, models +from odoo import SUPERUSER_ID, _, api, fields, models +from odoo.exceptions import UserError from odoo.osv import expression @@ -17,6 +18,8 @@ def todays_working_times(self, empl_domain): """Method used by my attendance/kiosk view in order to display employee planning and working times""" employee = self.search(empl_domain) + if not employee: + raise UserError(_("Employee not found or not created for current user")) employee.ensure_one() now = fields.Datetime.now() tz = timezone(employee.tz) diff --git a/hr_attendance_overtime/tests/test_attendance_overtime.py b/hr_attendance_overtime/tests/test_attendance_overtime.py index a14ddbcf..894c8f67 100644 --- a/hr_attendance_overtime/tests/test_attendance_overtime.py +++ b/hr_attendance_overtime/tests/test_attendance_overtime.py @@ -7,6 +7,7 @@ from freezegun import freeze_time from psycopg2 import IntegrityError +from odoo.exceptions import UserError from odoo.tests.common import TransactionCase from odoo.tools import mute_logger @@ -610,6 +611,18 @@ def test_check_in_on_out_time(self): self.assertFalse(attendance.is_overtime) self.assertFalse(attendance.attendance_reason_ids) + def test_todays_working_times_user_without_employee(self): + user = self.env["res.users"].create( + { + "name": "Test User", + "login": "test_user", + "email": "test2@user.com", + "tz": "UTC", + } + ) + with self.assertRaises(UserError): + self.employee.todays_working_times([("id", "=", user.id)]) + def test_todays_working_times(self): self.maxDiff = None with freeze_time("2021-12-13 05:01", tz_offset=0):