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

Implement first APIView and Serializer for Sportlist

parent 80033041
No related branches found
No related tags found
No related merge requests found
......@@ -34,3 +34,21 @@ class CriterionListSerializer(serializers.ModelSerializer):
class Meta:
model = Criterion
fields = ("id", "name")
class SmallSportListSerializer(serializers.BaseSerializer):
def to_representation(self, sport_instances):
serialized_data = []
for sport, boolean in sport_instances:
serialized_data.append(
{
"pk": sport.pk,
"name": sport.name,
"url": sport.url,
"is_filled": boolean,
}
)
return serialized_data
......@@ -4,7 +4,10 @@ Defines the views for the API
# from django.shortcuts import render
from rest_framework import viewsets
from rest_framework.views import APIView
from rest_framework.response import Response
from .serializers import (
SmallSportListSerializer,
SportListSerializer,
CriterionListSerializer,
QuestionListSerializer,
......@@ -39,3 +42,64 @@ class QuestionListView(viewsets.ModelViewSet): # pylint: disable=too-many-ances
serializer_class = QuestionListSerializer
queryset = Question.objects.all()
# Dev Notes:
# - If we want to include a View in the Router in urls.py, the View needs to be a Viewset
# - Those are mostly meant for Lists of Objects, so instead of get() and post(), list() and create() are used respectively
# https://stackoverflow.com/questions/30389248/how-can-i-register-a-single-view-not-a-viewset-on-my-router
class SmallSportListView(viewsets.ViewSet):
"""
View for the List of Sports on the Sport Homepage
TODO: More Documentation
"""
def list(self, request):
return Response({"test": 0})
# Dev Notes:
# - This is a singular APIView which isn't meant to expose complete lists
# - It cannot be written into the Router, as it isn't a Viewset, so it is written directly into the urls.py's urlspatterns like this:
# path(r"api/admin/single-small-sport-list", views.SmallSportListAPIView.as_view())
# - The API isn't in the list in /api/admin because of that, but needs to be called manually here:
# http://localhost:8000/api/admin/single-small-sport-list
class SmallSportListAPIView(APIView):
"""
View for the List of Sports on the Sport Homepage
TODO: More Documentation
"""
def get(self, request):
sports = Sport.objects.all()
criteria = Criterion.objects.all()
is_filled_tuples = []
for sport in sports.iterator():
filled_criteria_pks = []
# Get pks of Criteria which are connected to the sport
for criterion in sport.criteria_ratings.iterator():
filled_criteria_pks.append(criterion.pk)
is_filled = True
# Check every existing criterion whether it is in the generated list
for criterion in criteria.all():
if criterion.pk not in filled_criteria_pks:
# A criterion isn't filled out, so stop and set to false
is_filled = False
break
is_filled_tuples.append((sport, is_filled))
serializer = SmallSportListSerializer(is_filled_tuples)
return Response(serializer.data)
......@@ -22,8 +22,10 @@ router = routers.DefaultRouter()
router.register(r"sport-list", views.SportListView, "sport-list")
router.register(r"criterion-list", views.CriterionListView, "criterion-list")
router.register(r"question-list", views.QuestionListView, "question-list")
router.register(r"small-sport-list", views.SmallSportListView, "small-sport-list")
urlpatterns = [
path("admin/", admin.site.urls),
path("api/admin/", include(router.urls)),
path(r"api/admin/single-small-sport-list", views.SmallSportListAPIView.as_view()),
]
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