From 50f0b0c4e252d6b59ee4e1950ef0e3c42fb9b71e Mon Sep 17 00:00:00 2001 From: Carmen Bianca BAKKER Date: Mon, 2 Sep 2024 11:27:05 +0200 Subject: [PATCH] [MIG] resource_multi_week_calendar: Backport to 12.0 Signed-off-by: Carmen Bianca BAKKER --- resource_multi_week_calendar/__manifest__.py | 2 +- .../models/resource_calendar.py | 42 +++++---------- .../tests/test_calendar.py | 51 +++++++++++-------- .../views/resource_calendar_views.xml | 18 ++++--- 4 files changed, 54 insertions(+), 59 deletions(-) diff --git a/resource_multi_week_calendar/__manifest__.py b/resource_multi_week_calendar/__manifest__.py index f1f018a28a9..0b3a4368d21 100644 --- a/resource_multi_week_calendar/__manifest__.py +++ b/resource_multi_week_calendar/__manifest__.py @@ -6,7 +6,7 @@ "name": "Multi-week calendars", "summary": """ Allow a calendar to alternate between multiple weeks.""", - "version": "16.0.1.0.0", + "version": "12.0.1.0.0", "category": "Hidden", "website": "https://github.com/OCA/hr", "author": "Coop IT Easy SC, Odoo Community Association (OCA)", diff --git a/resource_multi_week_calendar/models/resource_calendar.py b/resource_multi_week_calendar/models/resource_calendar.py index 757e38fe982..3bb93daa30c 100644 --- a/resource_multi_week_calendar/models/resource_calendar.py +++ b/resource_multi_week_calendar/models/resource_calendar.py @@ -7,6 +7,7 @@ from odoo import _, api, fields, models from odoo.exceptions import ValidationError +from odoo.addons.resource.models.resource import Intervals class ResourceCalendar(models.Model): @@ -221,44 +222,27 @@ def _split_into_weeks(self, start_dt, end_dt): # Move to the next week (start of next Monday) current_start = current_end - def _attendance_intervals_batch( - self, start_dt, end_dt, resources=None, domain=None, tz=None - ): + def _attendance_intervals(self, start_dt, end_dt, resource=None): self.ensure_one() if not self.is_multi_week: - return super()._attendance_intervals_batch( - start_dt, end_dt, resources=resources, domain=domain, tz=tz - ) + return super()._attendance_intervals(start_dt, end_dt, resource=resource) calendars_by_week = { calendar.week_number: calendar for calendar in self.family_calendar_ids } - results = [] + result = Intervals() # Calculate each week separately, choosing the correct calendar for each # week. for week_start, week_end in self._split_into_weeks(start_dt, end_dt): - results.append( - super( - ResourceCalendar, - calendars_by_week[self._get_week_number(week_start)].with_context( - # This context is not used here, but could possibly be - # used by other modules that use this module. I am not - # sure how useful it is. - recursive_multi_week=True - ), - )._attendance_intervals_batch( - week_start, week_end, resources=resources, domain=domain, tz=tz - ) - ) - - # Aggregate the results from each week. - result = {} - for item in results: - for resource, intervals in item.items(): - if resource not in result: - result[resource] = intervals - else: - result[resource] |= intervals + result |= super( + ResourceCalendar, + calendars_by_week[self._get_week_number(week_start)].with_context( + # This context is not used here, but could possibly be + # used by other modules that use this module. I am not + # sure how useful it is. + recursive_multi_week=True + ), + )._attendance_intervals(week_start, week_end, resource=resource) return result diff --git a/resource_multi_week_calendar/tests/test_calendar.py b/resource_multi_week_calendar/tests/test_calendar.py index 8d6e3e6167a..b4622e8aabe 100644 --- a/resource_multi_week_calendar/tests/test_calendar.py +++ b/resource_multi_week_calendar/tests/test_calendar.py @@ -7,11 +7,10 @@ from freezegun import freeze_time from odoo.exceptions import ValidationError -from odoo.fields import Command -from odoo.tests.common import TransactionCase +from odoo.tests.common import SavepointCase -class CalendarCase(TransactionCase): +class CalendarCase(SavepointCase): @classmethod def setUpClass(cls): super().setUpClass() @@ -56,15 +55,15 @@ def test_cant_add_parent_to_parent(self): "week_sequence": 1, } ) + parent_of_parent = self.Calendar.create( + { + "name": "Parent of parent", + # This value is kind of arbitrary here. + "week_sequence": 2, + } + ) with self.assertRaises(ValidationError): - self.Calendar.create( - { - "name": "Parent of parent", - "child_calendar_ids": self.parent_calendar.ids, - # This value is kind of arbitrary here. - "week_sequence": 2, - } - ) + parent_of_parent.child_calendar_ids = self.parent_calendar class TestCalendarIsMultiweek(CalendarCase): @@ -218,50 +217,60 @@ def setUp(self): # In the child calendar, only work the mornings. self.child_2.attendance_ids = False self.child_2.attendance_ids = [ - Command.create( + ( + 0, + False, { "name": "Monday Morning", "dayofweek": "0", "hour_from": 8, "hour_to": 12, "day_period": "morning", - } + }, ), - Command.create( + ( + 0, + False, { "name": "Tuesday Morning", "dayofweek": "1", "hour_from": 8, "hour_to": 12, "day_period": "morning", - } + }, ), - Command.create( + ( + 0, + False, { "name": "Wednesday Morning", "dayofweek": "2", "hour_from": 8, "hour_to": 12, "day_period": "morning", - } + }, ), - Command.create( + ( + 0, + False, { "name": "Thursday Morning", "dayofweek": "3", "hour_from": 8, "hour_to": 12, "day_period": "morning", - } + }, ), - Command.create( + ( + 0, + False, { "name": "Friday Morning", "dayofweek": "4", "hour_from": 8, "hour_to": 12, "day_period": "morning", - } + }, ), ] diff --git a/resource_multi_week_calendar/views/resource_calendar_views.xml b/resource_multi_week_calendar/views/resource_calendar_views.xml index a63a6c36a01..fc2dd0bcb0c 100644 --- a/resource_multi_week_calendar/views/resource_calendar_views.xml +++ b/resource_multi_week_calendar/views/resource_calendar_views.xml @@ -5,11 +5,7 @@ resource.calendar - - + @@ -44,8 +40,14 @@ - - + + + + + {'invisible': [('child_calendar_ids', '!=', [])]} + + + {'invisible': [('child_calendar_ids', '!=', [])]} @@ -58,7 +60,7 @@ resource.calendar - +