From 5d84174db22036e1c6fa1b92b08d7c8746e749ca Mon Sep 17 00:00:00 2001 From: Danhia Date: Mon, 6 Feb 2023 19:41:48 +0100 Subject: [PATCH] Added new endpoint to retrieve campus associated to discord id --- src/api/urls.py | 4 +++- src/api/views.py | 21 +++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/api/urls.py b/src/api/urls.py index 5c1ae76..04d983e 100644 --- a/src/api/urls.py +++ b/src/api/urls.py @@ -2,6 +2,8 @@ from django.urls import path from . import views urlpatterns = [ - path('bot/discord', views.discord_bot, name='discord_bot'), + path('bot/discord', views.bot_discord_rank, name='bot_discord_rank'), # legacy, to remove when new bot is deployed + path('bot/discord/rank', views.bot_discord_rank, name='bot_discord_rank'), # use this + path('bot/discord/campus', views.bot_discord_campus, name='bot_discord_campus'), path('events/', views.events_data, name='events_data'), ] diff --git a/src/api/views.py b/src/api/views.py index 1a5fe45..0d22b53 100644 --- a/src/api/views.py +++ b/src/api/views.py @@ -7,8 +7,7 @@ from django.shortcuts import get_object_or_404 # Create your views here. - -def discord_bot(request): +def bot_discord_rank(request): if request.method != 'GET': return JsonResponse({'error':'bad request'}) @@ -28,6 +27,24 @@ def discord_bot(request): return JsonResponse(data) +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 + + return JsonResponse(data) + def events_data(request, event_slug): if request.method != 'GET': return JsonResponse({'error':'bad request'})