Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manager validity dates restrictions + Add booking date validation (didn’t exist before) #213

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ Changelog
2.7.8 (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.7 (2024-08-22)
Expand Down Expand Up @@ -42,7 +46,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
1 change: 1 addition & 0 deletions buildout.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ extends =
eggs +=
Products.PrintingMailHost
design.plone.ioprenoto

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
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
mamico marked this conversation as resolved.
Show resolved Hide resolved
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,
mamico marked this conversation as resolved.
Show resolved Hide resolved
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