Fix multiple problems in event place assignment
This commit is contained in:
parent
7e9b23d4a1
commit
c25356594a
2 changed files with 36 additions and 20 deletions
|
@ -6,11 +6,11 @@ from events.models import Event, EventRegistration
|
|||
from users.models import CustomUser
|
||||
|
||||
|
||||
def calc_score(user: CustomUser, for_date):
|
||||
def calc_score(user: CustomUser):
|
||||
registrations_last_month = EventRegistration.objects.filter(
|
||||
user_id=user.id,
|
||||
event__date__gt=for_date - timedelta(days=30),
|
||||
event__date__lte=for_date,
|
||||
event__date__gt=date.today() - timedelta(days=30),
|
||||
event__date__lte=date.today(),
|
||||
state=EventRegistration.ADMITTED
|
||||
)
|
||||
|
||||
|
@ -30,37 +30,52 @@ def assign_reservations(self):
|
|||
:param self:
|
||||
:return:
|
||||
"""
|
||||
print("Assigning reservations")
|
||||
print("======================")
|
||||
out = []
|
||||
|
||||
# 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
|
||||
registrations: List[EventRegistration] = EventRegistration.objects.filter(
|
||||
event_id__in=map(lambda event: event.id, events),
|
||||
state=EventRegistration.INTERESTED)
|
||||
event_id__in=map(lambda event: event.id, events))
|
||||
|
||||
if len(registrations) == 0:
|
||||
print("NO REGISTRATIONS?")
|
||||
out.append(f"{len(registrations) if registrations else 'Geen'} registratie(s)")
|
||||
out.append("")
|
||||
|
||||
# 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))
|
||||
queue = sorted(list(zip(users, scores)), key=lambda tup: tup[1])
|
||||
print(f"Scores: {scores}")
|
||||
print(f"Queue: {queue}")
|
||||
out.append("Prioriteiten: " + ", ".join(f"{u.username} ({s})" for u, s in queue))
|
||||
out.append("")
|
||||
|
||||
for event in events:
|
||||
print(f"EVENT: {event.date} - {event.capacity}")
|
||||
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))
|
||||
registrations = [r for r in registrations if r.event == event]
|
||||
|
||||
for user in event_queue[0:event.capacity]:
|
||||
print(f"Selected {user[0]}")
|
||||
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}")
|
||||
|
||||
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(
|
||||
event_id=event.id,
|
||||
user_id=user[0].id
|
||||
)
|
||||
r.state = EventRegistration.ADMITTED
|
||||
r.save()
|
||||
left -= 1
|
||||
|
||||
out.append(f" → Vrije plaatsen: {left}/{event.capacity}")
|
||||
|
||||
return "\n".join(out)
|
||||
|
|
|
@ -22,7 +22,8 @@ def index(request):
|
|||
|
||||
def view_score_stuff(request):
|
||||
return HttpResponse(
|
||||
assign_reservations()
|
||||
assign_reservations(),
|
||||
content_type="text/plain; charset=utf-8"
|
||||
)
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue