added endpoint API for bot

This commit is contained in:
Danhia 2022-02-17 12:40:40 +01:00
parent 58cd330354
commit 9d55eb688b
2 changed files with 34 additions and 12 deletions

View File

@ -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'),
]

View File

@ -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':
@ -182,3 +172,34 @@ def delete_account(request):
else:
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)