From 37881b29f4808b8d992dc21089312db50b2da584 Mon Sep 17 00:00:00 2001 From: Kevin Date: Sat, 14 Mar 2020 12:06:51 -0700 Subject: [PATCH 1/3] Finished waitlisting --- hknweb/events/models.py | 8 +++++- .../events/templates/events/show_details.html | 8 +++--- hknweb/events/views.py | 26 ++++++++++++++----- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/hknweb/events/models.py b/hknweb/events/models.py index 3c4d6a21..e0af1005 100644 --- a/hknweb/events/models.py +++ b/hknweb/events/models.py @@ -41,7 +41,13 @@ def __repr__(self): def __str__(self): return self.name - + def admitted_set(self): + return self.rsvp_set.order_by("created_at")[:self.rsvp_limit] + + def waitlist_set(self): + if not self.rsvp_limit: + return self.rsvp_set.none() + return self.rsvp_set.order_by("created_at")[self.rsvp_limit:] class Rsvp(models.Model): user = models.ForeignKey(User, models.CASCADE, verbose_name="rsvp'd by") event = models.ForeignKey(Event, models.CASCADE) diff --git a/hknweb/events/templates/events/show_details.html b/hknweb/events/templates/events/show_details.html index 33b593f3..a0cd921b 100644 --- a/hknweb/events/templates/events/show_details.html +++ b/hknweb/events/templates/events/show_details.html @@ -64,8 +64,10 @@

{{ event.name }}

{% endif %} - - {% if rsvpd %} + + {% if waitlisted %} +

You are waitlisted. You are at position {{waitlist_position}}

+ {% elif rsvpd %}

You have rsvp'd rsvp'd

{% else %}

You have not rsvp'd

@@ -81,7 +83,7 @@

RSVPs

Count: {{ rsvps.count }}

{% endif %} {% for rsvp in rsvps %} -

{{ rsvp.user.first_name }} {{ rsvp.user.last_name }} ({{ rsvp.user }})

+

{{ rsvp.user.first_name }} {{ rsvp.user.last_name }} ({{ rsvp.user.username }})

{% endfor %} {% elif not rsvpd %}

No rsvps.

diff --git a/hknweb/events/views.py b/hknweb/events/views.py index c33048b5..1e29f08f 100644 --- a/hknweb/events/views.py +++ b/hknweb/events/views.py @@ -44,15 +44,30 @@ def index(request): def show_details(request, id): event = get_object_or_404(Event, pk=id) - - rsvpd = Rsvp.objects.filter(user=request.user, event=event).exists() rsvps = Rsvp.objects.filter(event=event) - + rsvpd = Rsvp.objects.filter(user=request.user, event=event).exists() + waitlisted = False + waitlist_position = 0 + if (rsvpd): + #Gets the rsvp object for the user + rsvp = Rsvp.objects.filter(user=request.user, event=event)[:1].get() + #Check if waitlisted + if event.rsvp_limit: + rsvps_before = rsvps.filter(created_at__lt = rsvp.created_at).count() + waitlisted = rsvps_before >= event.rsvp_limit + #get waitlist position + if waitlisted: + position = rsvps.filter(created_at__lt=rsvp.created_at).count() + waitlist_position = position - event.rsvp_limit + 1 + #render only non-waitlisted rsvps + rsvps = Rsvp.objects.order_by('created_at')[:event.rsvp_limit] limit = event.rsvp_limit context = { 'event': event, 'rsvpd': rsvpd, 'rsvps': rsvps, + 'waitlisted': waitlisted, + 'waitlist_position': waitlist_position, 'limit': limit, 'can_edit': request.user.has_perm('events.change_event') } @@ -68,11 +83,10 @@ def rsvp(request, id): event = get_object_or_404(Event, pk=id) rsvps = event.rsvp_set.count() - if request.user.is_authenticated and (event.rsvp_limit is None or rsvps < event.rsvp_limit) \ - and Rsvp.has_not_rsvpd(request.user, event): + if request.user.is_authenticated and Rsvp.has_not_rsvpd(request.user, event): Rsvp.objects.create(user=request.user, event=event, confirmed=False) else: - messages.error(request, 'Could not RSVP; the RSVP limit has been reached or you have already RSVP\'d.') + messages.error(request, 'You have already RSVP\'d.') return redirect('/events/' + str(id)) From 287bfcb4ebe11c8e969e50fc67f549a5a33d2a7d Mon Sep 17 00:00:00 2001 From: Kevin Date: Sat, 14 Mar 2020 12:15:28 -0700 Subject: [PATCH 2/3] Filter non-waitlisted rsvps by event --- hknweb/events/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hknweb/events/views.py b/hknweb/events/views.py index 1e29f08f..fc2eadea 100644 --- a/hknweb/events/views.py +++ b/hknweb/events/views.py @@ -60,7 +60,7 @@ def show_details(request, id): position = rsvps.filter(created_at__lt=rsvp.created_at).count() waitlist_position = position - event.rsvp_limit + 1 #render only non-waitlisted rsvps - rsvps = Rsvp.objects.order_by('created_at')[:event.rsvp_limit] + rsvps = Rsvp.objects.filter(event=event).order_by('created_at')[:event.rsvp_limit] limit = event.rsvp_limit context = { 'event': event, From dc03927a06f8a92e5d813c4bd7f96d991d853f8d Mon Sep 17 00:00:00 2001 From: Kevin Date: Tue, 17 Mar 2020 11:21:24 -0700 Subject: [PATCH 3/3] Updated PR with requested changes --- hknweb/events/models.py | 2 ++ .../events/templates/events/show_details.html | 29 ++++++++++--------- hknweb/events/views.py | 13 +++++---- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/hknweb/events/models.py b/hknweb/events/models.py index e0af1005..d73736a8 100644 --- a/hknweb/events/models.py +++ b/hknweb/events/models.py @@ -41,6 +41,7 @@ def __repr__(self): def __str__(self): return self.name + def admitted_set(self): return self.rsvp_set.order_by("created_at")[:self.rsvp_limit] @@ -48,6 +49,7 @@ def waitlist_set(self): if not self.rsvp_limit: return self.rsvp_set.none() return self.rsvp_set.order_by("created_at")[self.rsvp_limit:] + class Rsvp(models.Model): user = models.ForeignKey(User, models.CASCADE, verbose_name="rsvp'd by") event = models.ForeignKey(Event, models.CASCADE) diff --git a/hknweb/events/templates/events/show_details.html b/hknweb/events/templates/events/show_details.html index a0cd921b..f9981ab7 100644 --- a/hknweb/events/templates/events/show_details.html +++ b/hknweb/events/templates/events/show_details.html @@ -66,28 +66,31 @@

{{ event.name }}

{% endif %} {% if waitlisted %} -

You are waitlisted. You are at position {{waitlist_position}}

+

You are at position {{waitlist_position}} on the waitlist. rsvp'd

{% elif rsvpd %}

You have rsvp'd rsvp'd

{% else %} -

You have not rsvp'd

+

You have not rsvp'd

{% endif %}

RSVPs

- {% if rsvps %} - {% if limit %} -

Count: {{ rsvps.count }} / {{ limit }}

- {% else %} -

Count: {{ rsvps.count }}

- {% endif %} - {% for rsvp in rsvps %} -

{{ rsvp.user.first_name }} {{ rsvp.user.last_name }} ({{ rsvp.user.username }})

- {% endfor %} - {% elif not rsvpd %} -

No rsvps.

+ {% if limit %} +

Count: {{ rsvps.count }} / {{ limit }}

+ {% else %} +

Count: {{ rsvps.count }}

{% endif %} + {% for rsvp in rsvps %} +

{{ rsvp.user.first_name }} {{ rsvp.user.last_name }} ({{ rsvp.user.username }})

+ {% endfor %} + +

Waitlist

+ +

Count: {{waitlists.count}}

+ {% for waitlist in waitlists %} +

{{ waitlist.user.first_name }} {{ waitlist.user.last_name }} ({{ waitlist.user.username }})

+ {% endfor %} diff --git a/hknweb/events/views.py b/hknweb/events/views.py index fc2eadea..64f284b0 100644 --- a/hknweb/events/views.py +++ b/hknweb/events/views.py @@ -49,18 +49,19 @@ def show_details(request, id): waitlisted = False waitlist_position = 0 if (rsvpd): - #Gets the rsvp object for the user + # Gets the rsvp object for the user rsvp = Rsvp.objects.filter(user=request.user, event=event)[:1].get() - #Check if waitlisted + # Check if waitlisted if event.rsvp_limit: rsvps_before = rsvps.filter(created_at__lt = rsvp.created_at).count() waitlisted = rsvps_before >= event.rsvp_limit - #get waitlist position + # Get waitlist position if waitlisted: position = rsvps.filter(created_at__lt=rsvp.created_at).count() waitlist_position = position - event.rsvp_limit + 1 - #render only non-waitlisted rsvps - rsvps = Rsvp.objects.filter(event=event).order_by('created_at')[:event.rsvp_limit] + # Render only non-waitlisted rsvps + rsvps = event.admitted_set() + waitlists = event.waitlist_set() limit = event.rsvp_limit context = { 'event': event, @@ -68,6 +69,7 @@ def show_details(request, id): 'rsvps': rsvps, 'waitlisted': waitlisted, 'waitlist_position': waitlist_position, + 'waitlists': waitlists, 'limit': limit, 'can_edit': request.user.has_perm('events.change_event') } @@ -81,7 +83,6 @@ def rsvp(request, id): raise Http404() event = get_object_or_404(Event, pk=id) - rsvps = event.rsvp_set.count() if request.user.is_authenticated and Rsvp.has_not_rsvpd(request.user, event): Rsvp.objects.create(user=request.user, event=event, confirmed=False)