Skip to content

Commit

Permalink
Manager validity dates restrictions + Add booking date validation (di…
Browse files Browse the repository at this point in the history
…dn’t exist before) (#213)

* Initial implement

* Add dates validation

* Intermediate commit

* Intermediate commit

* * Fix tests \n * Changelog \n * Translations

* Docs + tests fix

* Update src/redturtle/prenotazioni/locales/redturtle.prenotazioni.pot

Co-authored-by: Giulia Ghisini <[email protected]>

* Update src/redturtle/prenotazioni/locales/redturtle.prenotazioni.pot

Co-authored-by: Giulia Ghisini <[email protected]>

---------

Co-authored-by: Roman Kysil <[email protected]>
Co-authored-by: Giulia Ghisini <[email protected]>
  • Loading branch information
3 people authored Nov 22, 2024
1 parent 8bc006f commit 2d1e030
Show file tree
Hide file tree
Showing 10 changed files with 366 additions and 250 deletions.
6 changes: 5 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ Changelog
2.7.10 (unreleased)
-------------------

- Nothing changed yet.
- 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]


2.7.9 (2024-10-09)
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 @@ -294,7 +304,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 @@ -346,7 +356,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
53 changes: 48 additions & 5 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 @@ -285,18 +296,50 @@ 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"""
"""Returns True if the day is valid
Day is not valid in those conditions:
- day is out of validity range
(not applied to BookingManager if PrenotazioniFolder.bookins_manager_is_restricted_by_dates is False)
- day is vacation day
- day is out of PrenotazioniFolder.futures_day range
(not applied to BookingManager if PrenotazioniFolder.bookins_manager_is_restricted_by_dates is False)
- week day is not configured
"""

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

is_configured_day = self.is_configured_day(day)

if bypass_user_restrictions:
return True

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:
return False
if self.is_before_allowed_period(
day, bypass_user_restrictions=bypass_user_restrictions
):

if self.is_before_allowed_period(day, bypass_user_restrictions=False):
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:
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 2d1e030

Please sign in to comment.