Remove hardcoded "A" and "I" #7
3 changed files with 37 additions and 29 deletions
|
@ -7,10 +7,13 @@ from users.models import CustomUser
|
|||
|
||||
|
||||
def calc_score(user: CustomUser):
|
||||
registrations_last_month = EventRegistration.objects.all().filter(user_id=user.id,
|
||||
event__date__gt=date.today() - timedelta(days=30),
|
||||
event__date__lte=date.today(),
|
||||
state__exact='A')
|
||||
registrations_last_month = EventRegistration.objects.filter(
|
||||
user_id=user.id,
|
||||
event__date__gt=date.today() - timedelta(days=30),
|
||||
event__date__lte=date.today(),
|
||||
state=EventRegistration.ADMITTED
|
||||
)
|
||||
|
||||
score = 0
|
||||
for r in registrations_last_month:
|
||||
days_ago = (date.today() - r.event.date.date()).days
|
||||
|
@ -22,41 +25,53 @@ def calc_score(user: CustomUser):
|
|||
@app.task(bind=True)
|
||||
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.
|
||||
:param self:
|
||||
:return:
|
||||
"""
|
||||
print("Assigning reservations")
|
||||
print("======================")
|
||||
# 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
|
||||
registrations: List[EventRegistration] = EventRegistration.objects.all().filter(
|
||||
event_id__in=map(lambda event: event.id, events),
|
||||
state__exact='I')
|
||||
if len(registrations) == 0:
|
||||
print("NO REGISTRATIONS?")
|
||||
registrations: List[EventRegistration] = EventRegistration.objects.filter(
|
||||
event_id__in=map(lambda event: event.id, events))
|
||||
|
||||
print(f"{len(registrations) if registrations else 'Geen'} registratie(s)")
|
||||
print("")
|
||||
|
||||
# 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}")
|
||||
print("Prioriteiten: " + ", ".join(f"{u.username} ({s})" for u, s in queue))
|
||||
print("")
|
||||
|
||||
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]
|
||||
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(
|
||||
event_id=event.id,
|
||||
user_id=user[0].id
|
||||
)
|
||||
r.state = EventRegistration.ADMITTED
|
||||
r.save()
|
||||
left -= 1
|
||||
|
||||
print(f" → Vrije plaatsen: {left}/{event.capacity}")
|
||||
|
|
|
@ -7,5 +7,4 @@ urlpatterns = [
|
|||
path("", views.index, name="index"),
|
||||
path("<int:event_id>/register", views.register, name="register"),
|
||||
path("<int:event_id>/deregister", views.deregister, name="deregister"),
|
||||
path("test/", views.view_score_stuff, name="score_stuff"),
|
||||
]
|
||||
|
|
|
@ -20,12 +20,6 @@ def index(request):
|
|||
return render(request, "events/index.html", {"events": events_data})
|
||||
|
||||
|
||||
def view_score_stuff(request):
|
||||
return HttpResponse(
|
||||
assign_reservations()
|
||||
)
|
||||
|
||||
|
||||
def register(request, event_id):
|
||||
if request.method != "POST":
|
||||
return HttpResponse(status_code=405)
|
||||
|
|
Loading…
Reference in a new issue