Skip to content

Commit

Permalink
[REF] resource_multi_week_calendar: Parent calendar no longer uses it…
Browse files Browse the repository at this point in the history
…s attendances

The idea here is that the children contain all the logic/attendances,
and the parent is just a holder of children.

Signed-off-by: Carmen Bianca BAKKER <carmen@coopiteasy.be>
  • Loading branch information
carmenbianca committed Aug 30, 2024
1 parent beb3db0 commit 6c19789
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 55 deletions.
19 changes: 9 additions & 10 deletions resource_multi_week_calendar/models/resource_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class ResourceCalendar(models.Model):
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(
comodel_name="resource.calendar",
compute="_compute_family_calendar_ids",
Expand Down Expand Up @@ -89,7 +91,7 @@ def copy(self, default=None):
def _compute_family_calendar_ids(self):
for calendar in self:
parent = calendar.parent_calendar_id or calendar
calendar.family_calendar_ids = parent | parent.child_calendar_ids
calendar.family_calendar_ids = parent.child_calendar_ids

@api.depends(
"child_calendar_ids",
Expand All @@ -113,13 +115,14 @@ def _compute_week_number(self):
if parent:
for week_number, sibling in enumerate(
parent.child_calendar_ids.sorted(lambda item: item.week_sequence),
start=2,
start=1,
):
if calendar == sibling:
calendar.week_number = week_number
break
else:
calendar.week_number = 1
# Parent calendars have no week number.
calendar.week_number = 0

def _get_first_day_of_epoch_week(self):
self.ensure_one()
Expand All @@ -128,6 +131,8 @@ def _get_first_day_of_epoch_week(self):

def _get_week_number(self, day=None):
self.ensure_one()
if not self.is_multi_week:
return 0
if day is None:
day = fields.Date.today()
if isinstance(day, datetime):
Expand All @@ -142,7 +147,6 @@ def _get_week_number(self, day=None):
"multi_week_epoch_date",
"week_number",
"family_calendar_ids",
# TODO: current date. Port company_today or add a cron. Or don't store.
)
def _compute_current_week(self):
for calendar in self:
Expand Down Expand Up @@ -219,13 +223,8 @@ def _attendance_intervals_batch(
start_dt, end_dt, resources=resources, domain=domain, tz=tz
)

if self.parent_calendar_id:
return self.parent_calendar_id._attendance_intervals_batch(
start_dt, end_dt, resources=resources, domain=domain, tz=tz
)
calendars_by_week = {
calendar.week_number: calendar
for calendar in self | self.child_calendar_ids
calendar.week_number: calendar for calendar in self.family_calendar_ids
}
results = []

Expand Down
109 changes: 65 additions & 44 deletions resource_multi_week_calendar/tests/test_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ def test_has_child_or_parent(self):

class TestCalendarWeekNumber(CalendarCase):
def test_solo(self):
self.assertEqual(self.parent_calendar.week_number, 1)
# Parents don't have a week number.
self.assertEqual(self.parent_calendar.week_number, 0)

def test_children(self):
# The parent's sequence should not matter.
Expand All @@ -99,104 +100,124 @@ def test_children(self):
"week_sequence": 30,
}
)
self.assertEqual(self.parent_calendar.week_number, 1)
self.assertEqual(one.week_number, 2)
self.assertEqual(two.week_number, 3)
self.assertEqual(self.parent_calendar.week_number, 0)
self.assertEqual(one.week_number, 1)
self.assertEqual(two.week_number, 2)

# Change the order.
one.week_sequence = 31
self.assertEqual(one.week_number, 3)
self.assertEqual(two.week_number, 2)
self.assertEqual(one.week_number, 2)
self.assertEqual(two.week_number, 1)


class TestCalendarWeekEpoch(CalendarCase):
@freeze_time("1970-01-08")
def test_compute_current_week_no_family(self):
self.assertEqual(self.parent_calendar.current_week_number, 1)
self.assertEqual(self.parent_calendar.current_calendar_id, self.parent_calendar)
self.assertEqual(self.parent_calendar.current_week_number, 0)
self.assertFalse(self.parent_calendar.current_calendar_id)

# 1970-01-01 is a Thursday.
@freeze_time("1970-01-01")
def test_compute_current_week_same_day(self):
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, self.parent_calendar)
self.assertEqual(child.current_calendar_id, child)

# 1970-01-01 is a Thursday.
@freeze_time("1970-01-01")
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)
# 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(child_2.current_week_number, 1)
self.assertEqual(child_2.current_calendar_id, child_1)

# 1969-12-29 is a Monday.
@freeze_time("1969-12-29")
def test_compute_current_week_first_day_of_week(self):
child = self.create_simple_child()
self.assertEqual(child.current_week_number, 1)
self.assertEqual(child.current_calendar_id, self.parent_calendar)
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)

# 1969-12-28 is a Sunday.
@freeze_time("1969-12-28")
def test_compute_current_week_one_week_ago(self):
child = self.create_simple_child()
self.assertEqual(child.current_week_number, 2)
self.assertEqual(child.current_calendar_id, child)
# Test against parent, too, which should have the same result.
self.assertEqual(self.parent_calendar.current_week_number, 2)
self.assertEqual(self.parent_calendar.current_calendar_id, child)
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)

# 1970-01-04 is a Sunday.
@freeze_time("1970-01-04")
def test_compute_current_week_last_day_of_week(self):
child = self.create_simple_child()
self.assertEqual(child.current_week_number, 1)
self.assertEqual(child.current_calendar_id, self.parent_calendar)
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)

# 1970-01-05 is a Monday.
@freeze_time("1970-01-05")
def test_compute_current_week_next_week(self):
child = self.create_simple_child()
self.assertEqual(child.current_week_number, 2)
self.assertEqual(child.current_calendar_id, child)
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)

# 1970-01-12 is a Monday.
@freeze_time("1970-01-12")
def test_compute_current_week_in_two_weeks(self):
child = self.create_simple_child()
self.assertEqual(child.current_week_number, 1)
self.assertEqual(child.current_calendar_id, self.parent_calendar)
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)

# 1970-01-12 is a Monday.
@freeze_time("1970-01-12")
def test_compute_current_week_in_two_weeks_three_calendars(self):
self.create_simple_child()
child_2 = self.create_simple_child()
self.assertEqual(child_2.current_week_number, 3)
self.assertEqual(child_2.current_calendar_id, child_2)
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)

# 1970-01-04 is a Sunday.
@freeze_time("1970-01-04")
def test_compute_current_week_when_day_changes(self):
child = self.create_simple_child()
self.assertEqual(child.current_week_number, 1)
self.assertEqual(child.current_calendar_id, self.parent_calendar)
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)
with freeze_time("1970-01-05"):
# This re-compute shouldn't technically be needed... Maybe there's a
# cache?
child._compute_current_week()
self.assertEqual(child.current_week_number, 2)
self.assertEqual(child.current_calendar_id, child)
child_1._compute_current_week()
self.assertEqual(child_1.current_week_number, 2)
self.assertEqual(child_1.current_calendar_id, child_2)

# 2024-07-01 is a Monday.
@freeze_time("2024-07-01")
def test_compute_current_week_non_unix(self):
child = self.create_simple_child()
child_1 = self.create_simple_child()
self.create_simple_child()
self.parent_calendar.multi_week_epoch_date = "2024-07-08"
self.assertEqual(child.current_week_number, 2)
self.assertEqual(child_1.current_week_number, 2)


class TestMultiCalendar(CalendarCase):
def setUp(self):
super().setUpClass()
# The parent calendar has attendances by default: Every weekday from 8
# The child_1 calendar has attendances by default: Every weekday from 8
# to 12, and 13 to 17.
self.child_calendar = self.create_simple_child()
self.child_1 = self.create_simple_child()
self.child_2 = self.create_simple_child()
# In the child calendar, only work the mornings.
self.child_calendar.attendance_ids = False
self.child_calendar.attendance_ids = [
self.child_2.attendance_ids = False
self.child_2.attendance_ids = [
Command.create(
{
"name": "Monday Morning",
Expand Down Expand Up @@ -255,7 +276,7 @@ def test_count_work_hours_two_weeks(self):

def test_count_work_hours_from_child(self):
# It doesn't matter whether you call the method from the child.
hours = self.child_calendar.get_work_hours_count(
hours = self.child_2.get_work_hours_count(
datetime.datetime.fromisoformat("2024-07-01T00:00:00+00:00"),
datetime.datetime.fromisoformat("2024-07-14T23:59:59+00:00"),
)
Expand Down
10 changes: 9 additions & 1 deletion resource_multi_week_calendar/views/resource_calendar_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,19 @@
name="multi_week_epoch_date"
attrs="{'invisible': [('parent_calendar_id', '!=', False)]}"
/>
<field name="week_number" />
<field
name="week_number"
attrs="{'invisible': [('parent_calendar_id', '=', False)]}"
/>
<field name="current_week_number" />
<field name="current_calendar_id" />
</group>
</group>
<xpath expr="//page[@name='working_hours']" position="attributes">
<attribute name="attrs">
{'invisible': [('child_calendar_ids', '!=', [])]}
</attribute>
</xpath>
</field>
</record>
</odoo>

0 comments on commit 6c19789

Please sign in to comment.