From 51cac6bad1b693e5c50c143cdf70f8f4eaee5148 Mon Sep 17 00:00:00 2001 From: Henrik Norlin Date: Thu, 5 Oct 2023 08:48:46 +0200 Subject: [PATCH] [FIX] resource_booking: pre-commit --- .../migrations/16.0.1.0.0/post-migration.py | 4 ++- .../migrations/16.0.1.0.0/pre-migration.py | 31 ++++++++++++++----- resource_booking/models/resource_booking.py | 25 +++++++++++---- .../models/resource_booking_combination.py | 2 +- .../models/resource_booking_type.py | 10 ++---- resource_booking/tests/test_backend.py | 12 +++++-- 6 files changed, 59 insertions(+), 25 deletions(-) diff --git a/resource_booking/migrations/16.0.1.0.0/post-migration.py b/resource_booking/migrations/16.0.1.0.0/post-migration.py index 1fb677d5..1da234bb 100644 --- a/resource_booking/migrations/16.0.1.0.0/post-migration.py +++ b/resource_booking/migrations/16.0.1.0.0/post-migration.py @@ -3,7 +3,9 @@ def _update_attendance_hour_to(env): # 23:59 -> 24:00 - lines = env["resource.calendar.attendance"].search([("hour_to", ">=", 23 + 59/60)]) + lines = env["resource.calendar.attendance"].search( + [("hour_to", ">=", 23 + 59 / 60)] + ) lines.hour_to = 24.0 diff --git a/resource_booking/migrations/16.0.1.0.0/pre-migration.py b/resource_booking/migrations/16.0.1.0.0/pre-migration.py index cd2669cf..b614ea3c 100644 --- a/resource_booking/migrations/16.0.1.0.0/pre-migration.py +++ b/resource_booking/migrations/16.0.1.0.0/pre-migration.py @@ -1,13 +1,30 @@ from openupgradelib import openupgrade - xmlids_spec = [ - ("resource_booking.menu_resource_resource", "resource_booking.resource_resource_menu"), - ("resource_booking.menu_resource_calendar", "resource_booking.resource_calendar_menu"), - ("resource_booking.menu_view_resource_calendar_leaves_search", "resource_booking.resource_calendar_leaves_menu"), - ("resource_booking.resource_booking_combination_form", "resource_booking.resource_booking_combination_view_form"), - ("resource_booking.resource_booking_type_form", "resource_booking.resource_booking_type_view_form"), - ("resource_booking.resource_booking_form", "resource_booking.resource_booking_view_form"), + ( + "resource_booking.menu_resource_resource", + "resource_booking.resource_resource_menu", + ), + ( + "resource_booking.menu_resource_calendar", + "resource_booking.resource_calendar_menu", + ), + ( + "resource_booking.menu_view_resource_calendar_leaves_search", + "resource_booking.resource_calendar_leaves_menu", + ), + ( + "resource_booking.resource_booking_combination_form", + "resource_booking.resource_booking_combination_view_form", + ), + ( + "resource_booking.resource_booking_type_form", + "resource_booking.resource_booking_type_view_form", + ), + ( + "resource_booking.resource_booking_form", + "resource_booking.resource_booking_view_form", + ), ] diff --git a/resource_booking/models/resource_booking.py b/resource_booking/models/resource_booking.py index 7e17646a..d7cf2a27 100644 --- a/resource_booking/models/resource_booking.py +++ b/resource_booking/models/resource_booking.py @@ -20,7 +20,12 @@ def _merge_intervals(intervals): # Handle 23:59:59:99999 for i in range(len(intervals)): stop = intervals[i][1] - if stop.hour == 23 and stop.minute == 59 and stop.second == 59 and stop.microsecond == 999999: + if ( + stop.hour == 23 + and stop.minute == 59 + and stop.second == 59 + and stop.microsecond == 999999 + ): intervals[i][1] += timedelta(microseconds=1) # Begin with the last interval, to safely delete it if needed. for i in range(len(intervals) - 1, 0, -1): @@ -32,15 +37,18 @@ def _merge_intervals(intervals): del intervals[i] return Intervals([tuple(interval) for interval in intervals]) + def _availability_is_fitting(available_intervals, start_dt, stop_dt): available_intervals = _merge_intervals(available_intervals) - for available_start, available_stop, meta in available_intervals._items: + for item in available_intervals._items: + available_start, available_stop = item[0], item[1] if start_dt >= available_start and stop_dt <= available_stop: return True return False + def _availability_is_fitting_legacy(available_intervals, start_dt, end_dt): - """ I keep the old method, since part of it may be needed in the new method. """ + """I keep the old method, since part of it may be needed in the new method.""" # Test whether the stretch between start_dt and end_dt is an uninterrupted # stretch of time as determined by `available_intervals`. # @@ -164,7 +172,7 @@ class ResourceBooking(models.Model): store=True, compute="_compute_partner_ids", inverse="_inverse_partner_ids", - help="E.g. multiple people in a room. Used by sale_resource_booking_period" + help="E.g. multiple people in a room. Used by sale_resource_booking_period", ) user_id = fields.Many2one( comodel_name="res.users", @@ -553,11 +561,16 @@ def _get_available_slots(self, start_dt, end_dt): available_intervals = _merge_intervals(available_intervals) # Loop through available times and append tested start/stop to the result. test_start = False - for available_start, available_stop, meta in available_intervals._items: + for item in available_intervals._items: + available_start, available_stop = item[0], item[1] test_start = available_start while test_start and test_start < available_stop: test_stop = test_start + booking_duration - if test_start >= start_dt and test_start >= available_start and test_stop <= available_stop: + if ( + test_start >= start_dt + and test_start >= available_start + and test_stop <= available_stop + ): if not result.get(test_start.date()): result.setdefault(test_start.date(), []) result[test_start.date()].append(test_start) diff --git a/resource_booking/models/resource_booking_combination.py b/resource_booking/models/resource_booking_combination.py index c5fd515e..f8b3d390 100644 --- a/resource_booking/models/resource_booking_combination.py +++ b/resource_booking/models/resource_booking_combination.py @@ -101,7 +101,7 @@ def action_open_bookings(self): "res_model": "resource.booking", "type": "ir.actions.act_window", "view_mode": "calendar,tree,form", - "context": {'default_combination_id': self.id}, + "context": {"default_combination_id": self.id}, } def action_open_resource_booking_types(self): diff --git a/resource_booking/models/resource_booking_type.py b/resource_booking/models/resource_booking_type.py index d52937f9..b026c4b1 100644 --- a/resource_booking/models/resource_booking_type.py +++ b/resource_booking/models/resource_booking_type.py @@ -1,8 +1,6 @@ # Copyright 2021 Tecnativa - Jairo Llopis # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from datetime import timedelta -from math import ceil from random import random from odoo import _, api, fields, models @@ -59,16 +57,12 @@ class ResourceBookingType(models.Model): duration = fields.Float( required=True, default=0.5, # 30 minutes - help=( - "Booking default duration." - ), + help=("Booking default duration."), ) slot_duration = fields.Float( required=True, default=0.5, # 30 minutes - help=( - "Interval offered to start each resource booking." - ), + help=("Interval offered to start each resource booking."), ) location = fields.Char() modifications_deadline = fields.Float( diff --git a/resource_booking/tests/test_backend.py b/resource_booking/tests/test_backend.py index b915b099..2c1a077b 100644 --- a/resource_booking/tests/test_backend.py +++ b/resource_booking/tests/test_backend.py @@ -201,8 +201,16 @@ def test_availability_is_fitting_malformed_date_skip(self): """ recset = self.env["resource.booking"] tuples = [ - (datetime(2021, 3, 1, 18, 0), datetime(2021, 3, 1, 23, 59, 59, 999999), recset), - (datetime(2021, 3, 2, 0, 0), datetime(2021, 3, 2, 23, 59, 59, 999999), recset), + ( + datetime(2021, 3, 1, 18, 0), + datetime(2021, 3, 1, 23, 59, 59, 999999), + recset, + ), + ( + datetime(2021, 3, 2, 0, 0), + datetime(2021, 3, 2, 23, 59, 59, 999999), + recset, + ), (datetime(2021, 3, 3, 0, 0), datetime(2021, 3, 3, 18, 0), recset), ] available_intervals = Intervals(tuples)