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

Us orario invernale estivo #66

Merged
merged 19 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 13 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
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ Changelog
2.0.0 (unreleased)
------------------

- Add logic to override pauses and gates.
[daniele]

- permit to force gate / duration to operator (restapi add booking)
[mamico]

Expand Down
2 changes: 1 addition & 1 deletion src/redturtle/prenotazioni/adapters/conflict.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def get_limit(self):
# XXX: per come è ora la funzione probabilmente questa condizione non è mai vera
# if not self.prenotazioni.get_gates():
# return 1
return len(self.prenotazioni.get_available_gates())
return len(self.prenotazioni.get_gates())

def unrestricted_prenotazioni(self, **kw):
"""
Expand Down
117 changes: 60 additions & 57 deletions src/redturtle/prenotazioni/browser/prenotazioni_context_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@
from six.moves import map
from six.moves import range

import logging
import itertools
import json
import six

logger = logging.getLogger(__name__)


class PrenotazioniContextState(BrowserView):

Expand Down Expand Up @@ -168,14 +171,13 @@ def is_configured_day(self, day):
)
)

def get_week_table(self, day):
week_table = getattr(self.context, "week_table", {})
def get_week_overrides(self, day):
week_table_overrides = json.loads(
getattr(self.context, "week_table_overrides", "[]") or "[]"
)
if not week_table_overrides:
return week_table

if not week_table_overrides:
return {}
for override in week_table_overrides:
from_month = int(override.get("from_month", ""))
from_day = int(override.get("from_day", ""))
Expand All @@ -192,10 +194,17 @@ def get_week_table(self, day):

if isinstance(day, datetime):
if fromDate <= day.date() <= toDate:
return override["week_table"]
return override
else:
if fromDate <= day <= toDate:
return override["week_table"]
return override
return {}

def get_week_table(self, day):
week_table = getattr(self.context, "week_table", [])
overrides = self.get_week_overrides(day)
if overrides:
week_table = overrides.get("week_table", []) or week_table
return week_table

def is_before_allowed_period(self, day):
Expand Down Expand Up @@ -383,39 +392,20 @@ def get_anonymous_booking_url(self, day, slot, slot_min_size=0):
return self.unavailable_slot_booking_url

@memoize
def get_gates(self, day=None):
def get_gates(self, booking_date=None):
"""
Get's the gates, available and unavailable
Get the gates for booking_date
"""
# TODO: gestire le gates in base al giorno
unavailable = self.context.getUnavailable_gates() or []
return [
{
"name": gate,
"available": gate not in unavailable,
}
for gate in self.context.getGates() or [""]
]

@memoize
def get_unavailable_gates(self):
"""
Get's the gates declared unavailable
"""
return self.context.getUnavailable_gates()
if isinstance(booking_date, datetime):
# sometimes booking_date is passed as date and sometimes as datetime
booking_date = booking_date.date()

@memoize
def get_available_gates(self):
"""
Get's the gates declared available
"""
# total = set(self.get_gates())
# if self.get_unavailable_gates():
# unavailable = set(self.get_unavailable_gates())
# else:
# unavailable = set()
# return total - unavailable
return [gate["name"] for gate in self.get_gates() if gate["available"]]
gates = self.context.getGates()
overrides = self.get_week_overrides(day=booking_date)
if overrides:
gates = overrides.get("gates", []) or gates
return gates or [""]

def get_busy_gates_in_slot(self, booking_date, booking_end_date=None):
"""
Expand All @@ -439,9 +429,6 @@ def get_busy_gates_in_slot(self, booking_date, booking_end_date=None):
booking_date=booking_date,
booking_end_date=booking_end_date,
)
# unavailable gates are always busy
if self.get_unavailable_gates():
gates.update(self.get_unavailable_gates())
return gates

def get_full_gates_in_date(
Expand Down Expand Up @@ -473,7 +460,7 @@ def get_free_gates_in_slot(self, booking_date, booking_end_date=None):

:param booking_date: a DateTime object
"""
available = set(self.get_available_gates())
available = set(self.get_gates(booking_date))
busy = set(self.get_busy_gates_in_slot(booking_date, booking_end_date))
return available - busy

Expand All @@ -482,7 +469,11 @@ def get_day_intervals(self, day):
"""Return the time ranges of this day"""
weekday = day.weekday()
week_table = self.get_week_table(day=day)
day_table = week_table[weekday]
try:
day_table = week_table[weekday]
except IndexError as e:
logger.warning(e)
return {}
# Convert date + time (localtime) to datetime (utc)
morning_start = hm2DT(day, day_table["morning_start"])
morning_end = hm2DT(day, day_table["morning_end"])
Expand Down Expand Up @@ -604,8 +595,16 @@ def get_pauses_in_day_folder(self, booking_date):
This method takes all pauses from the week table and convert it on slot
:param booking_date: a date as a datetime or a string
"""
weekday = booking_date.weekday()
pause_table = self.context.pause_table or []
overrides = self.get_week_overrides(day=booking_date)
if overrides:
pause_table = overrides.get("pause_table", []) or pause_table

if not pause_table:
return []

weekday = booking_date.weekday()

today_pauses = [row for row in pause_table if row["day"] == str(weekday)]
pauses = []
for pause in today_pauses:
Expand All @@ -626,9 +625,9 @@ def get_existing_slots_in_day_folder(self, booking_date):
"""
bookings = self.get_bookings_in_day_folder(booking_date)
pauses = self.get_pauses_in_day_folder(booking_date)
bookins_list = list(map(ISlot, bookings))
bookings_list = list(map(ISlot, bookings))
pauses_list = list(map(ISlot, pauses))
return bookins_list + pauses_list
return bookings_list + pauses_list

def get_busy_slots_in_stormynight(self, booking_date):
"""This will show the slots that will not show elsewhere"""
Expand Down Expand Up @@ -656,7 +655,10 @@ def get_busy_slots_in_period(self, booking_date, period="day"):
"""
if period == "stormynight":
return self.get_busy_slots_in_stormynight(booking_date)
interval = self.get_day_intervals(booking_date)[period]
intervals = self.get_day_intervals(booking_date)
if not intervals:
return []
interval = intervals[period]
if interval.start() == "" and interval.stop() == "":
return []
allowed_review_states = ["pending", "confirmed", PAUSE_SLOT]
Expand Down Expand Up @@ -687,8 +689,8 @@ def get_busy_slots(self, booking_date, period="day"):
slots = self.get_busy_slots_in_period(booking_date, period)
for slot in slots:
if slot.context.portal_type == PAUSE_PORTAL_TYPE:
for gate in self.get_gates():
slots_by_gate.setdefault(gate["name"], []).append(slot)
for gate in self.get_gates(booking_date):
slots_by_gate.setdefault(gate, []).append(slot)
else:
slots_by_gate.setdefault(slot.gate, []).append(slot)
return slots_by_gate
Expand All @@ -705,23 +707,21 @@ def get_free_slots(self, booking_date, period="day"):
}
"""
day_intervals = self.get_day_intervals(booking_date)
if not day_intervals:
return {}
if period == "day":
intervals = [day_intervals["morning"], day_intervals["afternoon"]]
else:
intervals = [day_intervals[period]]
slots_by_gate = self.get_busy_slots(booking_date, period)
gates = [gate["name"] for gate in self.get_gates()]
gates = self.get_gates(booking_date)
availability = {}
for gate in gates:
# unavailable gates doesn't have free slots
if self.get_unavailable_gates() and gate in self.get_unavailable_gates():
availability[gate] = []
else:
availability.setdefault(gate, [])
gate_slots = slots_by_gate.get(gate, [])
for interval in intervals:
if interval:
availability[gate].extend(interval - gate_slots)
availability.setdefault(gate, [])
gate_slots = slots_by_gate.get(gate, [])
for interval in intervals:
if interval:
availability[gate].extend(interval - gate_slots)
return availability

def get_freebusy_slots(self, booking_date, period="day"):
Expand Down Expand Up @@ -751,8 +751,11 @@ def get_anonymous_slots(self, booking_date, period="day"):
{'anonymous_gate': [slot2, slot3],
}
"""
interval = self.get_day_intervals(booking_date)[period]
slots_by_gate = {"anonymous_gate": []}
intervals = self.get_day_intervals(booking_date)
if not intervals:
return slots_by_gate
interval = intervals[period]
if not interval or len(interval) == 0:
return slots_by_gate
start = interval.lower_value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class WidgetContainer extends Component {
};

const updateWidgetField = value => {
console.log('VALORE AGGIORNATO: ', value);
document.getElementById(this.props.fieldId).value = JSON.stringify(value);
};

Expand Down Expand Up @@ -79,6 +80,8 @@ class WidgetContainer extends Component {
afternoon_end: '',
},
],
gates: [],
pause_table: [],
});
this.setState({
...this.state,
Expand Down Expand Up @@ -124,6 +127,17 @@ class WidgetContainer extends Component {
return { token: key.toString(), title: key.toString() };
}),
},
weekDays: {
items: [
{ token: '0', title: 'Monday' },
{ token: '1', title: 'Tuesday' },
{ token: '2', title: 'Wednesday' },
{ token: '3', title: 'Thursday' },
{ token: '4', title: 'Friday' },
{ token: '5', title: 'Saturday' },
{ token: '6', title: 'Sunday' },
],
},
},
translations: {},
addRow: this.addRow,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import PropTypes from 'prop-types';
import WidgetContext from '../utils/widgetContext';
import IntervalSelectorField from '../fields/IntervalSelectorField';
import DaysTableField from '../fields/DaysTableField';
import GatesField from '../fields/GatesField';
import PauseTableField from '../fields/PauseTableField';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import {
faPlus,
Expand Down Expand Up @@ -113,6 +115,8 @@ const WidgetDataContainer = () => {
<div className="row-content">
<IntervalSelectorField value={entry} row={idx} />
<DaysTableField value={entry} row={idx} />
<GatesField value={entry} row={idx} />
<PauseTableField value={entry} row={idx} />
</div>
</Collapse>
</div>
Expand Down
Loading
Loading