From 098a235e6868a3f73878ee1eaeb197cb4b1b3d01 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 +++++---------- .../readme/ROADMAP.rst | 3 -- .../tests/test_calendar.py | 51 +++++++++++-------- .../views/resource_calendar_views.xml | 18 ++++--- 5 files changed, 54 insertions(+), 62 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 9aa530decd6..640310fe552 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.multi_week_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/readme/ROADMAP.rst b/resource_multi_week_calendar/readme/ROADMAP.rst index 158e0ee0555..31b06e98e14 100644 --- a/resource_multi_week_calendar/readme/ROADMAP.rst +++ b/resource_multi_week_calendar/readme/ROADMAP.rst @@ -2,9 +2,6 @@ This module is a template for building on top of. It _will_ need glue modules to work with various other modules. Most notably, ``hr_holidays`` will not work without modification. -The existing base Odoo two-week calendar functionality is hidden rather than -disabled. This may or may not be desirable. - The module may need improvements for timezone handling; this is currently untested. ``_split_into_weeks`` splits weeks on the timezone of the datetime objects passed to it instead of on the timezone of the calendar. The calculation diff --git a/resource_multi_week_calendar/tests/test_calendar.py b/resource_multi_week_calendar/tests/test_calendar.py index 3f2fa12f69e..a1b89b54d1b 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): @@ -220,50 +219,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 6d0b7888e45..9f728eeab17 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 - +