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

Sportlist and Single Sports in ViewSet

parent 42aae181
No related branches found
No related tags found
No related merge requests found
...@@ -6,6 +6,7 @@ Defines the views for the API ...@@ -6,6 +6,7 @@ Defines the views for the API
from rest_framework import viewsets from rest_framework import viewsets
from rest_framework.views import APIView from rest_framework.views import APIView
from rest_framework.response import Response from rest_framework.response import Response
from django.http import JsonResponse
from .serializers import ( from .serializers import (
SmallSportListSerializer, SmallSportListSerializer,
SportListSerializer, SportListSerializer,
...@@ -57,49 +58,10 @@ class SmallSportListView(viewsets.ViewSet): ...@@ -57,49 +58,10 @@ class SmallSportListView(viewsets.ViewSet):
TODO: More Documentation TODO: More Documentation
""" """
# GET for api/admin/sport/
def list(self, request):
return Response({"test": "Showing all Entries"})
# POST for api/admin/sport/
def create(self, request):
return Response({"test": "Creating a new Entry"})
# GET for api/admin/sport/<id>/
def retrieve(self, request, pk=None):
return Response({"test": "Getting a Single Entry"})
# PUT for api/admin/sport/<id>/
def update(self, request, pk=None):
return Response({"test": "Updating a Single Entry"})
# DELETE for api/admin/sport/<id>/
def destroy(self, request, pk=None):
return Response({"test": "Removing a Single Entry"})
# 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
"""
authentication_classes = [] authentication_classes = []
def get(self, request): # GET for api/admin/sport/
def list(self, request):
sports = Sport.objects.all() sports = Sport.objects.all()
criteria = Criterion.objects.all() criteria = Criterion.objects.all()
...@@ -128,31 +90,8 @@ class SmallSportListAPIView(APIView): ...@@ -128,31 +90,8 @@ class SmallSportListAPIView(APIView):
return Response(serializer.data) return Response(serializer.data)
# POST for api/admin/sport/
class SingleSportAPIView(APIView): def create(self, request):
authentication_classes = []
def get(self, request, pk):
return Response(SingleSportSerializer(Sport.objects.get(pk=pk)).data)
def put(self, request, pk):
data_dict = SingleSportSerializer().to_internal_value(request)
sport = Sport.objects.get(pk=pk)
print(data_dict)
return Response([])
class CreateSportAPIView(APIView):
authentication_classes = []
def post(self, request):
data_dict = SingleSportSerializer().to_internal_value(request) data_dict = SingleSportSerializer().to_internal_value(request)
...@@ -176,3 +115,46 @@ class CreateSportAPIView(APIView): ...@@ -176,3 +115,46 @@ class CreateSportAPIView(APIView):
response = SingleSportSerializer(new_sport) response = SingleSportSerializer(new_sport)
return Response(response.data) return Response(response.data)
# GET for api/admin/sport/<id>/
def retrieve(self, request, pk=None):
try:
sport = Sport.objects.get(pk=pk)
except:
return JsonResponse(
{
"status_code": 404,
"error": f"No Sport with ID {pk} found",
},
status=404,
)
return Response(SingleSportSerializer(sport).data)
# PUT for api/admin/sport/<id>/
def update(self, request, pk=None):
"""
TODO
"""
data_dict = SingleSportSerializer().to_internal_value(request)
sport = Sport.objects.get(pk=pk)
print(data_dict)
return Response({"test": "Updating a Single Entry"})
# DELETE for api/admin/sport/<id>/
def destroy(self, request, pk=None):
return Response({"test": "Removing a Single Entry"})
# 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
...@@ -27,7 +27,4 @@ router.register(r"small-sport-list", views.SmallSportListView, "small-sport-list ...@@ -27,7 +27,4 @@ router.register(r"small-sport-list", views.SmallSportListView, "small-sport-list
urlpatterns = [ urlpatterns = [
path("admin/", admin.site.urls), path("admin/", admin.site.urls),
path("api/admin/", include(router.urls)), path("api/admin/", include(router.urls)),
path("api/admin/single-small-sport-list", views.SmallSportListAPIView.as_view()),
path("api/admin/single-sport/<int:pk>", views.SingleSportAPIView.as_view()),
path("api/admin/single-sport", views.CreateSportAPIView.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