Skip to content

Commit dc5609c

Browse files
[IMP] hr_holidays_leave_report_calendar_type: implement compute and search methods to allow filtering by calendar type
1 parent 48473fe commit dc5609c

3 files changed

Lines changed: 24 additions & 44 deletions

File tree

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
21
from . import models
Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,26 @@
11
# Copyright 2026 Solvos Consultoría Informática, S.L. (<https://www.solvos.es>)
22
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
33

4-
from psycopg2 import sql
5-
6-
from odoo import fields, models, tools
4+
from odoo import fields, models
75

86

97
class LeaveReportCalendar(models.Model):
108
_inherit = "hr.leave.report.calendar"
119

1210
holiday_status_id = fields.Many2one(
1311
"hr.leave.type",
14-
readonly=True,
1512
string="Time Off Type",
13+
compute="_compute_holiday_status_id",
14+
search="_search_holiday_status_id",
1615
groups="hr_holidays.group_hr_holidays_user",
1716
)
1817

19-
def init(self):
20-
res = super().init()
21-
22-
self._cr.execute(
23-
"""
24-
SELECT view_definition
25-
FROM information_schema.views
26-
WHERE table_name = 'hr_leave_report_calendar'
27-
"""
28-
)
29-
view_def = self._cr.fetchone()[0]
30-
31-
if "holiday_status_id" not in view_def:
32-
view_def = view_def.replace(
33-
"is_hatched", "is_hatched, hl.holiday_status_id AS holiday_status_id"
34-
)
35-
36-
tools.drop_view_if_exists(self._cr, "hr_leave_report_calendar")
37-
38-
query = sql.SQL(
39-
"CREATE OR REPLACE VIEW hr_leave_report_calendar AS {}"
40-
).format(sql.SQL(view_def))
41-
self._cr.execute(query)
18+
def _compute_holiday_status_id(self):
19+
leaves = self.env["hr.leave"].browse(self.ids)
20+
leave_status_dict = {leave.id: leave.holiday_status_id for leave in leaves}
21+
for record in self:
22+
record.holiday_status_id = leave_status_dict.get(record.id, False)
4223

43-
return res
24+
def _search_holiday_status_id(self, operator, value):
25+
leaves = self.env["hr.leave"].search([("holiday_status_id", operator, value)])
26+
return [("id", "in", leaves.ids)]

hr_holidays_leave_report_calendar_type/tests/test_hr_holidays_leave_report_calendar_type.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright 2026 Solvos Consultoría Informática, S.L. (<https://www.solvos.es>)
22
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
33

4+
45
from odoo.tests.common import TransactionCase, tagged
56

67

@@ -40,23 +41,20 @@ def test_field_exists(self):
4041
self.assertIn(
4142
"holiday_status_id",
4243
self.env["hr.leave.report.calendar"]._fields,
43-
"holiday_status_id field dont exists.",
44+
"The 'holiday_status_id' field is not found in the Odoo model.",
4445
)
4546

46-
def test_sql_view_column_exists(self):
47-
self.env.cr.execute(
48-
"""
49-
SELECT column_name
50-
FROM information_schema.columns
51-
WHERE table_name = 'hr_leave_report_calendar';
52-
"""
53-
)
54-
result = self.env.cr.fetchall()
47+
def test_field_assigned_properly(self):
48+
calendar_record = self.env["hr.leave.report.calendar"].browse(self.leave.id)
5549

56-
column_names = [row[0] for row in result]
50+
self.assertEqual(
51+
calendar_record.holiday_status_id,
52+
self.leave.holiday_status_id,
53+
"The holiday_status_id is not properly assigned in the calendar report.",
54+
)
5755

58-
self.assertIn(
59-
"holiday_status_id",
60-
column_names,
61-
"The init() method failed or did not add the holiday_status_id column.",
56+
def test_search_holiday_status_id(self):
57+
calendars = self.env["hr.leave.report.calendar"].search(
58+
[("holiday_status_id", "=", self.leave_type.id)]
6259
)
60+
self.assertIn(self.leave.id, calendars.ids)

0 commit comments

Comments
 (0)