Skip to content

Commit

Permalink
Merge pull request compserv#194 from compserv/waitlist
Browse files Browse the repository at this point in the history
Finished waitlisting
  • Loading branch information
chevin-ken authored Mar 19, 2020
2 parents f09c1f1 + d4fcba8 commit 95adf85
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 21 deletions.
8 changes: 8 additions & 0 deletions hknweb/events/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ 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)
Expand Down
33 changes: 19 additions & 14 deletions hknweb/events/templates/events/show_details.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,28 +64,33 @@ <h1 id="event-detail-title"> {{ event.name }} </h1>
<input type="submit" value="RSVP" />
</form>
{% endif %}

{% if rsvpd %}

{% if waitlisted %}
<p class="rsvp-list">You are at position {{waitlist_position}} on the waitlist. <img src="{% static 'candidate/maybe.png'%}" alt="rsvp'd" class="table-icon"></p>
{% elif rsvpd %}
<p class="rsvp-list">You have rsvp'd <img src="{% static 'candidate/yes.png'%}" alt="rsvp'd" class="table-icon"></p>
{% else %}
<p class="rsvp-list">You have not rsvp'd</p>
<p class="rsvp-list">You have not rsvp'd </p>
{% endif %}


<h3 class="rsvp-list-title">RSVPs</h3>

{% if rsvps %}
{% if limit %}
<p class="rsvp-list"> Count: {{ rsvps.count }} / {{ limit }} </p>
{% else %}
<p class="rsvp-list"> Count: {{ rsvps.count }} </p>
{% endif %}
{% for rsvp in rsvps %}
<p class="rsvp-list">{{ rsvp.user.first_name }} {{ rsvp.user.last_name }} ({{ rsvp.user.username }})</p>
{% endfor %}
{% elif not rsvpd %}
<p class="rsvp-list">No rsvps.</p>
{% if limit %}
<p class="rsvp-list"> Count: {{ rsvps.count }} / {{ limit }} </p>
{% else %}
<p class="rsvp-list"> Count: {{ rsvps.count }} </p>
{% endif %}
{% for rsvp in rsvps %}
<p class="rsvp-list">{{ rsvp.user.first_name }} {{ rsvp.user.last_name }} ({{ rsvp.user.username }})</p>
{% endfor %}

<h3 class="rsvp-list-title">Waitlist</h3>

<p class="rsvp-list"> Count: {{waitlists.count}}</p>
{% for waitlist in waitlists %}
<p class="rsvp-list">{{ waitlist.user.first_name }} {{ waitlist.user.last_name }} ({{ waitlist.user.username }})</p>
{% endfor %}
</div>
</div>

Expand Down
29 changes: 22 additions & 7 deletions hknweb/events/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,32 @@ 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 = event.admitted_set()
waitlists = event.waitlist_set()
limit = event.rsvp_limit
context = {
'event': event,
'rsvpd': rsvpd,
'rsvps': rsvps,
'waitlisted': waitlisted,
'waitlist_position': waitlist_position,
'waitlists': waitlists,
'limit': limit,
'can_edit': request.user.has_perm('events.change_event')
}
Expand All @@ -66,13 +83,11 @@ 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 (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))


Expand Down

0 comments on commit 95adf85

Please sign in to comment.