Skip to content

Commit f557432

Browse files
committed
fix(eighth): restore waitlist metadata in signup serializer
Closes #1679
1 parent 564b304 commit f557432

2 files changed

Lines changed: 60 additions & 2 deletions

File tree

intranet/apps/eighth/serializers.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from rest_framework import serializers
1010
from rest_framework.reverse import reverse
1111

12-
from .models import EighthActivity, EighthBlock, EighthScheduledActivity, EighthSignup, EighthSponsor
12+
from .models import EighthActivity, EighthBlock, EighthScheduledActivity, EighthSignup, EighthSponsor, EighthWaitlist
1313

1414
logger = logging.getLogger(__name__)
1515

@@ -252,6 +252,26 @@ def fetch_activity_list_with_metadata(self, block):
252252
for activity_id, user_count in activities_with_signups:
253253
activity_list[activity_id]["roster"]["count"] = user_count
254254

255+
if user and getattr(user, "id", None) and activity_list:
256+
user_waitlist = EighthWaitlist.objects.filter(user_id=user.id, block_id=block.id).select_related("scheduled_activity__activity").first()
257+
if user_waitlist:
258+
user_waitlist_activity_id = user_waitlist.scheduled_activity.activity_id
259+
if user_waitlist_activity_id in activity_list:
260+
activity_list[user_waitlist_activity_id]["waitlisted"] = True
261+
activity_list[user_waitlist_activity_id]["waitlist_position"] = EighthWaitlist.objects.position_in_waitlist(
262+
user_waitlist.scheduled_activity_id, user.id
263+
)
264+
265+
waitlist_counts = (
266+
EighthWaitlist.objects.filter(scheduled_activity__block=block)
267+
.exclude(scheduled_activity__activity__deleted=True)
268+
.values_list("scheduled_activity__activity_id")
269+
.annotate(waitlist_user_count=Count("id"))
270+
)
271+
for activity_id, waitlist_user_count in waitlist_counts:
272+
if activity_id in activity_list:
273+
activity_list[activity_id]["waitlist_count"] = waitlist_user_count
274+
255275
sponsors_dict = EighthSponsor.objects.values_list("id", "user_id", "first_name", "last_name", "show_full_name")
256276

257277
all_sponsors = {

intranet/apps/eighth/tests/test_signup.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22

33
from django.contrib.auth import get_user_model
4+
from django.test import override_settings
45
from django.test.client import RequestFactory
56
from django.urls import reverse
67
from django.utils import timezone
@@ -9,7 +10,7 @@
910

1011
from ...schedule.models import Block, Day, DayType, Time
1112
from ..exceptions import SignupException
12-
from ..models import EighthActivity, EighthBlock, EighthScheduledActivity, EighthSignup
13+
from ..models import EighthActivity, EighthBlock, EighthScheduledActivity, EighthSignup, EighthWaitlist
1314
from .eighth_test import EighthAbstractTest
1415

1516

@@ -295,6 +296,43 @@ def test_eighth_signup_view(self):
295296
self.assertEqual(302, response.status_code)
296297
self.assertEqual(reverse("eighth_signup", kwargs={"block_id": block1.id}) + f"?user={user.id}", response.url)
297298

299+
@override_settings(ENABLE_WAITLIST=True)
300+
def test_eighth_signup_view_includes_waitlist_metadata(self):
301+
get_user_model().objects.all().delete()
302+
user = self.login("2021awilliam")
303+
user.user_type = "student"
304+
user.graduation_year = get_senior_graduation_year()
305+
user.save()
306+
307+
today = timezone.localtime().date()
308+
block = EighthBlock.objects.get_or_create(date=today, block_letter="A")[0]
309+
310+
activity_waitlisted = EighthActivity.objects.get_or_create(name="Waitlisted Activity", default_capacity=1)[0]
311+
scheduled_waitlisted = EighthScheduledActivity.objects.get_or_create(block=block, activity=activity_waitlisted, capacity=1)[0]
312+
313+
activity_other = EighthActivity.objects.get_or_create(name="Other Activity", default_capacity=1)[0]
314+
scheduled_other = EighthScheduledActivity.objects.get_or_create(block=block, activity=activity_other, capacity=1)[0]
315+
316+
other_user = get_user_model().objects.create(username="someone_else", graduation_year=get_senior_graduation_year())
317+
EighthWaitlist.objects.create(user=user, block=block, scheduled_activity=scheduled_waitlisted)
318+
EighthWaitlist.objects.create(user=other_user, block=block, scheduled_activity=scheduled_waitlisted)
319+
EighthWaitlist.objects.create(user=other_user, block=block, scheduled_activity=scheduled_other)
320+
321+
response = self.client.get(reverse("eighth_signup", kwargs={"block_id": block.id}))
322+
self.assertEqual(200, response.status_code)
323+
324+
act_list = json.loads(str(response.context["activities_list"]).encode().decode("unicode-escape"))
325+
waitlisted_info = act_list[str(activity_waitlisted.id)]
326+
other_info = act_list[str(activity_other.id)]
327+
328+
self.assertTrue(waitlisted_info["waitlisted"])
329+
self.assertEqual(1, waitlisted_info["waitlist_position"])
330+
self.assertEqual(2, waitlisted_info["waitlist_count"])
331+
332+
self.assertFalse(other_info["waitlisted"])
333+
self.assertEqual(0, other_info["waitlist_position"])
334+
self.assertEqual(1, other_info["waitlist_count"])
335+
298336
def test_eighth_multi_signup_view(self):
299337
"""Tests :func:`~intranet.apps.eighth.views.signup.eighth_multi_signup_view`."""
300338

0 commit comments

Comments
 (0)