From 236322b6381ed5f3744e0fb21df8db85f4e848ef Mon Sep 17 00:00:00 2001 From: esir Date: Wed, 27 Jan 2021 16:55:03 +0300 Subject: [PATCH 1/2] Includes country in the sensor data serializer --- sensorsafrica/api/v2/serializers.py | 11 +++++++++++ sensorsafrica/api/v2/views.py | 4 ++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/sensorsafrica/api/v2/serializers.py b/sensorsafrica/api/v2/serializers.py index 80e9c16..8129626 100644 --- a/sensorsafrica/api/v2/serializers.py +++ b/sensorsafrica/api/v2/serializers.py @@ -4,6 +4,8 @@ NestedSensorTypeSerializer, ) from feinstaub.sensors.models import Node, Sensor, SensorType +from feinstaub.sensors.serializers import (VerboseSensorDataSerializer, + NestedSensorLocationSerializer, ) class SensorDataStatSerializer(serializers.Serializer): @@ -76,3 +78,12 @@ class Meta: "inactive", "exact_location", ) + +class SensorLocationSerializer(NestedSensorLocationSerializer): + class Meta(NestedSensorLocationSerializer.Meta): + fields = NestedSensorLocationSerializer.Meta.fields + ( + 'country', + ) + +class SensorDataSerializer(VerboseSensorDataSerializer): + location = SensorLocationSerializer() diff --git a/sensorsafrica/api/v2/views.py b/sensorsafrica/api/v2/views.py index 030cd9f..f4e7272 100644 --- a/sensorsafrica/api/v2/views.py +++ b/sensorsafrica/api/v2/views.py @@ -33,7 +33,6 @@ SensorType, ) -from feinstaub.sensors.serializers import VerboseSensorDataSerializer from ..models import City, LastActiveNodes, SensorDataStat from .serializers import ( @@ -43,6 +42,7 @@ NodeSerializer, SensorSerializer, SensorLocationSerializer, + SensorDataSerializer, ) @@ -238,7 +238,7 @@ class SensorDataView( permission_classes = [IsAuthenticated] filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) filter_class = SensorFilter - serializer_class = VerboseSensorDataSerializer + serializer_class = SensorDataSerializer def get_queryset(self): if self.request.user.is_authenticated(): From 501fec9c33b9654f0423ddb284c0bd7c60dfa08f Mon Sep 17 00:00:00 2001 From: esir Date: Thu, 28 Jan 2021 03:46:16 +0300 Subject: [PATCH 2/2] Only return location id and country --- sensorsafrica/api/v2/serializers.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/sensorsafrica/api/v2/serializers.py b/sensorsafrica/api/v2/serializers.py index 8129626..f909cb6 100644 --- a/sensorsafrica/api/v2/serializers.py +++ b/sensorsafrica/api/v2/serializers.py @@ -3,9 +3,8 @@ NestedSensorLocationSerializer, NestedSensorTypeSerializer, ) -from feinstaub.sensors.models import Node, Sensor, SensorType -from feinstaub.sensors.serializers import (VerboseSensorDataSerializer, - NestedSensorLocationSerializer, ) +from feinstaub.sensors.models import Node, Sensor, SensorType, SensorLocation +from feinstaub.sensors.serializers import (VerboseSensorDataSerializer, ) class SensorDataStatSerializer(serializers.Serializer): @@ -79,11 +78,10 @@ class Meta: "exact_location", ) -class SensorLocationSerializer(NestedSensorLocationSerializer): - class Meta(NestedSensorLocationSerializer.Meta): - fields = NestedSensorLocationSerializer.Meta.fields + ( - 'country', - ) +class SensorDataSensorLocationSerializer(serializers.ModelSerializer): + class Meta: + model = SensorLocation + fields = ('id', "country", ) class SensorDataSerializer(VerboseSensorDataSerializer): - location = SensorLocationSerializer() + location = SensorDataSensorLocationSerializer()