Skip to content

Commit

Permalink
[IMP] resource_multi_week_calendar: Improve comments and simplify code
Browse files Browse the repository at this point in the history
- Improved the comment on how week_sequence works.
- Renamed family_size to calendar_count
- Added a comment on _get_multi_week_calendar() returning a 1-item
  recordset.
- Re-optimised the current week calculation.

Signed-off-by: Carmen Bianca BAKKER <[email protected]>
  • Loading branch information
carmenbianca committed Sep 9, 2024
1 parent ccedcc3 commit 1bfbfb3
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions resource_multi_week_calendar/models/resource_calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ class ResourceCalendar(models.Model):
# constraint method is called before all children/siblings are saved,
# meaning that they can conflict with each other in this interim stage.
#
# If this value is not unique, the behaviour is undefined. Fortunately, this
# should not happen in regular Odoo usage.
# If this value is not unique, the order is preserved between the identical
# elements. The elements of child_calendar_ids are always sorted by _order,
# which is id by default. The value may not be unique when new calendars are
# added.
week_sequence = fields.Integer(default=0)
week_number = fields.Integer(
compute="_compute_week_number",
Expand Down Expand Up @@ -134,17 +136,18 @@ def _get_week_number(self, day=None):
day = fields.Date.today()
if isinstance(day, datetime):
day = day.date()
family_size = len(self.multi_week_calendar_ids)
calendar_count = len(self.multi_week_calendar_ids)
weeks_since_epoch = math.floor(
(day - self._get_first_day_of_epoch_week()).days / 7
)
return (weeks_since_epoch % family_size) + 1
return (weeks_since_epoch % calendar_count) + 1

def _get_multi_week_calendar(self, day=None):
self.ensure_one()
if not self.is_multi_week:
return self
week_number = self._get_week_number(day=day)
# Should return a 1-item recordset. If it does not, we've hit a bug.
return self.multi_week_calendar_ids.filtered(
lambda item: item.week_number == week_number
)
Expand All @@ -156,11 +159,9 @@ def _get_multi_week_calendar(self, day=None):
)
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_multi_week_calendar_id = (
calendar._get_multi_week_calendar()
)
current_calendar = calendar._get_multi_week_calendar()
calendar.current_multi_week_calendar_id = current_calendar
calendar.current_week_number = current_calendar.week_number

@api.constrains("parent_calendar_id", "child_calendar_ids")
def _check_child_is_not_parent(self):
Expand Down

0 comments on commit 1bfbfb3

Please sign in to comment.