From 691c53e110cb6970cb687641043b541d3c3797f0 Mon Sep 17 00:00:00 2001 From: Starthur Date: Tue, 7 Feb 2023 10:15:30 +0100 Subject: [PATCH] fix(api): Check if user has campus before add to response --- src/api/views.py | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/api/views.py b/src/api/views.py index 0d22b53..7f43193 100644 --- a/src/api/views.py +++ b/src/api/views.py @@ -10,13 +10,13 @@ from django.shortcuts import get_object_or_404 def bot_discord_rank(request): if request.method != 'GET': return JsonResponse({'error':'bad request'}) - + token = request.GET.get('token') auth_token = os.getenv('BOT_TOKEN') if (token != auth_token or not auth_token): return JsonResponse({'error':'not authorized'}) - + all_users = UserProfileInfo.objects.select_related().order_by('-score', 'last_submission_date', 'user__username') data = {} rank = 1 @@ -30,30 +30,30 @@ def bot_discord_rank(request): def bot_discord_campus(request): if request.method != 'GET': return JsonResponse({'error':'bad request'}) - + token = request.GET.get('token') auth_token = os.getenv('BOT_TOKEN') if (token != auth_token or not auth_token): return JsonResponse({'error':'not authorized'}) - + all_users = UserProfileInfo.objects.select_related().order_by('-score', 'last_submission_date', 'user__username') data = {} for user in all_users: - if user.discord_id: - data[user.discord_id] = user.campus + if user.campus and user.discord_id: + data[user.discord_id] = user.campus.name return JsonResponse(data) def events_data(request, event_slug): if request.method != 'GET': return JsonResponse({'error':'bad request'}) - + event_info = get_object_or_404(Event, slug=event_slug) - + if event_info.password and request.GET.get('password') != event_info.password: return JsonResponse({'error':'not authorized'}) - + players = EventPlayer.objects.filter(event=event_info) data = {} @@ -67,7 +67,6 @@ def events_data(request, event_slug): else: for player in players: data[player.user.username] = player.score - + return JsonResponse(data) - \ No newline at end of file