-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbooking.py
More file actions
303 lines (233 loc) · 10.2 KB
/
Copy pathbooking.py
File metadata and controls
303 lines (233 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
from __future__ import annotations
from onegov.activity.models import Booking, BookingPeriod
from onegov.core.collection import GenericCollection
from onegov.activity.matching.utils import unblockable, booking_order
from onegov.activity.errors import BookingLimitReached
from sqlalchemy.orm import joinedload
from typing import Literal, TYPE_CHECKING
if TYPE_CHECKING:
from collections.abc import Callable, Collection
from onegov.activity.models import Attendee, Occasion
from onegov.user import User
from sqlalchemy.orm import Query, Session
from sortedcontainers._typing import SupportsHashableAndRichComparison
from typing import Self, TypeAlias
from uuid import UUID
ScoreFunction: TypeAlias = Callable[
[Booking],
SupportsHashableAndRichComparison
]
class BookingCollection(GenericCollection[Booking]):
def __init__(
self,
session: Session,
period_id: UUID | None = None,
username: str | None = None
) -> None:
super().__init__(session)
self.period_id = period_id
self.username = username
def query(self) -> Query[Booking]:
query = super().query()
if self.username is not None:
query = query.filter(Booking.username == self.username)
if self.period_id is not None:
query = query.filter(Booking.period_id == self.period_id)
return query.order_by(self.model_class.priority)
def for_period(self, period: BookingPeriod) -> Self:
return self.__class__(self.session, period.id, self.username)
def for_username(self, username: str) -> Self:
return self.__class__(self.session, self.period_id, username)
@property
def model_class(self) -> type[Booking]:
return Booking
def count(
self,
usernames: Collection[str] | Literal['*'] = '*',
periods: Collection[UUID] | Literal['*'] = '*',
states: Collection[str] | Literal['*'] = '*'
) -> int:
""" Returns the number of bookings, optionally filtered by usernames,
periods and states.
All parameters may either be iterables or subqueries.
"""
query = self.query().with_entities(Booking.id)
if states != '*':
# FIXME: This is to deal with the fact, that our argument type
# cannot reject a plain string.
if isinstance(states, str):
states = (states, )
query = query.filter(Booking.state.in_(states))
if periods != '*':
query = query.filter(Booking.period_id.in_(periods))
if usernames != '*':
# FIXME: This is to deal with the fact, that our argument type
# cannot reject a plain string.
if isinstance(usernames, str):
usernames = (usernames, )
query = query.filter(Booking.username.in_(usernames))
return query.count()
def booking_count(
self,
username: str,
states: Collection[str] | Literal['*'] = '*'
) -> int:
""" Returns the number of bookings in the active period. """
periods = self.session.query(BookingPeriod)
periods = periods.with_entities(BookingPeriod.id)
periods = periods.filter(BookingPeriod.active == True)
return self.count(
usernames=(username, ),
periods=periods.scalar_subquery(), # type: ignore[arg-type]
states=states
)
def by_user(self, user: User) -> Query[Booking]:
return self.query().filter(Booking.username == user.username)
def by_username(self, username: str) -> Query[Booking]:
return self.query().filter(Booking.username == username)
def by_occasion(self, occasion: Occasion) -> Query[Booking]:
return self.query().filter(Booking.occasion_id == occasion.id)
def add( # type:ignore[override]
self,
user: User,
attendee: Attendee,
occasion: Occasion,
priority: int | None = None,
group_code: str | None = None
) -> Booking:
return super().add(
username=user.username,
attendee_id=attendee.id,
occasion_id=occasion.id,
priority=priority,
group_code=group_code,
period_id=occasion.period_id
)
def accept_booking(self, booking: Booking) -> None:
""" Accepts the given booking, setting all other bookings which
conflict with it to 'blocked'.
Can only be done if the period has been confirmed already, the
occasion is not yet full and the given booking doesn't conflict
with another accepted booking.
"""
if not booking.period.confirmed:
raise RuntimeError('The period has not yet been confirmed')
if booking.occasion.full:
raise RuntimeError('The occasion is already full')
if booking.state not in ('open', 'denied'):
raise RuntimeError('Only open/denied bookings can be accepted')
bookings = tuple(
self.session.query(Booking)
.options(joinedload(Booking.occasion))
.filter(Booking.attendee_id == booking.attendee_id)
.filter(Booking.period_id == booking.period_id)
.filter(Booking.id != booking.id)
)
limit = booking.attendee.limit or booking.period.booking_limit
if limit and not booking.occasion.exempt_from_booking_limit:
accepted = sum(
1 for b in bookings if b.state == 'accepted'
and not b.occasion.exempt_from_booking_limit
)
if accepted >= limit:
raise BookingLimitReached()
# accepting one more booking will reach the limit
block_rest = (accepted + 1) >= limit
else:
block_rest = False
# block the overlapping bookings
for b in bookings:
if b.overlaps(booking):
if b.state == 'cancelled':
continue
if b.state == 'accepted':
raise RuntimeError('Conflict with booking {}'.format(b.id))
b.state = 'blocked'
# if we reached the limit, block *all* bookings
if block_rest:
for b in bookings:
if b.state in ('cancelled', 'accepted'):
continue
if b.occasion.exempt_from_booking_limit:
continue
b.state = 'blocked'
booking.state = 'accepted'
booking.cost = booking.occasion.total_cost
def cancel_booking(
self,
booking: Booking,
score_function: ScoreFunction = booking_order,
cascade: bool = False
) -> None:
""" Cancels the given booking.
If ``cascade`` is set to False, this amounts to a simple state change
and you can stop reading now.
If ``cascade`` is set to true, all denied bookings which have a chance
of becoming accepted as a result (because the occasion frees up) are
accepted according to their matching score (already accepted bookings
are not touched).
All bookings which get unblocked for the current user are tried
to be accepted as well, also with the highest score first. Contrary
to the other influenced bookings, these bookings are not limited to
the occasion of the cancelled booking.
This won't necesserily create a new stable matching, but it will
keep the operability as high as possible. It's not ideal from a
usability perspective as a single cancel may have a bigger impact
than the user intended, but it beats having occasions fail with
too few attendees when there are bookings waiting.
Can only be done if the period has been confirmed already and the
booking is an accepted bookings. Open, cancelled, blocked and
denied bookings can simply be deleted.
"""
if not booking.period.confirmed:
raise RuntimeError('The period has not yet been confirmed')
# if the booking wasn't accepted or if we don't cascade, this is quick
if not cascade or booking.state != 'accepted':
booking.state = 'cancelled'
booking.group_code = None
return
bookings = tuple(
self.session.query(Booking)
.options(joinedload(Booking.occasion))
.filter(Booking.attendee_id == booking.attendee_id)
.filter(Booking.period_id == booking.period_id)
.filter(Booking.id != booking.id))
booking.state = 'cancelled'
booking.group_code = None
# mark the no-longer blocked bookings as denied
accepted = {b for b in bookings if b.state == 'accepted'}
blocked = {b for b in bookings if b.state == 'blocked'}
unblocked = set()
if booking.period.all_inclusive or booking.period.booking_limit:
limit = booking.period.booking_limit
else:
limit = booking.attendee.limit
unblockable_bookings = unblockable(accepted, blocked, score_function)
for cnt, booking in enumerate(unblockable_bookings, start=1):
if limit and limit < (cnt + len(accepted)):
break
booking.state = 'denied'
unblocked.add(booking)
self.session.flush()
# try to accept the denied bookings in their respective occasions
for b in unblocked:
# the denied state changes during the loop execution
if b.state == 'denied' and not b.occasion.full:
self.accept_booking(b)
self.session.flush()
# try to accept the open/denied bookings in the current occasion
spots = booking.occasion.available_spots
denied_bookings = sorted(
(
b for b in booking.occasion.bookings
if b.state in ('open', 'denied')
),
key=score_function)
for b in denied_bookings:
if spots:
try:
self.accept_booking(b)
self.session.flush()
spots -= 1
except BookingLimitReached:
pass