Skip to content

Commit

Permalink
Allowing judges to enroll either as judge or participant but not both (
Browse files Browse the repository at this point in the history
  • Loading branch information
stefdworschak authored Jun 3, 2021
1 parent e12425a commit 6de9dcf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 26 deletions.
4 changes: 2 additions & 2 deletions hackathon/templates/hackathon/includes/enrollpart.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<form id="enroll-form" action="{% url 'hackathon:enroll_toggle' %}" method="POST">
{% csrf_token %}
<!--It only sends the hackathon ID.-->
<input type="hidden" name="hackathon-id" value="{{ hackathon.id }}" />
<input type="hidden" name="hackathon-id" value="{{ hackathon.id }}">
<!-- Must be one long string for the JS script to switch words correctly.
An alternative would be to have two different buttons.-->
<button id="enroll-part" type="submit"
Expand All @@ -21,7 +21,7 @@
{% if request.user in hackathon.participants.all %}
<form id="enroll-form" action="{% url 'hackathon:enroll_toggle' %}" method="POST">
{% csrf_token %}
<input type="hidden" name="hackathon-id" value="{{ hackathon.id }}" />
<input type="hidden" name="hackathon-id" value="{{ hackathon.id }}">
<button id="enroll-part" type="submit" class="btn btn-ci mt-4 mb-2 form-control">
Withdraw from the Hackaton
</button>
Expand Down
24 changes: 21 additions & 3 deletions hackathon/templates/hackathon/includes/enrollstaff.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{% if request.user not in hackathon.participants.all %}
<form id="enroll-form" action="{% url 'hackathon:enroll_toggle' %}" method="POST">
{% csrf_token %}
<!--It only sends the hackathon ID.-->
<input type="hidden" name="enrollment-type" value="judge" />
<input type="hidden" name="hackathon-id" value="{{ hackathon.id }}" />
<!-- Must be one long string for the JS script to switch words correctly.
An alternative would be to have two different buttons.-->
<button id="enroll-judge" type="submit" class="dropdown-item"
{% if hackathon.status == 'finished' or hackathon.status == 'deleted' %}
disabled
Expand All @@ -16,3 +15,22 @@
{% endif %}
</button>
</form>
{% endif %}
{% if request.user not in hackathon.judges.all %}
<form id="enroll-form" action="{% url 'hackathon:enroll_toggle' %}" method="POST">
{% csrf_token %}
<input type="hidden" name="hackathon-id" value="{{ hackathon.id }}" />
<button id="enroll-judge" type="submit" class="dropdown-item"
{% if hackathon.status == 'finished' or hackathon.status == 'deleted' %}
disabled
{% endif %}
>
{% if user in hackathon.participants.all %}
Withdraw as Participant
{% else %}
Enroll as Participant
{% endif %}
</button>
</form>
{% endif %}

45 changes: 24 additions & 21 deletions hackathon/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -398,29 +398,32 @@ def delete_hackathon(request, hackathon_id):

@login_required
def enroll_toggle(request):
user = request.user
data = {}
if request.method == "POST":
# Gets the PK of the Hackathon and then the related Hackathon
hackathon_id = request.POST.get("hackathon-id")
hackathon = Hackathon.objects.get(pk=hackathon_id)
if user.user_type == UserType.STAFF:
if user in hackathon.judges.all():
hackathon.judges.remove(user)
messages.success(request, "You have withdrawn from judging.")
else:
hackathon.judges.add(user)
messages.success(request, "You have enrolled as a judge.")
judge_user_types = [
UserType.SUPERUSER, UserType.STAFF, UserType.FACILITATOR_ADMIN,
UserType.FACILITATOR_JUDGE, UserType.PARTNER_ADMIN,
UserType.PARTNER_JUDGE,
]
hackathon = get_object_or_404(Hackathon,
id=request.POST.get("hackathon-id"))
if request.user in hackathon.judges.all():
hackathon.judges.remove(request.user)
messages.success(request, "You have withdrawn from judging.")
elif request.user in hackathon.participants.all():
hackathon.participants.remove(request.user)
messages.success(request,
"You have withdrawn from this Hackaton.")
elif (request.POST.get('enrollment-type') == 'judge'
and request.user.user_type in judge_user_types):
hackathon.judges.add(request.user)
messages.success(request, "You have enrolled as a judge.")
else:
if user in hackathon.participants.all():
hackathon.participants.remove(user)
messages.success(request,
"You have withdrawn from this Hackaton.")
else:
hackathon.participants.add(user)
messages.success(request, "You have enrolled successfully.")
return redirect(reverse('hackathon:view_hackathon',
kwargs={'hackathon_id': hackathon_id}))
hackathon.participants.add(request.user)
messages.success(request, "You have enrolled successfully.")

return redirect(reverse(
'hackathon:view_hackathon',
kwargs={'hackathon_id': request.POST.get("hackathon-id")}))
else:
return HttpResponse(status=403)

Expand Down

0 comments on commit 6de9dcf

Please sign in to comment.