Skip to content

Commit

Permalink
chore: add Now api endpoint, cleanup router
Browse files Browse the repository at this point in the history
  • Loading branch information
thepsalmist committed Oct 4, 2024
1 parent 95c3d69 commit 6d28784
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
40 changes: 12 additions & 28 deletions sensorsafrica/api/v2/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .views import (
CitiesView,
NodesView,
NowView,
SensorDataStatsView,
SensorDataView,
SensorLocationsView,
Expand All @@ -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<sensor_type>[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<sensor_type>[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),
]
17 changes: 16 additions & 1 deletion sensorsafrica/api/v2/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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]
)

0 comments on commit 6d28784

Please sign in to comment.