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

[WIP] I147 backend for the user statistics page #162

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class Meta:
fields = [
"id",
"user",
"questions_created",
"average_score",
"quizzes_completed",
]

def create(self, validated_data):
Expand Down
Empty file.
3 changes: 3 additions & 0 deletions server/api/apps/user_statistics/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
Empty file.
3 changes: 3 additions & 0 deletions server/api/apps/user_statistics/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
52 changes: 52 additions & 0 deletions server/api/apps/user_statistics/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from django.test import TestCase
from rest_framework.test import APITestCase
from django.contrib.auth import get_user_model
from django.urls import reverse
from api.apps.shared_models.models import statistics_models, quiz_models

# Create your tests here.


class UserStatisticsTest(APITestCase):
def setUp(self):
self.user = get_user_model().objects.create_user(
"[email protected]",
"password",
grade="50",
first_name="Sam",
last_name="Sean",
)
self.client.login(email="[email protected]", password="password")
US = statistics_models.UserStatistics.objects.create(user=self.user)
QS = statistics_models.QuizStatistics.objects.create(user=self.user, quiz_title="Math Quiz", score=100)
Q = quiz_models.Question.objects.create(question_type="MC", marks=100, creator=self.user)

def test_user_statistics(self):
US = statistics_models.UserStatistics.objects.get(
user__first_name="Sam"
)
response = self.client.get(
reverse("user_statistics:stats", kwargs={"user_pk": US.user.pk})
)
print(response.data)

def test_quiz_list(self):
QS = statistics_models.UserStatistics.objects.get(
user__first_name="Sam"
)
response = self.client.get(
reverse("user_statistics:quiz-list", kwargs={"user_pk": QS.user.pk})
)
print(response.data)

def test_question_list(self):
Q = statistics_models.UserStatistics.objects.get(
user__first_name="Sam"
)
response = self.client.get(
reverse("user_statistics:question-list", kwargs={"user_pk": Q.user.pk})
)
print(response.data)



13 changes: 13 additions & 0 deletions server/api/apps/user_statistics/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.urls import path
from . import views

app_name = "user_statistics"
urlpatterns = [
path(
"user/<int:user_pk>/",
views.UserStatisticsDetailView.as_view(),
name="stats",
),
path("user/<int:user_pk>/quiz_list/",views.QuizStatisticsListView.as_view(),name="quiz-list"),
path("user/<int:user_pk>/question_list/",views.QuestionListView.as_view(), name="question-list")
]
32 changes: 32 additions & 0 deletions server/api/apps/user_statistics/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from django.shortcuts import render
from rest_framework import generics, permissions
from api.apps.shared_models.serializers import statistics_serializers, quiz_serializers
from api.apps.shared_models.models import statistics_models, quiz_models

# Create your views here.
class UserStatisticsDetailView(generics.RetrieveAPIView):
permission_classes = [permissions.IsAuthenticated]
serializer_class = statistics_serializers.UserStatisticsSerializer
lookup_url_kwarg = "user_pk"

def get_queryset(self):
return statistics_models.UserStatistics.objects.filter(
user__pk=self.kwargs["user_pk"]
)
class QuizStatisticsListView(generics.ListAPIView):
permission_classes = [permissions.IsAuthenticated]
serializer_class = statistics_serializers.QuizStatisticsSerializer

def get_queryset(self):
return statistics_models.QuizStatistics.objects.filter(
user__pk=self.kwargs["user_pk"]
)

class QuestionListView(generics.ListAPIView):
permission_classes = [permissions.IsAuthenticated]
serializer_class = quiz_serializers.QuestionSerializer

def get_queryset(self):
return quiz_models.Question.objects.filter(
creator__pk=self.kwargs["user_pk"]
)
2 changes: 1 addition & 1 deletion server/api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"rest_framework_jwt",
"api.apps.quizzes",
"api.apps.users",
"api.apps.user_statistics",
]

MIDDLEWARE = [
Expand Down Expand Up @@ -94,7 +95,6 @@

# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
Expand Down
1 change: 1 addition & 0 deletions server/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
path("api/users/", include("api.apps.users.urls")),
path("api/quizzes/", include("api.apps.quizzes.urls")),
path("api/take-quiz/", include("api.apps.quiz_take.urls")),
path("api/user-statistics", include("api.apps.user_statistics.urls")),
]