From a0b76903a79fbe139f7355faffe740c32b3ab005 Mon Sep 17 00:00:00 2001 From: Danhia Date: Thu, 17 Feb 2022 12:40:40 +0100 Subject: [PATCH] added endpoint API for bot --- src/accounts/urls.py | 1 + src/accounts/views/views.py | 45 +++++++++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/accounts/urls.py b/src/accounts/urls.py index abf5ec9..2ba5a2b 100644 --- a/src/accounts/urls.py +++ b/src/accounts/urls.py @@ -14,4 +14,5 @@ urlpatterns = [ path('connections/connect/discord/authorize', views.connection.authorize, name='connections-connect-discord-authorize'), path('connections/disconnect/discord', views.connection.disconnect, name='connections-disconnect-discord'), path('delete_account/', views.delete_account, name='delete_account'), + path('api/bot/', views.api_bot, name='api_bot'), ] diff --git a/src/accounts/views/views.py b/src/accounts/views/views.py index 806ad75..f81a05b 100644 --- a/src/accounts/views/views.py +++ b/src/accounts/views/views.py @@ -16,6 +16,7 @@ from secrets import token_hex from accounts.models import UserProfileInfo from django.contrib.auth.models import timezone +import os from . import connection @@ -155,17 +156,6 @@ def profile(request, user_name): return render(request,'accounts/profile.html', {'user':user_obj, 'solves':solves,'solved':solved,'catsDatas': catsDatas, 'pointDatas': pointDatas, 'rank': rank, 'score' : somme, 'member' : member, 'cats':cats}) -def rank(request, token): - all_users = UserProfileInfo.objects.filter(score__gt=0).select_related().order_by('-score', 'last_submission_date', 'user__username') - - rank = 1 - for elem in all_users: - if elem.token == token: - break - rank += 1 - data = {"rank": rank} - return JsonResponse(data) - @login_required def delete_account(request): if request.method == 'POST': @@ -181,4 +171,35 @@ def delete_account(request): return render(request, 'accounts/delete.html', {'deleted': False, 'bad_password': True}) else: - return render(request, 'accounts/delete.html', {'deleted': False, 'bad_password': False} ) \ No newline at end of file + return render(request, 'accounts/delete.html', {'deleted': False, 'bad_password': False} ) + +def rank(request, token): + all_users = UserProfileInfo.objects.filter(score__gt=0).select_related().order_by('-score', 'last_submission_date', 'user__username') + + rank = 1 + for elem in all_users: + if elem.token == token: + break + rank += 1 + data = {"rank": rank} + return JsonResponse(data) + +def api_bot(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 + for user in all_users: + if user.discord_id: + data[user.discord_id] = rank + rank += 1 + + return JsonResponse(data) \ No newline at end of file