From 6766eea6eed242aa956457ca39ba61503fbbb58f Mon Sep 17 00:00:00 2001 From: Carmen Bianca BAKKER Date: Wed, 4 Sep 2024 15:31:56 +0200 Subject: [PATCH] [REF] resource_multi_week_calendar: Miscelleanous changes - `parent_calendar_id` is now `ondelete="cascade"`. - Renamed `family_calendar_ids` to `multi_week_calendar_ids`. - Renamed `current_calendar_id` to `current_multi_week_calendar_id`. - Renamed `_get_calendar` to `_get_multi_week_calendar`. Signed-off-by: Carmen Bianca BAKKER --- .../models/resource_calendar.py | 33 +++++++++---------- .../tests/test_calendar.py | 28 ++++++++-------- .../views/resource_calendar_views.xml | 2 +- 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/resource_multi_week_calendar/models/resource_calendar.py b/resource_multi_week_calendar/models/resource_calendar.py index 3bb93daa30c..4996c9c3e0f 100644 --- a/resource_multi_week_calendar/models/resource_calendar.py +++ b/resource_multi_week_calendar/models/resource_calendar.py @@ -16,23 +16,20 @@ class ResourceCalendar(models.Model): parent_calendar_id = fields.Many2one( comodel_name="resource.calendar", domain=[("parent_calendar_id", "=", False)], - # TODO: should this cascade instead? - ondelete="set null", + ondelete="cascade", string="Main Working Time", ) child_calendar_ids = fields.One2many( comodel_name="resource.calendar", inverse_name="parent_calendar_id", - # TODO: this causes a recursion error, but seems correct to me. - # domain=[("child_calendar_ids", "=", [])], string="Alternating Working Times", copy=True, ) # These are all your siblings (including yourself) if you are a child, or - # all your children if you are a parent. - family_calendar_ids = fields.One2many( + # all your children if you are a parent. This is not a sorted set. + multi_week_calendar_ids = fields.One2many( comodel_name="resource.calendar", - compute="_compute_family_calendar_ids", + compute="_compute_multi_week_calendar_ids", recursive=True, ) is_multi_week = fields.Boolean(compute="_compute_is_multi_week", store=True) @@ -59,7 +56,7 @@ class ResourceCalendar(models.Model): compute="_compute_current_week", recursive=True, ) - current_calendar_id = fields.Many2one( + current_multi_week_calendar_id = fields.Many2one( comodel_name="resource.calendar", compute="_compute_current_week", recursive=True, @@ -78,7 +75,7 @@ def copy(self, default=None): self.ensure_one() if default is None: default = {} - sequences = sorted(self.family_calendar_ids.mapped("week_sequence")) + sequences = sorted(self.multi_week_calendar_ids.mapped("week_sequence")) if sequences: # Assign highest value sequence. default["week_sequence"] = sequences[-1] + 1 @@ -89,10 +86,10 @@ def copy(self, default=None): "parent_calendar_id", "parent_calendar_id.child_calendar_ids", ) - def _compute_family_calendar_ids(self): + def _compute_multi_week_calendar_ids(self): for calendar in self: parent = calendar.parent_calendar_id or calendar - calendar.family_calendar_ids = parent.child_calendar_ids + calendar.multi_week_calendar_ids = parent.child_calendar_ids @api.depends( "child_calendar_ids", @@ -138,31 +135,33 @@ def _get_week_number(self, day=None): day = fields.Date.today() if isinstance(day, datetime): day = day.date() - family_size = len(self.family_calendar_ids) + family_size = len(self.multi_week_calendar_ids) weeks_since_epoch = math.floor( (day - self._get_first_day_of_epoch_week()).days / 7 ) return (weeks_since_epoch % family_size) + 1 - def _get_calendar(self, day=None): + def _get_multi_week_calendar(self, day=None): self.ensure_one() if not self.is_multi_week: return self week_number = self._get_week_number(day=day) - return self.family_calendar_ids.filtered( + return self.multi_week_calendar_ids.filtered( lambda item: item.week_number == week_number ) @api.depends( "multi_week_epoch_date", "week_number", - "family_calendar_ids", + "multi_week_calendar_ids", ) def _compute_current_week(self): for calendar in self: current_week_number = calendar._get_week_number() calendar.current_week_number = current_week_number - calendar.current_calendar_id = calendar._get_calendar() + calendar.current_multi_week_calendar_id = ( + calendar._get_multi_week_calendar() + ) @api.constrains("parent_calendar_id", "child_calendar_ids") def _check_child_is_not_parent(self): @@ -228,7 +227,7 @@ def _attendance_intervals(self, start_dt, end_dt, resource=None): return super()._attendance_intervals(start_dt, end_dt, resource=resource) calendars_by_week = { - calendar.week_number: calendar for calendar in self.family_calendar_ids + calendar.week_number: calendar for calendar in self.multi_week_calendar_ids } result = Intervals() diff --git a/resource_multi_week_calendar/tests/test_calendar.py b/resource_multi_week_calendar/tests/test_calendar.py index b4622e8aabe..a1b89b54d1b 100644 --- a/resource_multi_week_calendar/tests/test_calendar.py +++ b/resource_multi_week_calendar/tests/test_calendar.py @@ -113,14 +113,16 @@ class TestCalendarWeekEpoch(CalendarCase): @freeze_time("1970-01-08") def test_compute_current_week_no_family(self): self.assertEqual(self.parent_calendar.current_week_number, 0) - self.assertFalse(self.parent_calendar.current_calendar_id) + self.assertEqual( + self.parent_calendar.current_multi_week_calendar_id, self.parent_calendar + ) # 1970-01-01 is a Thursday. @freeze_time("1970-01-01") def test_compute_current_week_solo(self): child = self.create_simple_child() self.assertEqual(child.current_week_number, 1) - self.assertEqual(child.current_calendar_id, child) + self.assertEqual(child.current_multi_week_calendar_id, child) # 1970-01-01 is a Thursday. @freeze_time("1970-01-01") @@ -128,12 +130,12 @@ def test_compute_current_week_same_day(self): child_1 = self.create_simple_child() child_2 = self.create_simple_child() self.assertEqual(child_1.current_week_number, 1) - self.assertEqual(child_1.current_calendar_id, child_1) + self.assertEqual(child_1.current_multi_week_calendar_id, child_1) # Test against the others, too, which should have the same result. self.assertEqual(self.parent_calendar.current_week_number, 1) - self.assertEqual(self.parent_calendar.current_calendar_id, child_1) + self.assertEqual(self.parent_calendar.current_multi_week_calendar_id, child_1) self.assertEqual(child_2.current_week_number, 1) - self.assertEqual(child_2.current_calendar_id, child_1) + self.assertEqual(child_2.current_multi_week_calendar_id, child_1) # 1969-12-29 is a Monday. @freeze_time("1969-12-29") @@ -141,7 +143,7 @@ def test_compute_current_week_first_day_of_week(self): child_1 = self.create_simple_child() self.create_simple_child() self.assertEqual(child_1.current_week_number, 1) - self.assertEqual(child_1.current_calendar_id, child_1) + self.assertEqual(child_1.current_multi_week_calendar_id, child_1) # 1969-12-28 is a Sunday. @freeze_time("1969-12-28") @@ -149,7 +151,7 @@ def test_compute_current_week_one_week_ago(self): child_1 = self.create_simple_child() child_2 = self.create_simple_child() self.assertEqual(child_1.current_week_number, 2) - self.assertEqual(child_1.current_calendar_id, child_2) + self.assertEqual(child_1.current_multi_week_calendar_id, child_2) # 1970-01-04 is a Sunday. @freeze_time("1970-01-04") @@ -157,7 +159,7 @@ def test_compute_current_week_last_day_of_week(self): child_1 = self.create_simple_child() self.create_simple_child() self.assertEqual(child_1.current_week_number, 1) - self.assertEqual(child_1.current_calendar_id, child_1) + self.assertEqual(child_1.current_multi_week_calendar_id, child_1) # 1970-01-05 is a Monday. @freeze_time("1970-01-05") @@ -165,7 +167,7 @@ def test_compute_current_week_next_week(self): child_1 = self.create_simple_child() child_2 = self.create_simple_child() self.assertEqual(child_1.current_week_number, 2) - self.assertEqual(child_1.current_calendar_id, child_2) + self.assertEqual(child_1.current_multi_week_calendar_id, child_2) # 1970-01-12 is a Monday. @freeze_time("1970-01-12") @@ -173,7 +175,7 @@ def test_compute_current_week_in_two_weeks(self): child_1 = self.create_simple_child() self.create_simple_child() self.assertEqual(child_1.current_week_number, 1) - self.assertEqual(child_1.current_calendar_id, child_1) + self.assertEqual(child_1.current_multi_week_calendar_id, child_1) # 1970-01-12 is a Monday. @freeze_time("1970-01-12") @@ -182,7 +184,7 @@ def test_compute_current_week_in_two_weeks_three_calendars(self): self.create_simple_child() child_3 = self.create_simple_child() self.assertEqual(child_3.current_week_number, 3) - self.assertEqual(child_3.current_calendar_id, child_3) + self.assertEqual(child_3.current_multi_week_calendar_id, child_3) # 1970-01-04 is a Sunday. @freeze_time("1970-01-04") @@ -190,13 +192,13 @@ def test_compute_current_week_when_day_changes(self): child_1 = self.create_simple_child() child_2 = self.create_simple_child() self.assertEqual(child_1.current_week_number, 1) - self.assertEqual(child_1.current_calendar_id, child_1) + self.assertEqual(child_1.current_multi_week_calendar_id, child_1) with freeze_time("1970-01-05"): # This re-compute shouldn't technically be needed... Maybe there's a # cache? child_1._compute_current_week() self.assertEqual(child_1.current_week_number, 2) - self.assertEqual(child_1.current_calendar_id, child_2) + self.assertEqual(child_1.current_multi_week_calendar_id, child_2) # 2024-07-01 is a Monday. @freeze_time("2024-07-01") diff --git a/resource_multi_week_calendar/views/resource_calendar_views.xml b/resource_multi_week_calendar/views/resource_calendar_views.xml index fc2dd0bcb0c..9f728eeab17 100644 --- a/resource_multi_week_calendar/views/resource_calendar_views.xml +++ b/resource_multi_week_calendar/views/resource_calendar_views.xml @@ -38,7 +38,7 @@ attrs="{'invisible': [('parent_calendar_id', '=', False)]}" /> - +