From d5ae5261e758a9862ecc78d6560873ae5a0d1a0d Mon Sep 17 00:00:00 2001 From: Carmen Bianca BAKKER Date: Mon, 9 Sep 2024 14:40:51 +0200 Subject: [PATCH] [IMP] resource_multi_week_calendar: Improve comments and simplify code - 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 --- .../models/resource_calendar.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/resource_multi_week_calendar/models/resource_calendar.py b/resource_multi_week_calendar/models/resource_calendar.py index d1eeae2719e..9aa530decd6 100644 --- a/resource_multi_week_calendar/models/resource_calendar.py +++ b/resource_multi_week_calendar/models/resource_calendar.py @@ -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", @@ -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 ) @@ -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):