Compare commits

...

4 commits

Author SHA1 Message Date
bd714538df
Convert back to print since not needed on web any more 2020-07-25 21:26:58 +02:00
a37e59ed02
Remove /test endpoint 2020-07-25 21:25:39 +02:00
c25356594a
Fix multiple problems in event place assignment 2020-07-25 21:23:46 +02:00
7e9b23d4a1
Remove hardcoded "A" and "I"
These stand for "admitted" and "interested", but we have constants for
that.

Also: fix a typo, remove cargo culted `.all()`, use saner indentation.
2020-07-25 18:40:47 +02:00
3 changed files with 37 additions and 29 deletions

View file

@ -7,10 +7,13 @@ from users.models import CustomUser
def calc_score(user: CustomUser): def calc_score(user: CustomUser):
registrations_last_month = EventRegistration.objects.all().filter(user_id=user.id, registrations_last_month = EventRegistration.objects.filter(
event__date__gt=date.today() - timedelta(days=30), user_id=user.id,
event__date__lte=date.today(), event__date__gt=date.today() - timedelta(days=30),
state__exact='A') event__date__lte=date.today(),
state=EventRegistration.ADMITTED
)
score = 0 score = 0
for r in registrations_last_month: for r in registrations_last_month:
days_ago = (date.today() - r.event.date.date()).days days_ago = (date.today() - r.event.date.date()).days
@ -22,41 +25,53 @@ def calc_score(user: CustomUser):
@app.task(bind=True) @app.task(bind=True)
def assign_reservations(self): def assign_reservations(self):
""" """
Chech if there are any events the next day. Check if there are any events tomorrow.
If so, calculate the current score for every interested user and assign the ones with the lowest scores. If so, calculate the current score for every interested user and assign the ones with the lowest scores.
:param self: :param self:
:return: :return:
""" """
print("Assigning reservations")
print("======================")
# Get all events of tomorrow # Get all events of tomorrow
events = Event.objects.all().filter(date__date=date.today() + timedelta(days=1)) events = Event.objects.filter(date=date.today() + timedelta(days=1))
print(f"{len(events) if events else 'Geen'} event(s)")
# Reservations # Reservations
registrations: List[EventRegistration] = EventRegistration.objects.all().filter( registrations: List[EventRegistration] = EventRegistration.objects.filter(
event_id__in=map(lambda event: event.id, events), event_id__in=map(lambda event: event.id, events))
state__exact='I')
if len(registrations) == 0: print(f"{len(registrations) if registrations else 'Geen'} registratie(s)")
print("NO REGISTRATIONS?") print("")
# Relevant users # Relevant users
users = set(map(lambda r: r.user, registrations)) users = [r.user for r in registrations if r.state == EventRegistration.INTERESTED]
scores = list(map(calc_score, users)) scores = list(map(calc_score, users))
queue = sorted(list(zip(users, scores)), key=lambda tup: tup[1]) queue = sorted(list(zip(users, scores)), key=lambda tup: tup[1])
print(f"Scores: {scores}") print("Prioriteiten: " + ", ".join(f"{u.username} ({s})" for u, s in queue))
print(f"Queue: {queue}") print("")
for event in events: for event in events:
print(f"EVENT: {event.date} - {event.capacity}") registrations = [r for r in registrations if r.event == event]
event_registrations = list(filter(lambda r: r.event == event, registrations))
event_users = set(map(lambda r: r.user, event_registrations))
event_queue = list(filter(lambda element: element[0] in event_users, queue))
for user in event_queue[0:event.capacity]: already_admitted_regs = [r for r in registrations if r.state == EventRegistration.ADMITTED]
print(f"Selected {user[0]}") left = max(event.capacity - sum(1 for r in already_admitted_regs), 0)
moment_str = Event.TIME_SLOTS[event.time]
print(f"• Event: {event.date}, {moment_str}. Vrije plaatsen: {left}/{event.capacity}")
for reg in already_admitted_regs:
print(f" = {reg.user.username}")
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]:
print(f" + {user[0].username}")
r = EventRegistration.objects.get( r = EventRegistration.objects.get(
event_id=event.id, event_id=event.id,
user_id=user[0].id user_id=user[0].id
) )
r.state = EventRegistration.ADMITTED r.state = EventRegistration.ADMITTED
r.save() r.save()
left -= 1
print(f" → Vrije plaatsen: {left}/{event.capacity}")

View file

@ -7,5 +7,4 @@ urlpatterns = [
path("", views.index, name="index"), path("", views.index, name="index"),
path("<int:event_id>/register", views.register, name="register"), path("<int:event_id>/register", views.register, name="register"),
path("<int:event_id>/deregister", views.deregister, name="deregister"), path("<int:event_id>/deregister", views.deregister, name="deregister"),
path("test/", views.view_score_stuff, name="score_stuff"),
] ]

View file

@ -20,12 +20,6 @@ def index(request):
return render(request, "events/index.html", {"events": events_data}) return render(request, "events/index.html", {"events": events_data})
def view_score_stuff(request):
return HttpResponse(
assign_reservations()
)
def register(request, event_id): def register(request, event_id):
if request.method != "POST": if request.method != "POST":
return HttpResponse(status_code=405) return HttpResponse(status_code=405)