Skip to content

Commit

Permalink
WIP: Simplify epoch date
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
carmenbianca committed Aug 30, 2024
1 parent c5f2031 commit 9f3f079
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 61 deletions.
50 changes: 9 additions & 41 deletions resource_multi_week_calendar/models/resource_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
27 changes: 7 additions & 20 deletions resource_multi_week_calendar/tests/test_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 9f3f079

Please sign in to comment.