diff --git a/shedpi_hub_dashboard/serlializers.py b/shedpi_hub_dashboard/serlializers.py index 24b4302..063b6e7 100644 --- a/shedpi_hub_dashboard/serlializers.py +++ b/shedpi_hub_dashboard/serlializers.py @@ -1,3 +1,5 @@ +from typing import ClassVar + from rest_framework import serializers from .models import DeviceModule, DeviceModuleReading @@ -13,4 +15,10 @@ class DeviceModuleReadingSerializer(serializers.ModelSerializer): class Meta: model = DeviceModuleReading fields = "__all__" - # extra_kwargs = {"device_module": {"required": True}} + extra_kwargs: ClassVar[dict] = {"device_module": {"required": True}} + + +class DeviceModuleReadingListSerializer(serializers.ModelSerializer): + class Meta: + model = DeviceModuleReading + exclude: ClassVar[list] = ["device_module"] diff --git a/shedpi_hub_dashboard/tests/test_endpoints.py b/shedpi_hub_dashboard/tests/test_endpoints.py index 0e26bee..315eacf 100644 --- a/shedpi_hub_dashboard/tests/test_endpoints.py +++ b/shedpi_hub_dashboard/tests/test_endpoints.py @@ -1,8 +1,8 @@ import json import pytest -from django.urls import reverse from rest_framework import status +from rest_framework.reverse import reverse from shedpi_hub_dashboard.tests.utils.factories import ( DeviceModuleFactory, @@ -68,11 +68,11 @@ def test_device_module_readings_list_pagination(client): 110, device_module=device_module, data={"temperature": "20"} ) - url = reverse("devicemodulereading-list") + url = reverse("devicemodulereading-paginated-list") response = client.get(url, data={"device_module": device_module.id}) assert response.status_code == status.HTTP_200_OK - assert len(response.data) == 100 + assert len(response.data["results"]) == 100 @pytest.mark.django_db diff --git a/shedpi_hub_dashboard/views.py b/shedpi_hub_dashboard/views.py index 6396412..f7d2825 100644 --- a/shedpi_hub_dashboard/views.py +++ b/shedpi_hub_dashboard/views.py @@ -1,8 +1,14 @@ from django.template.response import TemplateResponse -from rest_framework import viewsets +from rest_framework import mixins, viewsets +from rest_framework.viewsets import GenericViewSet from .models import DeviceModule, DeviceModuleReading -from .serlializers import DeviceModuleReadingSerializer, DeviceModuleSerializer +from .pagination import CreatedAtBasedCursorPagination +from .serlializers import ( + DeviceModuleReadingListSerializer, + DeviceModuleReadingSerializer, + DeviceModuleSerializer, +) def index(request): @@ -19,6 +25,11 @@ class DeviceModuleReadingViewSet(viewsets.ModelViewSet): queryset = DeviceModuleReading.objects.all() serializer_class = DeviceModuleReadingSerializer + def get_serializer_class(self): + if hasattr(self, "action") and self.action == "list": + return DeviceModuleReadingListSerializer + return DeviceModuleReadingSerializer + def get_queryset(self): # FIXME: Validate that the user supplied this get param! device_module_id = self.request.query_params.get("device_module") @@ -28,20 +39,22 @@ def get_queryset(self): return self.queryset - # def list(self, request): - # queryset = self.get_queryset() - # - # context = {"request": request} - # device_module_id = self.request.query_params.get("device_module") - # - # if device_module_id: - # queryset = queryset.filter(device_module=device_module_id) - # - # context["device_module"] = device_module_id - # - # context["queryset"] = queryset - # - # serializer = self.get_serializer(data=request.data, context=context) - # serializer.is_valid(raise_exception=True) - # - # return Response(serializer.data) + +class ExperimentalViewSet( + mixins.ListModelMixin, + mixins.RetrieveModelMixin, + mixins.UpdateModelMixin, + GenericViewSet, +): + queryset = DeviceModuleReading.objects.all() + serializer_class = DeviceModuleReadingListSerializer + pagination_class = CreatedAtBasedCursorPagination + + def get_queryset(self): + # FIXME: Validate that the user supplied this get param! + device_module_id = self.request.query_params.get("device_module") + + if device_module_id: + return self.queryset.filter(device_module=device_module_id) + + return self.queryset diff --git a/shedpi_hub_example_project/settings.py b/shedpi_hub_example_project/settings.py index a0dc765..18944d2 100644 --- a/shedpi_hub_example_project/settings.py +++ b/shedpi_hub_example_project/settings.py @@ -134,4 +134,5 @@ # "DEFAULT_PERMISSION_CLASSES": [ # "rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly" # ] + "PAGE_SIZE": 100, } diff --git a/shedpi_hub_example_project/urls.py b/shedpi_hub_example_project/urls.py index 679974d..4b87646 100644 --- a/shedpi_hub_example_project/urls.py +++ b/shedpi_hub_example_project/urls.py @@ -4,11 +4,20 @@ from django.urls import include, path from rest_framework import routers -from shedpi_hub_dashboard.views import DeviceModuleReadingViewSet, DeviceModuleViewSet +from shedpi_hub_dashboard.views import ( + DeviceModuleReadingViewSet, + DeviceModuleViewSet, + ExperimentalViewSet, +) router = routers.DefaultRouter() router.register(r"device-module", DeviceModuleViewSet) router.register(r"device-module-readings", DeviceModuleReadingViewSet) +router.register( + r"device-module-readings-paginated", + ExperimentalViewSet, + basename="devicemodulereading-paginated", +) urlpatterns = [ *[