fix(api): Check if user has campus before add to response #85

Merged
Danhia merged 1 commits from Starthur/website:main into main 2023-09-01 11:09:53 +02:00
1 changed files with 10 additions and 11 deletions

View File

@ -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)