Skip to content
Snippets Groups Projects
Commit 34b9a13b authored by borzechof99's avatar borzechof99 :whale2:
Browse files

Implemented User-Frontend API

parent fa2a8726
No related branches found
No related tags found
No related merge requests found
......@@ -11,4 +11,6 @@ disable=line-too-long,
invalid-name,
no-else-raise,
arguments-renamed,
too-many-lines,
too-many-locals,
......@@ -3,6 +3,7 @@ Defines the views for the API
"""
import copy
import random
from rest_framework import viewsets
from rest_framework.views import APIView
from rest_framework.response import Response
......@@ -11,6 +12,7 @@ from django.http import HttpResponse
from django.db.models.functions import Lower
from .pagination import PageNumberWithPageSizePagination
from .course_scraper.course_scraper import scraping
from .serializers import (
......@@ -937,3 +939,125 @@ class EndView(GreetingEndView):
"""
given_object = EndText
class QuizView(APIView):
"""
View for providing Questions for Quiz and calculating top ten sports
"""
def get(self, request):
"""
Gets all Questions in their correct order with snacks and Activities inbetween them
"""
queryset = QuestionOrderEntry.objects.all().order_by("order_id")
data_dict = {
"start_text": GreetingText.objects.get(pk=1).text,
"end_text": EndText.objects.get(pk=1).text,
}
order_list = []
for order_entry in queryset:
entry_dict = {}
if order_entry.type_of_slot == "question":
entry_dict["pk"] = order_entry.question_id
entry_dict["type"] = order_entry.type_of_slot
entry_dict["text"] = Question.objects.get(
pk=order_entry.question_id
).text
elif order_entry.type_of_slot == "snack":
entry_dict["type"] = order_entry.type_of_slot
# Get random Snack:
random_snack = random.choice(list(KnowledgeSnack.objects.all()))
entry_dict["text"] = random_snack.text
entry_dict["url"] = request.build_absolute_uri(random_snack.image.url)
else:
entry_dict["type"] = order_entry.type_of_slot
# Get random Activity:
random_activity = random.choice(list(CallToMove.objects.all()))
entry_dict["text"] = random_activity.text
entry_dict["url"] = request.build_absolute_uri(
random_activity.image.url
)
order_list.append(entry_dict)
data_dict["order_list"] = order_list
return Response(status=200, data=data_dict)
def post(self, request):
"""
Gets Answers of Quiz and returns top ten Sports
pk: PK of Question
answer: Answer between 0 and 3
relevance: Answer between 0 and 5
"""
criterion_factors = []
for response in request.data:
print(response)
criterion = get_object_or_404(Question, pk=response["pk"]).criterion
factor = response["answer"] * response["relevance"]
criterion_factors.append((criterion, factor))
rated_sports = []
for sport in Sport.objects.iterator():
criteria_sum = 0
criteria_by_ratings = []
for criterion, factor in criterion_factors:
rating = sport.get_rating(criterion)
if rating != -1:
factored_rating = rating * factor
criteria_sum += factored_rating
criteria_by_ratings.append((criterion.name, factored_rating))
rated_sports.append((sport, criteria_sum, criteria_by_ratings))
sorted_sport_list = sorted(rated_sports, key=lambda x: x[1], reverse=True)
top_ten_sports = sorted_sport_list[:10]
data_list = []
for sport, criteria_sum, criteria_list in top_ten_sports:
sorted_criteria_list = sorted(
criteria_list, key=lambda x: x[1], reverse=True
)
top_criteria = sorted_criteria_list[:3]
data_list.append(
{
"name": sport.name,
"link": sport.url,
"rating": criteria_sum,
"top_criteria": top_criteria,
}
)
return Response(status=200, data=data_list)
......@@ -17,6 +17,7 @@ BASE_DIR = Path(__file__).resolve().parent.parent
# Where user uploaded files like images are stored
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = "/media/"
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
......
......@@ -13,6 +13,8 @@ Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include
from rest_framework import routers
......@@ -38,4 +40,5 @@ urlpatterns = [
path("api/admin/greeting/", views.GreetingView.as_view(), name="greeting"),
path("api/admin/end/", views.EndView.as_view(), name="end"),
path("api/admin/", include(router.urls)),
]
path("api/user/quiz/", views.QuizView.as_view(), name="quiz"),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment