Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add rank routes #51

Merged
merged 1 commit into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/masteriq/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

router.register("category", views.IQView, basename="category")
router.register("question", views.QuestionView, basename="question")
router.register("rank", views.RankView, basename="rank")

urlpatterns = [
path('admin/', admin.site.urls),
Expand Down
27 changes: 27 additions & 0 deletions api/masteriqapp/tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,30 @@ def test_route(self):
assert response.json()['options'] is not None
assert len(response.json()['options']) >= 2

response = c.get("/api/rank/1/leaderboard/")
assert response.status_code == 200
assert len(response.json()) > 0
assert response.json()[0]['user_id'] is not None
assert response.json()[0]['user_name'] is not None
assert response.json()[0]['user_iq'] is not None

response = c.get("/api/rank/global_leaderboard/")
assert response.status_code == 200
assert len(response.json()) > 0
assert response.json()[0]['user_id'] is not None
assert response.json()[0]['user_name'] is not None
assert response.json()[0]['user_iq'] is not None

response = c.get("/api/rank/1/user/")
assert response.status_code == 200
assert response.json()['user_rank'] is not None
assert response.json()['user_iq'] is not None

response = c.get("/api/rank/global_user/")
assert response.status_code == 200
assert response.json()['user_rank'] is not None
assert response.json()['user_iq'] is not None




54 changes: 54 additions & 0 deletions api/masteriqapp/views/RankView.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import random

from django.apps import apps
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework import status

masteriq = apps.get_app_config("masteriqapp")


class RankView(viewsets.ViewSet):
category_model = masteriq.get_model("Category")
question_model = masteriq.get_model("Question")
queryset = category_model.objects.all()

@action(detail=True, methods=["GET"])
def leaderboard(self, request, pk):
# TODO: get data from database when users are implemented
data_to_send = []
for i in range(1, 11):
data_to_send.append({
"user_id": random.randint(1, 1000),
"user_name": f"player_number_{i}",
"user_iq": 150 - (i * 10)
})

return Response(data=data_to_send, status=status.HTTP_200_OK)

@action(detail=False, methods=["GET"])
def global_leaderboard(self, request):
# TODO: get data from database when users are implemented
data_to_send = []
for i in range(1, 11):
data_to_send.append({
"user_id": random.randint(1, 1000),
"user_name": f"player_number_{i}",
"user_iq": 50 + (i * 10)
})

return Response(data=data_to_send, status=status.HTTP_200_OK)

@action(detail=True, methods=["GET"])
def user(self, request, pk):
# TODO: get data from database when users are implemented

data_to_send = {"user_rank": random.randint(1, 1000), "user_iq": random.randint(1, 200)}
return Response(data=data_to_send, status=status.HTTP_200_OK)

@action(detail=False, methods=["GET"])
def global_user(self, request):
# TODO: get data from database when users are implemented
data_to_send = {"user_rank": random.randint(1, 1000), "user_iq": random.randint(1, 200)}
return Response(data=data_to_send, status=status.HTTP_200_OK)
3 changes: 2 additions & 1 deletion api/masteriqapp/views/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .IQView import IQView
from .QuestionView import QuestionView
from .QuestionView import QuestionView
from .RankView import RankView
Loading