From 9f3f079c9cb0d9eddb156223db4a1dec67090582 Mon Sep 17 00:00:00 2001 From: Carmen Bianca BAKKER Date: Fri, 30 Aug 2024 13:51:08 +0200 Subject: [PATCH] WIP: Simplify epoch date The epoch date is hidden on the child anyway. Let's just hide it, and always make sure to get the parent's epoch date. This gets rid of the complicated computation stuff that won't backport well to v12. Signed-off-by: Carmen Bianca BAKKER --- .../models/resource_calendar.py | 50 ++++--------------- .../tests/test_calendar.py | 27 +++------- 2 files changed, 16 insertions(+), 61 deletions(-) diff --git a/resource_multi_week_calendar/models/resource_calendar.py b/resource_multi_week_calendar/models/resource_calendar.py index 4d05e817861..34f32200179 100644 --- a/resource_multi_week_calendar/models/resource_calendar.py +++ b/resource_multi_week_calendar/models/resource_calendar.py @@ -58,16 +58,8 @@ class ResourceCalendar(models.Model): help="""When using alternating weeks, the week which contains the specified date becomes the first week, and all subsequent weeks alternate in order.""", - # required=True, - # default="1970-01-01", - # Compute this on child calendars; write this manually on parent - # calendars. Would use 'related=', but that wouldn't work here. Although - # technically, the value of this field on child calendars isn't super - # pertinent. - compute="_compute_multi_week_epoch_date", - readonly=False, - store=True, - recursive=True, + required=True, + default="1970-01-01", ) def copy(self, default=None): @@ -122,9 +114,8 @@ def _compute_week_number(self): def _get_first_day_of_epoch_week(self): self.ensure_one() - return self.multi_week_epoch_date - timedelta( - days=self.multi_week_epoch_date.weekday() - ) + epoch_date = self.get_multi_week_epoch_date() + return epoch_date - timedelta(days=epoch_date.weekday()) def _get_week_number(self, day=None): self.ensure_one() @@ -152,16 +143,6 @@ def _compute_current_week(self): lambda item: item.week_number == current_week_number ) - @api.depends("parent_calendar_id.multi_week_epoch_date") - def _compute_multi_week_epoch_date(self): - for calendar in self: - parent = calendar.parent_calendar_id - if parent: - calendar.multi_week_epoch_date = parent.multi_week_epoch_date - else: - # A default value. - calendar.multi_week_epoch_date = "1970-01-01" - @api.constrains("parent_calendar_id", "week_sequence") def _check_week_sequence_unique(self): for calendar in self: @@ -214,24 +195,11 @@ def _check_child_is_not_parent(self): } ) - @api.constrains("parent_calendar_id", "multi_week_epoch_date") - def _check_epoch_date_matches_parent(self): - for calendar in self: - if calendar.parent_calendar_id: - if ( - calendar.multi_week_epoch_date - != calendar.parent_calendar_id.multi_week_epoch_date - ): - # Because the epoch date is hidden on the views of children, - # this should not happen. However, for sanity, we do this - # check anyway. - raise ValidationError( - _( - "Working Time '%s' has an epoch date which does not" - " match its Main Working Time's. This should not happen." - ) - % calendar.name - ) + def get_multi_week_epoch_date(self): + self.ensure_one() + if self.parent_calendar_id: + return self.parent_calendar_id.multi_week_epoch_date + return self.multi_week_epoch_date @api.model def _split_into_weeks(self, start_dt, end_dt): diff --git a/resource_multi_week_calendar/tests/test_calendar.py b/resource_multi_week_calendar/tests/test_calendar.py index 20cf6c3f7c9..c2e195203ab 100644 --- a/resource_multi_week_calendar/tests/test_calendar.py +++ b/resource_multi_week_calendar/tests/test_calendar.py @@ -104,11 +104,6 @@ def test_cant_add_parent_to_parent(self): } ) - def test_cant_change_epoch_date(self): - child = self.create_simple_child() - with self.assertRaises(ValidationError): - child.multi_week_epoch_date = "2001-01-01" - class TestCalendarIsMultiweek(CalendarCase): def test_solo(self): @@ -153,21 +148,6 @@ def test_children(self): class TestCalendarWeekEpoch(CalendarCase): - def test_set_epoch_on_parent(self): - child = self.create_simple_child() - self.parent_calendar.multi_week_epoch_date = "2001-01-01" - self.assertEqual(child.multi_week_epoch_date, datetime.date(2001, 1, 1)) - - def test_set_epoch_on_parent_prior_to_creation(self): - self.parent_calendar.multi_week_epoch_date = "2001-01-01" - child = self.create_simple_child() - self.assertEqual(child.multi_week_epoch_date, datetime.date(2001, 1, 1)) - - def test_unix_epoch_default(self): - self.assertEqual( - self.parent_calendar.multi_week_epoch_date, datetime.date(1970, 1, 1) - ) - @freeze_time("1970-01-08") def test_compute_current_week_no_family(self): self.assertEqual(self.parent_calendar.current_week_number, 1) @@ -238,6 +218,13 @@ def test_compute_current_week_when_day_changes(self): self.assertEqual(child.current_week_number, 2) self.assertEqual(child.current_calendar_id, child) + # 2024-07-01 is a Monday. + @freeze_time("2024-07-01") + def test_compute_current_week_non_unix(self): + child = self.create_simple_child() + self.parent_calendar.multi_week_epoch_date = "2024-07-08" + self.assertEqual(child.current_week_number, 2) + class TestMultiCalendar(CalendarCase): def setUp(self):