From 21bd963b6e14e8dc1d7d5080d0f488eb2e8b2f1a Mon Sep 17 00:00:00 2001
From: borzechof99 <borzechof99@mi.fu-berlin.de>
Date: Sun, 20 Jun 2021 21:02:09 +0200
Subject: [PATCH] Implement first APIView and Serializer for Sportlist

---
 unisportomat/quiz/serializers.py  | 18 +++++++++
 unisportomat/quiz/views.py        | 64 +++++++++++++++++++++++++++++++
 unisportomat/unisportomat/urls.py |  2 +
 3 files changed, 84 insertions(+)

diff --git a/unisportomat/quiz/serializers.py b/unisportomat/quiz/serializers.py
index 46c5f5a..f17f486 100644
--- a/unisportomat/quiz/serializers.py
+++ b/unisportomat/quiz/serializers.py
@@ -34,3 +34,21 @@ class CriterionListSerializer(serializers.ModelSerializer):
     class Meta:
         model = Criterion
         fields = ("pk", "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
diff --git a/unisportomat/quiz/views.py b/unisportomat/quiz/views.py
index 03cb6f9..5c528aa 100644
--- a/unisportomat/quiz/views.py
+++ b/unisportomat/quiz/views.py
@@ -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)
diff --git a/unisportomat/unisportomat/urls.py b/unisportomat/unisportomat/urls.py
index 801097c..480adf1 100644
--- a/unisportomat/unisportomat/urls.py
+++ b/unisportomat/unisportomat/urls.py
@@ -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()),
 ]
-- 
GitLab