forked from 42CTF/website
added endpoint API for bot
This commit is contained in:
parent
a7de7b8054
commit
a0b76903a7
|
@ -14,4 +14,5 @@ urlpatterns = [
|
||||||
path('connections/connect/discord/authorize', views.connection.authorize, name='connections-connect-discord-authorize'),
|
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('connections/disconnect/discord', views.connection.disconnect, name='connections-disconnect-discord'),
|
||||||
path('delete_account/', views.delete_account, name='delete_account'),
|
path('delete_account/', views.delete_account, name='delete_account'),
|
||||||
|
path('api/bot/', views.api_bot, name='api_bot'),
|
||||||
]
|
]
|
||||||
|
|
|
@ -16,6 +16,7 @@ from secrets import token_hex
|
||||||
from accounts.models import UserProfileInfo
|
from accounts.models import UserProfileInfo
|
||||||
|
|
||||||
from django.contrib.auth.models import timezone
|
from django.contrib.auth.models import timezone
|
||||||
|
import os
|
||||||
|
|
||||||
from . import connection
|
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,
|
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})
|
'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
|
@login_required
|
||||||
def delete_account(request):
|
def delete_account(request):
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
|
@ -182,3 +172,34 @@ def delete_account(request):
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return render(request, 'accounts/delete.html', {'deleted': False, 'bad_password': False} )
|
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)
|
Loading…
Reference in New Issue