Skip to content

Commit

Permalink
[MIG] resource_multi_week_calendar: Backport to 12.0
Browse files Browse the repository at this point in the history
Signed-off-by: Carmen Bianca BAKKER <[email protected]>
  • Loading branch information
carmenbianca committed Sep 9, 2024
1 parent 1bfbfb3 commit 098a235
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 62 deletions.
2 changes: 1 addition & 1 deletion resource_multi_week_calendar/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"name": "Multi-week calendars",
"summary": """
Allow a calendar to alternate between multiple weeks.""",
"version": "16.0.1.0.0",
"version": "12.0.1.0.0",
"category": "Hidden",
"website": "https://github.com/OCA/hr",
"author": "Coop IT Easy SC, Odoo Community Association (OCA)",
Expand Down
42 changes: 13 additions & 29 deletions resource_multi_week_calendar/models/resource_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from odoo import _, api, fields, models
from odoo.exceptions import ValidationError
from odoo.addons.resource.models.resource import Intervals


class ResourceCalendar(models.Model):
Expand Down Expand Up @@ -221,44 +222,27 @@ def _split_into_weeks(self, start_dt, end_dt):
# Move to the next week (start of next Monday)
current_start = current_end

def _attendance_intervals_batch(
self, start_dt, end_dt, resources=None, domain=None, tz=None
):
def _attendance_intervals(self, start_dt, end_dt, resource=None):
self.ensure_one()
if not self.is_multi_week:
return super()._attendance_intervals_batch(
start_dt, end_dt, resources=resources, domain=domain, tz=tz
)
return super()._attendance_intervals(start_dt, end_dt, resource=resource)

calendars_by_week = {
calendar.week_number: calendar for calendar in self.multi_week_calendar_ids
}
results = []
result = Intervals()

# Calculate each week separately, choosing the correct calendar for each
# week.
for week_start, week_end in self._split_into_weeks(start_dt, end_dt):
results.append(
super(
ResourceCalendar,
calendars_by_week[self._get_week_number(week_start)].with_context(
# This context is not used here, but could possibly be
# used by other modules that use this module. I am not
# sure how useful it is.
recursive_multi_week=True
),
)._attendance_intervals_batch(
week_start, week_end, resources=resources, domain=domain, tz=tz
)
)

# Aggregate the results from each week.
result = {}
for item in results:
for resource, intervals in item.items():
if resource not in result:
result[resource] = intervals
else:
result[resource] |= intervals
result |= super(
ResourceCalendar,
calendars_by_week[self._get_week_number(week_start)].with_context(
# This context is not used here, but could possibly be
# used by other modules that use this module. I am not
# sure how useful it is.
recursive_multi_week=True
),
)._attendance_intervals(week_start, week_end, resource=resource)

return result
3 changes: 0 additions & 3 deletions resource_multi_week_calendar/readme/ROADMAP.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ This module is a template for building on top of. It _will_ need glue modules to
work with various other modules. Most notably, ``hr_holidays`` will not work
without modification.

The existing base Odoo two-week calendar functionality is hidden rather than
disabled. This may or may not be desirable.

The module may need improvements for timezone handling; this is currently
untested. ``_split_into_weeks`` splits weeks on the timezone of the datetime
objects passed to it instead of on the timezone of the calendar. The calculation
Expand Down
51 changes: 30 additions & 21 deletions resource_multi_week_calendar/tests/test_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
from freezegun import freeze_time

from odoo.exceptions import ValidationError
from odoo.fields import Command
from odoo.tests.common import TransactionCase
from odoo.tests.common import SavepointCase


class CalendarCase(TransactionCase):
class CalendarCase(SavepointCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
Expand Down Expand Up @@ -56,15 +55,15 @@ def test_cant_add_parent_to_parent(self):
"week_sequence": 1,
}
)
parent_of_parent = self.Calendar.create(
{
"name": "Parent of parent",
# This value is kind of arbitrary here.
"week_sequence": 2,
}
)
with self.assertRaises(ValidationError):
self.Calendar.create(
{
"name": "Parent of parent",
"child_calendar_ids": self.parent_calendar.ids,
# This value is kind of arbitrary here.
"week_sequence": 2,
}
)
parent_of_parent.child_calendar_ids = self.parent_calendar


class TestCalendarIsMultiweek(CalendarCase):
Expand Down Expand Up @@ -220,50 +219,60 @@ def setUp(self):
# In the child calendar, only work the mornings.
self.child_2.attendance_ids = False
self.child_2.attendance_ids = [
Command.create(
(
0,
False,
{
"name": "Monday Morning",
"dayofweek": "0",
"hour_from": 8,
"hour_to": 12,
"day_period": "morning",
}
},
),
Command.create(
(
0,
False,
{
"name": "Tuesday Morning",
"dayofweek": "1",
"hour_from": 8,
"hour_to": 12,
"day_period": "morning",
}
},
),
Command.create(
(
0,
False,
{
"name": "Wednesday Morning",
"dayofweek": "2",
"hour_from": 8,
"hour_to": 12,
"day_period": "morning",
}
},
),
Command.create(
(
0,
False,
{
"name": "Thursday Morning",
"dayofweek": "3",
"hour_from": 8,
"hour_to": 12,
"day_period": "morning",
}
},
),
Command.create(
(
0,
False,
{
"name": "Friday Morning",
"dayofweek": "4",
"hour_from": 8,
"hour_to": 12,
"day_period": "morning",
}
},
),
]

Expand Down
18 changes: 10 additions & 8 deletions resource_multi_week_calendar/views/resource_calendar_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@
<field name="model">resource.calendar</field>
<field name="inherit_id" ref="resource.resource_calendar_form" />
<field name="arch" type="xml">
<button name="switch_calendar_type" position="attributes">
<attribute name="attrs" />
<attribute name="invisible">1</attribute>
</button>
<group name="resource_details" position="inside">
<xpath expr="//field[@name='company_id']/.." position="after">
<group name="multi_week">
<!-- TODO: Should this be readonly? If it is, you cannot add
duplicated records to an existing relation. -->
Expand Down Expand Up @@ -44,8 +40,14 @@
<field name="current_week_number" />
<field name="current_multi_week_calendar_id" />
</group>
</group>
<xpath expr="//page[@name='working_hours']" position="attributes">
</xpath>
<!-- Hide the pages -->
<xpath expr="//field[@name='attendance_ids']/.." position="attributes">
<attribute name="attrs">
{'invisible': [('child_calendar_ids', '!=', [])]}
</attribute>
</xpath>
<xpath expr="//field[@name='global_leave_ids']/.." position="attributes">
<attribute name="attrs">
{'invisible': [('child_calendar_ids', '!=', [])]}
</attribute>
Expand All @@ -58,7 +60,7 @@
<field name="model">resource.calendar</field>
<field name="inherit_id" ref="resource.view_resource_calendar_search" />
<field name="arch" type="xml">
<xpath expr="//filter[@name='inactive']" position="before">
<xpath expr="//search" position="inside">
<filter
string="Only Main Working Times"
name="only_parent_calendars"
Expand Down

0 comments on commit 098a235

Please sign in to comment.