Skip to content

Commit

Permalink
[FIX] resource_booking: pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
norlinhenrik committed Nov 27, 2023
1 parent c5d6126 commit 51cac6b
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 25 deletions.
4 changes: 3 additions & 1 deletion resource_booking/migrations/16.0.1.0.0/post-migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
31 changes: 24 additions & 7 deletions resource_booking/migrations/16.0.1.0.0/pre-migration.py
Original file line number Diff line number Diff line change
@@ -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",
),
]


Expand Down
25 changes: 19 additions & 6 deletions resource_booking/models/resource_booking.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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`.
#
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion resource_booking/models/resource_booking_combination.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 2 additions & 8 deletions resource_booking/models/resource_booking_type.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
Expand Down
12 changes: 10 additions & 2 deletions resource_booking/tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 51cac6b

Please sign in to comment.