def undisqualify_team(request): # Check for POST request if request.method == 'POST': # Check for valid input form = IdForm(data=request.POST) if form.is_valid(): # Retrieve data team_id = form.cleaned_data['id'] # Verify if the team exists team = Team.objects.filter(pk=team_id) if team: # Remove disqualification team = Team.objects.get(pk=team_id) team.is_disqualified = False team.save() return HttpResponse(status=HTTP_200_OK) # Team doesn't exist else: return HttpResponse(status=HTTP_404_NOT_FOUND) # POST data invalid else: return HttpResponse(status=HTTP_400_BAD_REQUEST) # Request is not a POST request else: return HttpResponse(status=HTTP_405_METHOD_NOT_ALLOWED)
def add_team(request): # Only allow POST requests if request.method == "POST": # Check if the POST data is valid form = IdForm(data=request.POST) if form.is_valid(): # Check if the event exists event_id = form.cleaned_data['id'] event = Event.objects.filter(pk=event_id) if event: # Create a team team = Team() team.is_winner = False team.is_winner = False team.event_id = event_id team.timer_started = False team.save() return HttpResponse(status=HTTP_200_OK) # The event doesn't exist else: return HttpResponse(status=HTTP_404_NOT_FOUND) # Request data is invalid else: return HttpResponse(HTTP_400_BAD_REQUEST) # If the request type is not POST else: return HttpResponse(status=HTTP_405_METHOD_NOT_ALLOWED)
def delete_challenge(request): if request.method == "POST": # Check if the POST data is valid form = IdForm(data=request.POST) if form.is_valid(): # Get the sub-destination ID challenge_id = form.cleaned_data['id'] # Check if the sub-destination exists challenge = Challenge.objects.filter(pk=challenge_id) if challenge: # Get challenge challenge = Challenge.objects.get(pk=challenge_id) # Delete associated team challenges TeamChallenge.objects.filter( challenge_id=challenge_id).delete() # Delete challenge challenge.delete() return HttpResponse(status=HTTP_200_OK) # The sub-destination doesn't exist else: return HttpResponse(status=HTTP_404_NOT_FOUND) # The provided ID is not valid else: return HttpResponse(status=HTTP_400_BAD_REQUEST) # Not a POST request else: return HttpResponse(status=HTTP_405_METHOD_NOT_ALLOWED)
def add_sublocation(request): # Check for permission if request.user.is_superuser: if request.method == "POST": # Check for valid input form = IdForm(data=request.POST) if form.is_valid(): # Retrieve data id = form.cleaned_data["id"] event = Event.objects.filter(pk=id) if event: # Create a new location location = Location() # Give it Eindhoven coordinates (arbitrary) location.latitude = 51.43 location.longitude = 5.47 location.save() # Create a new sub-destination and return it sublocation = SubLocation() sublocation.location_id = location.pk sublocation.event_id = id sublocation.city = "Eindhoven" # Retrieve the order of the last sub-destination for the event last_sublocation = SubLocation.objects.filter(event_id=id) if last_sublocation: last_sublocation = SubLocation.objects.filter( event_id=id).order_by('-order')[0] sublocation.order = last_sublocation.order + 1 else: sublocation.order = 1 sublocation.save() return HttpResponse(status=HTTP_200_OK) # Event does not exist else: return HttpResponse(status=HTTP_404_NOT_FOUND) # The POST data is invalid else: return HttpResponse(status=HTTP_400_BAD_REQUEST) # The request is not a POST request else: return HttpResponse(status=HTTP_405_METHOD_NOT_ALLOWED) # The user is not the superuser else: return HttpResponse(status=HTTP_403_FORBIDDEN)
def delete_team(request): if request.method == "POST": # Check if user is an administrator if request.user.is_staff: # Check if POST data is valid form = IdForm(data=request.POST) if form.is_valid(): # Get Team ID team_id = form.cleaned_data["id"] # Check if team exists team = Team.objects.filter(pk=team_id) if team: # Retrieve team team = Team.objects.get(pk=team_id) # Delete associated scores scores = Score.objects.filter(team_id=team.pk) for score in scores: score.delete() # Delete associated locations team_locations = TeamLocation.objects.filter( team_id=team.pk) for team_location in team_locations: team_location.delete() # Delete team members team_members = UserTeam.objects.filter(team_id=team.pk) for team_member in team_members: team_member.delete() # Delete team team.delete() return HttpResponse(status=HTTP_200_OK) # Team doesn't exist else: return HttpResponse(status=HTTP_404_NOT_FOUND) # POST data invalid else: return HttpResponse(HTTP_400_BAD_REQUEST) # User is not an administrator else: return HttpResponse(status=HTTP_403_FORBIDDEN) else: return HttpResponse(status=HTTP_405_METHOD_NOT_ALLOWED)
def delete_user(request): if request.user.is_superuser: if request.method == "POST": form = IdForm(data=request.POST) if form.is_valid(): id = form.cleaned_data['id'] user = get_object_or_404(User, pk=id) # Delete the teams the user is in UserTeam.objects.filter(user_id=user.pk).delete() # Delete the user user.delete() return HttpResponse(status=HTTP_200_OK) else: return HttpResponse(status=HTTP_400_BAD_REQUEST) else: return HttpResponse(status=HTTP_405_METHOD_NOT_ALLOWED) else: return HttpResponse(status=HTTP_403_FORBIDDEN)
def delete_event_sublocation(request): if request.method == "POST": # Check if the user is the superuser if request.user.is_superuser: # Check if the POST data is valid form = IdForm(data=request.POST) if form.is_valid(): # Get the sub-destination ID sublocation_id = form.cleaned_data['id'] # Check if the sub-destination exists sub_destination = SubLocation.objects.filter(pk=sublocation_id) if sub_destination: # Get sub-destination sub_destination = SubLocation.objects.get( pk=sublocation_id) # Retrieve the other sub-destinations sub_locations = SubLocation.objects.filter( event_id=sub_destination.event_id) # Update the order for the other sub-destinations for location in sub_locations: if location.order > sub_destination.order: location.order = location.order - 1 location.save() # Delete sub-destination sub_destination.delete() return HttpResponse(status=HTTP_200_OK) # The sub-destination doesn't exist else: return HttpResponse(status=HTTP_404_NOT_FOUND) # The provided ID is not valid else: return HttpResponse(status=HTTP_400_BAD_REQUEST) # Requesting user is not the superuser else: return HttpResponse(status=HTTP_403_FORBIDDEN) # Not a GET request else: return HttpResponse(status=HTTP_405_METHOD_NOT_ALLOWED)