Skip to content

Commit

Permalink
Fix typing errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Aiky30 committed Feb 29, 2024
1 parent 080dbec commit faed355
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 24 deletions.
10 changes: 9 additions & 1 deletion shedpi_hub_dashboard/serlializers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import ClassVar

from rest_framework import serializers

from .models import DeviceModule, DeviceModuleReading
Expand All @@ -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"]
6 changes: 3 additions & 3 deletions shedpi_hub_dashboard/tests/test_endpoints.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
Expand Down
51 changes: 32 additions & 19 deletions shedpi_hub_dashboard/views.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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")
Expand All @@ -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
1 change: 1 addition & 0 deletions shedpi_hub_example_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,5 @@
# "DEFAULT_PERMISSION_CLASSES": [
# "rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly"
# ]
"PAGE_SIZE": 100,
}
11 changes: 10 additions & 1 deletion shedpi_hub_example_project/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
*[
Expand Down

0 comments on commit faed355

Please sign in to comment.