From 6d287841af91a4e89c1ee3444a2b9269e1854e43 Mon Sep 17 00:00:00 2001 From: Xavier Frankline Date: Fri, 4 Oct 2024 12:14:47 +0300 Subject: [PATCH] chore: add Now api endpoint, cleanup router --- sensorsafrica/api/v2/router.py | 40 ++++++++++------------------------ sensorsafrica/api/v2/views.py | 17 ++++++++++++++- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/sensorsafrica/api/v2/router.py b/sensorsafrica/api/v2/router.py index 46f4bc4..3ed91d4 100644 --- a/sensorsafrica/api/v2/router.py +++ b/sensorsafrica/api/v2/router.py @@ -4,6 +4,7 @@ from .views import ( CitiesView, NodesView, + NowView, SensorDataStatsView, SensorDataView, SensorLocationsView, @@ -12,34 +13,17 @@ meta_data, ) -stat_data_router = routers.DefaultRouter() -stat_data_router.register(r"", SensorDataStatsView) - -data_router = routers.DefaultRouter() -data_router.register(r"", SensorDataView) - -cities_router = routers.DefaultRouter() -cities_router.register(r"", CitiesView, basename="cities") - -nodes_router = routers.DefaultRouter() -nodes_router.register(r"", NodesView, basename="map") - -sensors_router = routers.DefaultRouter() -sensors_router.register(r"", SensorsView, basename="sensors") - -sensor_locations_router = routers.DefaultRouter() -sensor_locations_router.register(r"", SensorLocationsView, basename="locations") - -sensor_types_router = routers.DefaultRouter() -sensor_types_router.register(r"", SensorTypesView, basename="sensor_types") +router = routers.DefaultRouter() +router.register(r"data", SensorDataView, basename="sensor-data") +router.register(r"data/(?P[air]+)", SensorDataStatsView, basename="sensor-data-stats") +router.register(r"cities", CitiesView, basename="cities") +router.register(r"nodes", NodesView, basename="nodes") +router.register(r"now", NowView, basename="now") +router.register(r"locations", SensorLocationsView, basename="sensor-locations") +router.register(r"sensors", SensorsView, basename="sensors") +router.register(r"sensor-types", SensorTypesView, basename="sensor-types") api_urls = [ - url(r"data/(?P[air]+)/", include(stat_data_router.urls)), - url(r"data/", include(data_router.urls)), - url(r"cities/", include(cities_router.urls)), - url(r"nodes/", include(nodes_router.urls)), - url(r"locations/", include(sensor_locations_router.urls)), - url(r"sensors/", include(sensors_router.urls)), - url(r"sensor-types/", include(sensor_types_router.urls)), - url(r"meta/", meta_data), + url(r"^", include(router.urls)), + url(r"^meta/", meta_data), ] diff --git a/sensorsafrica/api/v2/views.py b/sensorsafrica/api/v2/views.py index f669a52..af0a1b0 100644 --- a/sensorsafrica/api/v2/views.py +++ b/sensorsafrica/api/v2/views.py @@ -23,7 +23,7 @@ from rest_framework.decorators import api_view, authentication_classes from feinstaub.sensors.views import SensorFilter, StandardResultsSetPagination - +from feinstaub.sensors.serializers import NowSerializer from feinstaub.sensors.models import ( Node, Sensor, @@ -471,3 +471,18 @@ def get_database_last_updated(): sensor_data_value = SensorDataValue.objects.latest('created') if sensor_data_value: return sensor_data_value.modified + + +class NowView(mixins.ListModelMixin, viewsets.GenericViewSet): + """Show all public sensors active in the last 5 minutes with newest value""" + + authentication_classes = [SessionAuthentication, TokenAuthentication] + permission_classes = [IsAuthenticated] + serializer_class = NowSerializer + + def get_queryset(self): + now = timezone.now() + startdate = now - datetime.timedelta(minutes=5) + return SensorData.objects.filter( + sensor__public=True, modified__range=[startdate, now] + )