Skip to content

Commit

Permalink
Merge branch 'manager_dates_restrictions' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
folix-01 committed Aug 19, 2024
2 parents 7a9b0b2 + 8e90e17 commit e48e243
Show file tree
Hide file tree
Showing 10 changed files with 332 additions and 173 deletions.
6 changes: 5 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Changelog
------------------

- Bookings details help text.
- Apply validity dates restrictions for the Bookings Manger if selected flag 'apply_date_restrictions_to_manager'.
[folix-01]

- Fixed missing validity dates check during the booking creation.
[folix-01]


Expand Down Expand Up @@ -36,7 +40,7 @@ Changelog
2.7.3 (2024-06-14)
------------------

- With an experimental envionment `SEE_OWN_ANONYMOUS_BOOKINGS` set to `True`, the endpoint will return
- With an experimental envionment `SEE_OWN_ANONYMOUS_BOOKINGS` set to `True`, the endpoint will return
also the bookings created by anonymous users with the same fiscalcode of the authenticated user.
[mamico]

Expand Down
28 changes: 19 additions & 9 deletions src/redturtle/prenotazioni/adapters/booker.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
import math
from datetime import datetime
from datetime import timedelta
from random import choice

Expand Down Expand Up @@ -30,7 +31,6 @@
from redturtle.prenotazioni.interfaces import IBookingEmailMessage
from redturtle.prenotazioni.interfaces import IBookingNotificationSender
from redturtle.prenotazioni.prenotazione_event import MovedPrenotazione
from redturtle.prenotazioni.utilities.dateutils import exceedes_date_limit


class IBooker(Interface):
Expand Down Expand Up @@ -140,16 +140,26 @@ def get_available_gate(self, booking_date, booking_expiration_date=None):
# less_used_gates = free_time_map[max_free_time]
# return choice(less_used_gates)

def check_future_days(self, booking, data):
def check_date_validity(self, booking, data):
"""
Check if date is in the right range.
Managers bypass this check
"""
future_days = booking.getFutureDays()
if future_days and not self.prenotazioni.user_can_manage_prenotazioni:
if exceedes_date_limit(data, future_days):
msg = _("Sorry, you can not book this slot for now.")
raise BookerException(api.portal.translate(msg))
booking_date = data.get("booking_date", None)

if not isinstance(booking_date, datetime):
return False

bypass_user_restrictions = (
self.prenotazioni.user_can_manage_prenotazioni
and not self.prenotazioni.bookins_manager_is_restricted_by_dates
)

if not self.prenotazioni.is_valid_day(
booking_date, bypass_user_restrictions=bypass_user_restrictions
):
msg = _("Sorry, you can not book this slot for now.")
raise BookerException(api.portal.translate(msg))

def generate_params(self, data, force_gate, duration):
# remove empty fields
Expand Down Expand Up @@ -292,7 +302,7 @@ def book(self, data, force_gate=None, duration=-1):
"""
data["booking_date"] = datetime_with_tz(data["booking_date"])

self.check_future_days(booking=self.context, data=data)
self.check_date_validity(booking=self.context, data=data)

conflict_manager = self.prenotazioni.conflict_manager
if conflict_manager.conflicts(data, force_gate=force_gate):
Expand Down Expand Up @@ -344,7 +354,7 @@ def move(self, booking, data):
)
raise BookerException(api.portal.translate(msg))

self.check_future_days(booking=booking, data=data)
self.check_date_validity(booking=booking, data=data)

# move the booking
duration = booking.getDuration()
Expand Down
40 changes: 38 additions & 2 deletions src/redturtle/prenotazioni/browser/prenotazioni_context_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ def user_can_manage_prenotazioni(self):
"redturtle.prenotazioni: Manage Prenotazioni", obj=self.context
)

@property
@memoize
def bookins_manager_is_restricted_by_dates(self):
"""Bookings manager is restricted by dates as an usual user"""
return self.context.apply_date_restrictions_to_manager

@property
@memoize
def booker(self):
Expand Down Expand Up @@ -149,6 +155,11 @@ def last_bookable_day(self):
return
return adata

@property
@memoize
def future_days_limit(self):
return self.context.getFutureDays()

@memoize
def is_vacation_day(self, date):
"""
Expand Down Expand Up @@ -286,17 +297,42 @@ def is_before_allowed_period(self, day, bypass_user_restrictions=False):
@memoize
def is_valid_day(self, day, bypass_user_restrictions=False):
"""Returns True if the day is valid"""

if isinstance(day, datetime):
day = day.date()

is_configured_day = self.is_configured_day(day)

if (
is_configured_day
and self.user_can_manage_prenotazioni
and not self.bookins_manager_is_restricted_by_dates
):
return True

if day < self.first_bookable_day:
return False

if self.is_vacation_day(day):
return False
if self.last_bookable_day and day > self.last_bookable_day:

if not bypass_user_restrictions and (
self.last_bookable_day and day > self.last_bookable_day
):
return False

if self.is_before_allowed_period(
day, bypass_user_restrictions=bypass_user_restrictions
):
return False
return self.is_configured_day(day)

if self.future_days_limit:
date_limit = date.today() + timedelta(days=self.future_days_limit)

if day >= date_limit and not bypass_user_restrictions:
return False

return is_configured_day

@property
@memoize
Expand Down
14 changes: 13 additions & 1 deletion src/redturtle/prenotazioni/content/prenotazioni_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,17 @@ class IPrenotazioniFolder(model.Schema):
required=False,
)

apply_date_restrictions_to_manager = schema.Bool(
title=_(
"Apply restrictions to Bookings Manager",
),
description=_(
"If selected, Bookings Manager will be restricted by selected dates as an usual user."
),
required=False,
default=False,
)

def get_options():
"""Return the options for this widget"""
options = [
Expand Down Expand Up @@ -363,7 +374,7 @@ def get_options():
)

notBeforeDays = schema.Int(
default=2,
default=0,
title=_("Days booking is not allowed before"),
description=_(
"notBeforeDays",
Expand Down Expand Up @@ -512,6 +523,7 @@ def data_validation(data):
"holidays",
"futureDays",
"notBeforeDays",
"apply_date_restrictions_to_manager",
],
)

Expand Down
Loading

0 comments on commit e48e243

Please sign in to comment.