Fix multiple problems in event place assignment

This commit is contained in:
Midgard 2020-07-25 21:23:46 +02:00
parent 7e9b23d4a1
commit c25356594a
Signed by: midgard
GPG key ID: 511C112F1331BBB4
2 changed files with 36 additions and 20 deletions

View file

@ -6,11 +6,11 @@ from events.models import Event, EventRegistration
from users.models import CustomUser from users.models import CustomUser
def calc_score(user: CustomUser, for_date): def calc_score(user: CustomUser):
registrations_last_month = EventRegistration.objects.filter( registrations_last_month = EventRegistration.objects.filter(
user_id=user.id, user_id=user.id,
event__date__gt=for_date - timedelta(days=30), event__date__gt=date.today() - timedelta(days=30),
event__date__lte=for_date, event__date__lte=date.today(),
state=EventRegistration.ADMITTED state=EventRegistration.ADMITTED
) )
@ -30,37 +30,52 @@ def assign_reservations(self):
:param self: :param self:
:return: :return:
""" """
print("Assigning reservations") out = []
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))
out.append(f"{len(events) if events else 'Geen'} event(s)")
# Reservations # Reservations
registrations: List[EventRegistration] = EventRegistration.objects.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=EventRegistration.INTERESTED)
if len(registrations) == 0: out.append(f"{len(registrations) if registrations else 'Geen'} registratie(s)")
print("NO REGISTRATIONS?") out.append("")
# 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}") out.append("Prioriteiten: " + ", ".join(f"{u.username} ({s})" for u, s in queue))
print(f"Queue: {queue}") out.append("")
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]
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}")
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}")
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
out.append(f" → Vrije plaatsen: {left}/{event.capacity}")
return "\n".join(out)

View file

@ -22,7 +22,8 @@ def index(request):
def view_score_stuff(request): def view_score_stuff(request):
return HttpResponse( return HttpResponse(
assign_reservations() assign_reservations(),
content_type="text/plain; charset=utf-8"
) )