2020-07-22 04:02:13 +02:00
|
|
|
from datetime import date, timedelta
|
|
|
|
from typing import List
|
|
|
|
|
|
|
|
from KeRS.celery import app
|
|
|
|
from events.models import Event, EventRegistration
|
|
|
|
from users.models import CustomUser
|
|
|
|
|
|
|
|
|
2020-07-25 21:23:46 +02:00
|
|
|
def calc_score(user: CustomUser):
|
2020-07-25 18:28:04 +02:00
|
|
|
registrations_last_month = EventRegistration.objects.filter(
|
|
|
|
user_id=user.id,
|
2020-07-25 21:23:46 +02:00
|
|
|
event__date__gt=date.today() - timedelta(days=30),
|
|
|
|
event__date__lte=date.today(),
|
2020-07-25 18:28:04 +02:00
|
|
|
state=EventRegistration.ADMITTED
|
|
|
|
)
|
|
|
|
|
2020-07-22 04:02:13 +02:00
|
|
|
score = 0
|
|
|
|
for r in registrations_last_month:
|
|
|
|
days_ago = (date.today() - r.event.date.date()).days
|
|
|
|
score += 1 / (days_ago + 1)
|
|
|
|
|
|
|
|
return score
|
|
|
|
|
|
|
|
|
|
|
|
@app.task(bind=True)
|
|
|
|
def assign_reservations(self):
|
|
|
|
"""
|
2020-07-25 18:28:04 +02:00
|
|
|
Check if there are any events tomorrow.
|
2020-07-22 04:02:13 +02:00
|
|
|
If so, calculate the current score for every interested user and assign the ones with the lowest scores.
|
|
|
|
:param self:
|
|
|
|
:return:
|
|
|
|
"""
|
2020-07-25 21:23:46 +02:00
|
|
|
out = []
|
|
|
|
|
2020-07-22 04:02:13 +02:00
|
|
|
# Get all events of tomorrow
|
2020-07-25 21:23:46 +02:00
|
|
|
events = Event.objects.filter(date=date.today() + timedelta(days=1))
|
|
|
|
|
|
|
|
out.append(f"{len(events) if events else 'Geen'} event(s)")
|
2020-07-22 04:02:13 +02:00
|
|
|
|
|
|
|
# Reservations
|
2020-07-25 18:28:04 +02:00
|
|
|
registrations: List[EventRegistration] = EventRegistration.objects.filter(
|
2020-07-25 21:23:46 +02:00
|
|
|
event_id__in=map(lambda event: event.id, events))
|
2020-07-25 18:28:04 +02:00
|
|
|
|
2020-07-25 21:23:46 +02:00
|
|
|
out.append(f"{len(registrations) if registrations else 'Geen'} registratie(s)")
|
|
|
|
out.append("")
|
2020-07-22 04:02:13 +02:00
|
|
|
|
|
|
|
# Relevant users
|
2020-07-25 21:23:46 +02:00
|
|
|
users = [r.user for r in registrations if r.state == EventRegistration.INTERESTED]
|
2020-07-22 04:02:13 +02:00
|
|
|
scores = list(map(calc_score, users))
|
|
|
|
queue = sorted(list(zip(users, scores)), key=lambda tup: tup[1])
|
2020-07-25 21:23:46 +02:00
|
|
|
out.append("Prioriteiten: " + ", ".join(f"{u.username} ({s})" for u, s in queue))
|
|
|
|
out.append("")
|
2020-07-22 04:02:13 +02:00
|
|
|
|
|
|
|
for event in events:
|
2020-07-25 21:23:46 +02:00
|
|
|
registrations = [r for r in registrations if r.event == event]
|
|
|
|
|
|
|
|
already_admitted_regs = [r for r in registrations if r.state == EventRegistration.ADMITTED]
|
|
|
|
left = max(event.capacity - sum(1 for r in already_admitted_regs), 0)
|
|
|
|
|
|
|
|
moment_str = Event.TIME_SLOTS[event.time]
|
|
|
|
out.append(f"• Event: {event.date}, {moment_str}. Vrije plaatsen: {left}/{event.capacity}")
|
|
|
|
for reg in already_admitted_regs:
|
|
|
|
out.append(f" = {reg.user.username}")
|
2020-07-22 04:02:13 +02:00
|
|
|
|
2020-07-25 21:23:46 +02:00
|
|
|
interested_users = {r.user for r in registrations
|
|
|
|
if r.state == EventRegistration.INTERESTED}
|
|
|
|
user_queue = list(filter(lambda element: element[0] in interested_users, queue))
|
|
|
|
|
|
|
|
for user in user_queue[0:left]:
|
|
|
|
out.append(f" + {user[0].username}")
|
2020-07-22 04:02:13 +02:00
|
|
|
r = EventRegistration.objects.get(
|
|
|
|
event_id=event.id,
|
|
|
|
user_id=user[0].id
|
|
|
|
)
|
2020-07-25 16:33:20 +02:00
|
|
|
r.state = EventRegistration.ADMITTED
|
2020-07-22 04:02:13 +02:00
|
|
|
r.save()
|
2020-07-25 21:23:46 +02:00
|
|
|
left -= 1
|
|
|
|
|
|
|
|
out.append(f" → Vrije plaatsen: {left}/{event.capacity}")
|
|
|
|
|
|
|
|
return "\n".join(out)
|