Skip to content

Commit

Permalink
[REF] resource_multi_week_calendar: Miscelleanous changes
Browse files Browse the repository at this point in the history
- `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 <[email protected]>
  • Loading branch information
carmenbianca committed Sep 4, 2024
1 parent 50f0b0c commit 6766eea
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
33 changes: 16 additions & 17 deletions resource_multi_week_calendar/models/resource_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -78,7 +75,7 @@ def copy(self, default=None):
self.ensure_one()

Check warning on line 75 in resource_multi_week_calendar/models/resource_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/models/resource_calendar.py#L75

Added line #L75 was not covered by tests
if default is None:
default = {}
sequences = sorted(self.family_calendar_ids.mapped("week_sequence"))
sequences = sorted(self.multi_week_calendar_ids.mapped("week_sequence"))

Check warning on line 78 in resource_multi_week_calendar/models/resource_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/models/resource_calendar.py#L77-L78

Added lines #L77 - L78 were not covered by tests
if sequences:
# Assign highest value sequence.
default["week_sequence"] = sequences[-1] + 1
Expand All @@ -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

Check warning on line 92 in resource_multi_week_calendar/models/resource_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/models/resource_calendar.py#L91-L92

Added lines #L91 - L92 were not covered by tests

@api.depends(
"child_calendar_ids",
Expand Down Expand Up @@ -138,31 +135,33 @@ def _get_week_number(self, day=None):
day = fields.Date.today()

Check warning on line 135 in resource_multi_week_calendar/models/resource_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/models/resource_calendar.py#L135

Added line #L135 was not covered by tests
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(

Check warning on line 139 in resource_multi_week_calendar/models/resource_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/models/resource_calendar.py#L137-L139

Added lines #L137 - L139 were not covered by tests
(day - self._get_first_day_of_epoch_week()).days / 7
)
return (weeks_since_epoch % family_size) + 1

Check warning on line 142 in resource_multi_week_calendar/models/resource_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/models/resource_calendar.py#L142

Added line #L142 was not covered by tests

def _get_calendar(self, day=None):
def _get_multi_week_calendar(self, day=None):
self.ensure_one()

Check warning on line 145 in resource_multi_week_calendar/models/resource_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/models/resource_calendar.py#L145

Added line #L145 was not covered by tests
if not self.is_multi_week:
return self
week_number = self._get_week_number(day=day)

Check warning on line 148 in resource_multi_week_calendar/models/resource_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/models/resource_calendar.py#L147-L148

Added lines #L147 - L148 were not covered by tests
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 = (

Check warning on line 162 in resource_multi_week_calendar/models/resource_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/models/resource_calendar.py#L160-L162

Added lines #L160 - L162 were not covered by tests
calendar._get_multi_week_calendar()
)

@api.constrains("parent_calendar_id", "child_calendar_ids")
def _check_child_is_not_parent(self):
Expand Down Expand Up @@ -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()

Check warning on line 232 in resource_multi_week_calendar/models/resource_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/models/resource_calendar.py#L232

Added line #L232 was not covered by tests

Expand Down
28 changes: 15 additions & 13 deletions resource_multi_week_calendar/tests/test_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,67 +113,69 @@ class TestCalendarWeekEpoch(CalendarCase):
@freeze_time("1970-01-08")

Check warning on line 113 in resource_multi_week_calendar/tests/test_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/tests/test_calendar.py#L113

Added line #L113 was not covered by tests
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(

Check warning on line 116 in resource_multi_week_calendar/tests/test_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/tests/test_calendar.py#L115-L116

Added lines #L115 - L116 were not covered by tests
self.parent_calendar.current_multi_week_calendar_id, self.parent_calendar
)

# 1970-01-01 is a Thursday.
@freeze_time("1970-01-01")

Check warning on line 121 in resource_multi_week_calendar/tests/test_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/tests/test_calendar.py#L121

Added line #L121 was not covered by tests
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)

Check warning on line 125 in resource_multi_week_calendar/tests/test_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/tests/test_calendar.py#L123-L125

Added lines #L123 - L125 were not covered by tests

# 1970-01-01 is a Thursday.
@freeze_time("1970-01-01")

Check warning on line 128 in resource_multi_week_calendar/tests/test_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/tests/test_calendar.py#L128

Added line #L128 was not covered by tests
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)

Check warning on line 133 in resource_multi_week_calendar/tests/test_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/tests/test_calendar.py#L130-L133

Added lines #L130 - L133 were not covered by tests
# 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)

Check warning on line 138 in resource_multi_week_calendar/tests/test_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/tests/test_calendar.py#L135-L138

Added lines #L135 - L138 were not covered by tests

# 1969-12-29 is a Monday.
@freeze_time("1969-12-29")

Check warning on line 141 in resource_multi_week_calendar/tests/test_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/tests/test_calendar.py#L141

Added line #L141 was not covered by tests
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)

Check warning on line 146 in resource_multi_week_calendar/tests/test_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/tests/test_calendar.py#L143-L146

Added lines #L143 - L146 were not covered by tests

# 1969-12-28 is a Sunday.
@freeze_time("1969-12-28")

Check warning on line 149 in resource_multi_week_calendar/tests/test_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/tests/test_calendar.py#L149

Added line #L149 was not covered by tests
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)

Check warning on line 154 in resource_multi_week_calendar/tests/test_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/tests/test_calendar.py#L151-L154

Added lines #L151 - L154 were not covered by tests

# 1970-01-04 is a Sunday.
@freeze_time("1970-01-04")

Check warning on line 157 in resource_multi_week_calendar/tests/test_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/tests/test_calendar.py#L157

Added line #L157 was not covered by tests
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)

Check warning on line 162 in resource_multi_week_calendar/tests/test_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/tests/test_calendar.py#L159-L162

Added lines #L159 - L162 were not covered by tests

# 1970-01-05 is a Monday.
@freeze_time("1970-01-05")

Check warning on line 165 in resource_multi_week_calendar/tests/test_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/tests/test_calendar.py#L165

Added line #L165 was not covered by tests
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)

Check warning on line 170 in resource_multi_week_calendar/tests/test_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/tests/test_calendar.py#L167-L170

Added lines #L167 - L170 were not covered by tests

# 1970-01-12 is a Monday.
@freeze_time("1970-01-12")

Check warning on line 173 in resource_multi_week_calendar/tests/test_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/tests/test_calendar.py#L173

Added line #L173 was not covered by tests
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)

Check warning on line 178 in resource_multi_week_calendar/tests/test_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/tests/test_calendar.py#L175-L178

Added lines #L175 - L178 were not covered by tests

# 1970-01-12 is a Monday.
@freeze_time("1970-01-12")

Check warning on line 181 in resource_multi_week_calendar/tests/test_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/tests/test_calendar.py#L181

Added line #L181 was not covered by tests
Expand All @@ -182,21 +184,21 @@ 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)

Check warning on line 187 in resource_multi_week_calendar/tests/test_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/tests/test_calendar.py#L183-L187

Added lines #L183 - L187 were not covered by tests

# 1970-01-04 is a Sunday.
@freeze_time("1970-01-04")

Check warning on line 190 in resource_multi_week_calendar/tests/test_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/tests/test_calendar.py#L190

Added line #L190 was not covered by tests
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"):

Check warning on line 196 in resource_multi_week_calendar/tests/test_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/tests/test_calendar.py#L192-L196

Added lines #L192 - L196 were not covered by tests
# 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)

Check warning on line 201 in resource_multi_week_calendar/tests/test_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/tests/test_calendar.py#L199-L201

Added lines #L199 - L201 were not covered by tests

# 2024-07-01 is a Monday.
@freeze_time("2024-07-01")

Check warning on line 204 in resource_multi_week_calendar/tests/test_calendar.py

View check run for this annotation

Codecov / codecov/patch

resource_multi_week_calendar/tests/test_calendar.py#L204

Added line #L204 was not covered by tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
attrs="{'invisible': [('parent_calendar_id', '=', False)]}"
/>
<field name="current_week_number" />
<field name="current_calendar_id" />
<field name="current_multi_week_calendar_id" />
</group>
</xpath>
<!-- Hide the pages -->
Expand Down

0 comments on commit 6766eea

Please sign in to comment.