From 38aba5211b597a57b53a8b840ec37b42fe399641 Mon Sep 17 00:00:00 2001 From: esir Date: Mon, 1 Feb 2021 12:31:55 +0300 Subject: [PATCH 1/5] Adds a nodes filter using location --- sensorsafrica/api/v2/views.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sensorsafrica/api/v2/views.py b/sensorsafrica/api/v2/views.py index f4e7272..4592005 100644 --- a/sensorsafrica/api/v2/views.py +++ b/sensorsafrica/api/v2/views.py @@ -140,8 +140,13 @@ def get_permissions(self): def list(self, request): nodes = [] + if 'location' in request.query_params: + last_active_nodes = LastActiveNodes.objects.filter(location__country=request.query_params['location']) + else: + last_active_nodes = LastActiveNodes.objects.iterator() + # Loop through the last active nodes - for last_active in LastActiveNodes.objects.iterator(): + for last_active in last_active_nodes: # Get the current node node = Node.objects.filter( Q(id=last_active.node.id), ~Q(sensors=None) From 5247806be6518ef9a60a5646ea24379b94cfd92c Mon Sep 17 00:00:00 2001 From: esir Date: Mon, 1 Feb 2021 13:21:10 +0300 Subject: [PATCH 2/5] Adds nodes filter for v1 of the API --- sensorsafrica/api/v1/filters.py | 9 +++++++++ sensorsafrica/api/v1/views.py | 5 ++++- sensorsafrica/api/v2/views.py | 4 ++-- 3 files changed, 15 insertions(+), 3 deletions(-) create mode 100644 sensorsafrica/api/v1/filters.py diff --git a/sensorsafrica/api/v1/filters.py b/sensorsafrica/api/v1/filters.py new file mode 100644 index 0000000..3cee53b --- /dev/null +++ b/sensorsafrica/api/v1/filters.py @@ -0,0 +1,9 @@ +import django_filters + +from feinstaub.sensors.models import Node + +class NodeFilter(django_filters.FilterSet): + class Meta: + model = Node + fields = {"location__country": ["exact"]} + diff --git a/sensorsafrica/api/v1/views.py b/sensorsafrica/api/v1/views.py index 53d9921..f997b4f 100644 --- a/sensorsafrica/api/v1/views.py +++ b/sensorsafrica/api/v1/views.py @@ -1,6 +1,7 @@ import datetime import pytz import json +import django_filters from django.conf import settings @@ -18,7 +19,7 @@ from feinstaub.sensors.views import StandardResultsSetPagination from .serializers import SensorDataSerializer - +from .filters import NodeFilter class FilterView(mixins.ListModelMixin, viewsets.GenericViewSet): serializer_class = SensorDataSerializer @@ -49,6 +50,8 @@ class NodeView( permission_classes = [IsAuthenticatedOrReadOnly] queryset = SensorData.objects.none() serializer_class = NodeSerializer + filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) + filter_class = NodeFilter def get_queryset(self): if self.request.user.is_authenticated(): diff --git a/sensorsafrica/api/v2/views.py b/sensorsafrica/api/v2/views.py index 4592005..0146bca 100644 --- a/sensorsafrica/api/v2/views.py +++ b/sensorsafrica/api/v2/views.py @@ -140,8 +140,8 @@ def get_permissions(self): def list(self, request): nodes = [] - if 'location' in request.query_params: - last_active_nodes = LastActiveNodes.objects.filter(location__country=request.query_params['location']) + if 'location__country' in request.query_params: + last_active_nodes = LastActiveNodes.objects.filter(location__country=request.query_params['location__country']) else: last_active_nodes = LastActiveNodes.objects.iterator() From 0122919abd5a1b155fcb1cc696b1f837fa9c2f03 Mon Sep 17 00:00:00 2001 From: esir Date: Mon, 1 Feb 2021 14:16:48 +0300 Subject: [PATCH 3/5] Remove filterling on v2 since it's only used for the F.E map --- sensorsafrica/api/v2/views.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/sensorsafrica/api/v2/views.py b/sensorsafrica/api/v2/views.py index 0146bca..75e232a 100644 --- a/sensorsafrica/api/v2/views.py +++ b/sensorsafrica/api/v2/views.py @@ -140,13 +140,9 @@ def get_permissions(self): def list(self, request): nodes = [] - if 'location__country' in request.query_params: - last_active_nodes = LastActiveNodes.objects.filter(location__country=request.query_params['location__country']) - else: - last_active_nodes = LastActiveNodes.objects.iterator() # Loop through the last active nodes - for last_active in last_active_nodes: + for last_active in LastActiveNodes.objects.iterator(): # Get the current node node = Node.objects.filter( Q(id=last_active.node.id), ~Q(sensors=None) From a78c27a1434c8d1dde7cae9fecaf6fff6e33d93d Mon Sep 17 00:00:00 2001 From: esir Date: Mon, 1 Feb 2021 14:21:24 +0300 Subject: [PATCH 4/5] Use default django filter backend --- sensorsafrica/api/v1/views.py | 1 - 1 file changed, 1 deletion(-) diff --git a/sensorsafrica/api/v1/views.py b/sensorsafrica/api/v1/views.py index f997b4f..a51aecc 100644 --- a/sensorsafrica/api/v1/views.py +++ b/sensorsafrica/api/v1/views.py @@ -50,7 +50,6 @@ class NodeView( permission_classes = [IsAuthenticatedOrReadOnly] queryset = SensorData.objects.none() serializer_class = NodeSerializer - filter_backends = (django_filters.rest_framework.DjangoFilterBackend,) filter_class = NodeFilter def get_queryset(self): From da306d7d81f4b01b83d8fb1faf2d96dedbb6096f Mon Sep 17 00:00:00 2001 From: esir Date: Mon, 1 Feb 2021 14:25:49 +0300 Subject: [PATCH 5/5] Makes country location matching case insensitive. --- sensorsafrica/api/v1/filters.py | 2 +- sensorsafrica/api/v2/views.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/sensorsafrica/api/v1/filters.py b/sensorsafrica/api/v1/filters.py index 3cee53b..d525dd2 100644 --- a/sensorsafrica/api/v1/filters.py +++ b/sensorsafrica/api/v1/filters.py @@ -5,5 +5,5 @@ class NodeFilter(django_filters.FilterSet): class Meta: model = Node - fields = {"location__country": ["exact"]} + fields = {"location__country": ["iexact"]} diff --git a/sensorsafrica/api/v2/views.py b/sensorsafrica/api/v2/views.py index 75e232a..f4e7272 100644 --- a/sensorsafrica/api/v2/views.py +++ b/sensorsafrica/api/v2/views.py @@ -140,7 +140,6 @@ def get_permissions(self): def list(self, request): nodes = [] - # Loop through the last active nodes for last_active in LastActiveNodes.objects.iterator(): # Get the current node