forked from 42CTF/website
added API for events data
This commit is contained in:
parent
280ba0d1fc
commit
d554961dd1
|
@ -3,4 +3,5 @@ from . import views
|
|||
|
||||
urlpatterns = [
|
||||
path('bot/discord', views.discord_bot, name='discord_bot'),
|
||||
path('events/<str:event_slug>', views.events_data, name='events_data'),
|
||||
]
|
||||
|
|
|
@ -2,6 +2,8 @@ from django.shortcuts import render
|
|||
from accounts.models import UserProfileInfo
|
||||
from django.http import JsonResponse
|
||||
import os
|
||||
from events.models import Event, Team, EventPlayer
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
# Create your views here.
|
||||
|
||||
|
@ -24,4 +26,31 @@ def discord_bot(request):
|
|||
data[user.discord_id] = rank
|
||||
rank += 1
|
||||
|
||||
return JsonResponse(data)
|
||||
return JsonResponse(data)
|
||||
|
||||
def events_data(request, event_slug):
|
||||
if request.method != 'GET':
|
||||
return JsonResponse({'error':'bad request'})
|
||||
|
||||
event_info = get_object_or_404(Event, slug=event_slug)
|
||||
|
||||
if event_info.password and request.GET.get('password') != event_info.password:
|
||||
return JsonResponse({'error':'not authorized'})
|
||||
|
||||
players = EventPlayer.objects.filter(event=event_info)
|
||||
|
||||
data = {}
|
||||
if event_info.team_size > 1:
|
||||
for player in players:
|
||||
if not player.team:
|
||||
continue
|
||||
if not player.team.name in data:
|
||||
data[player.team.name] = []
|
||||
data[player.team.name].append({"name": player.user.username, "score": player.score})
|
||||
else:
|
||||
for player in players:
|
||||
data[player.user.username] = player.score
|
||||
|
||||
return JsonResponse(data)
|
||||
|
||||
|
Loading…
Reference in New Issue