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
Showing only changes of commit 691c53e110 - Show all commits

View File

@ -10,13 +10,13 @@ from django.shortcuts import get_object_or_404
def bot_discord_rank(request): def bot_discord_rank(request):
if request.method != 'GET': if request.method != 'GET':
return JsonResponse({'error':'bad request'}) return JsonResponse({'error':'bad request'})
token = request.GET.get('token') token = request.GET.get('token')
auth_token = os.getenv('BOT_TOKEN') auth_token = os.getenv('BOT_TOKEN')
if (token != auth_token or not auth_token): if (token != auth_token or not auth_token):
return JsonResponse({'error':'not authorized'}) return JsonResponse({'error':'not authorized'})
all_users = UserProfileInfo.objects.select_related().order_by('-score', 'last_submission_date', 'user__username') all_users = UserProfileInfo.objects.select_related().order_by('-score', 'last_submission_date', 'user__username')
data = {} data = {}
rank = 1 rank = 1
@ -30,30 +30,30 @@ def bot_discord_rank(request):
def bot_discord_campus(request): def bot_discord_campus(request):
if request.method != 'GET': if request.method != 'GET':
return JsonResponse({'error':'bad request'}) return JsonResponse({'error':'bad request'})
token = request.GET.get('token') token = request.GET.get('token')
auth_token = os.getenv('BOT_TOKEN') auth_token = os.getenv('BOT_TOKEN')
if (token != auth_token or not auth_token): if (token != auth_token or not auth_token):
return JsonResponse({'error':'not authorized'}) return JsonResponse({'error':'not authorized'})
all_users = UserProfileInfo.objects.select_related().order_by('-score', 'last_submission_date', 'user__username') all_users = UserProfileInfo.objects.select_related().order_by('-score', 'last_submission_date', 'user__username')
data = {} data = {}
for user in all_users: for user in all_users:
if user.discord_id: if user.campus and user.discord_id:
data[user.discord_id] = user.campus data[user.discord_id] = user.campus.name
return JsonResponse(data) return JsonResponse(data)
def events_data(request, event_slug): def events_data(request, event_slug):
if request.method != 'GET': if request.method != 'GET':
return JsonResponse({'error':'bad request'}) return JsonResponse({'error':'bad request'})
event_info = get_object_or_404(Event, slug=event_slug) event_info = get_object_or_404(Event, slug=event_slug)
if event_info.password and request.GET.get('password') != event_info.password: if event_info.password and request.GET.get('password') != event_info.password:
return JsonResponse({'error':'not authorized'}) return JsonResponse({'error':'not authorized'})
players = EventPlayer.objects.filter(event=event_info) players = EventPlayer.objects.filter(event=event_info)
data = {} data = {}
@ -67,7 +67,6 @@ def events_data(request, event_slug):
else: else:
for player in players: for player in players:
data[player.user.username] = player.score data[player.user.username] = player.score
return JsonResponse(data) return JsonResponse(data)