Skip to content

Commit

Permalink
Make it possible to stop registrations
Browse files Browse the repository at this point in the history
  • Loading branch information
wil93 committed Dec 1, 2023
1 parent 815bcb2 commit fd75782
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
3 changes: 3 additions & 0 deletions itacpc/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
ACCOUNT_ADAPTER = 'teams.adapters.StudentAccountAdapter'

# Temporary way to close registrations (needs to be implemented better)
REGISTRATION_IS_CLOSED = eval(os.getenv("REGISTRATION_IS_CLOSED", default="False"))

# Prod stuff
if not DEBUG:
SECURE_HSTS_PRELOAD = True
Expand Down
26 changes: 23 additions & 3 deletions teams/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from django import forms
from django.conf import settings
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.core.exceptions import PermissionDenied
from django.db import transaction
from django.db.models import Count, Q, F
from django.db.models import Count
from django.shortcuts import get_object_or_404, redirect, render
from django.http import HttpResponse, HttpResponsePermanentRedirect, HttpResponseRedirect
from django.http import HttpResponsePermanentRedirect, HttpResponseRedirect
from django.utils.crypto import get_random_string
from teams.models import TeamJoinEvent, User, Team, University
from allauth.account.views import SignupView
Expand Down Expand Up @@ -155,6 +155,10 @@ def save(self, request):
success_url = None

def post(self, request, *args, **kwargs):
if settings.REGISTRATION_IS_CLOSED:
messages.error(request, 'It is too late now to register.')
return redirect('university', university_short_name=kwargs['university_short_name'])

form_class = self.get_form_class()
form = self.get_form(form_class)
if form.is_valid():
Expand All @@ -166,6 +170,10 @@ def post(self, request, *args, **kwargs):
)

def get(self, request, university_short_name, **kwargs):
if settings.REGISTRATION_IS_CLOSED:
messages.error(request, 'It is too late now to register.')
return redirect('university', university_short_name=university_short_name)

kwargs['university_short_name'] = university_short_name
return self.render_to_response(self.get_context_data(**kwargs))

Expand Down Expand Up @@ -196,6 +204,10 @@ class Meta:
model = Team
fields = ["name"]

if settings.REGISTRATION_IS_CLOSED:
messages.error(request, 'It is too late now to make changes to the teams.')
return redirect('my-profile')

university = get_object_or_404(University, short_name=university_short_name)
if university != request.user.university:
messages.error(request, 'You can only create teams in your own university!')
Expand Down Expand Up @@ -236,6 +248,10 @@ class Meta:
def join_team(request, secret):
MAX_MEMBERS = 3

if settings.REGISTRATION_IS_CLOSED:
messages.error(request, 'It is too late now to make changes to the teams.')
return redirect('my-profile')

team = get_object_or_404(Team, secret=secret)

if team.university != request.user.university:
Expand Down Expand Up @@ -275,6 +291,10 @@ def join_team(request, secret):
@login_required
def leave_team(request):
if request.method == "POST":
if settings.REGISTRATION_IS_CLOSED:
messages.error(request, 'It is too late now to make changes to the teams.')
return redirect('my-profile')

# Run in a transaction to prevent race conditions (e.g. someone else
# joins the team right before we check if the team is empty)
with transaction.atomic():
Expand Down

0 comments on commit fd75782

Please sign in to comment.