|
1 | 1 | import datetime |
2 | 2 | import pytz |
3 | 3 | import json |
| 4 | +import django_filters |
4 | 5 |
|
5 | | -from rest_framework.exceptions import ValidationError |
6 | 6 |
|
7 | 7 | from django.conf import settings |
8 | | -from django.utils import timezone |
9 | | -from dateutil.relativedelta import relativedelta |
10 | 8 | from django.db.models import ExpressionWrapper, F, FloatField, Max, Min, Sum, Avg, Q |
11 | 9 | from django.db.models.functions import Cast, TruncDate |
| 10 | +from dateutil.relativedelta import relativedelta |
| 11 | +from django.utils import timezone |
12 | 12 | from rest_framework import mixins, pagination, viewsets |
| 13 | +from rest_framework.authentication import SessionAuthentication, TokenAuthentication |
| 14 | +from rest_framework.exceptions import ValidationError |
| 15 | +from rest_framework.permissions import IsAuthenticatedOrReadOnly |
13 | 16 |
|
14 | | -from .serializers import SensorDataSerializer |
15 | | -from feinstaub.sensors.models import SensorData |
| 17 | +from feinstaub.sensors.models import Node, SensorData |
| 18 | +from feinstaub.sensors.serializers import NowSerializer |
| 19 | +from feinstaub.sensors.views import SensorDataView, StandardResultsSetPagination |
| 20 | +from feinstaub.sensors.authentication import NodeUidAuthentication |
16 | 21 |
|
| 22 | +from .filters import NodeFilter, SensorFilter |
| 23 | +from .serializers import LastNotifySensorDataSerializer, NodeSerializer, SensorDataSerializer |
17 | 24 |
|
18 | | -class SensorDataView(mixins.ListModelMixin, viewsets.GenericViewSet): |
| 25 | +class FilterView(mixins.ListModelMixin, viewsets.GenericViewSet): |
19 | 26 | serializer_class = SensorDataSerializer |
20 | 27 |
|
21 | 28 | def get_queryset(self): |
| 29 | + sensor_type = self.request.GET.get("type", r"\w+") |
| 30 | + country = self.request.GET.get("country", r"\w+") |
| 31 | + city = self.request.GET.get("city", r"\w+") |
22 | 32 | return ( |
23 | | - SensorData.objects |
24 | | - .filter( |
| 33 | + SensorData.objects.filter( |
25 | 34 | timestamp__gte=timezone.now() - datetime.timedelta(minutes=5), |
26 | | - sensor=self.kwargs["sensor_id"] |
| 35 | + sensor__sensor_type__uid__iregex=sensor_type, |
| 36 | + location__country__iregex=country, |
| 37 | + location__city__iregex=city, |
27 | 38 | ) |
28 | | - .only('sensor', 'timestamp') |
29 | | - .prefetch_related('sensordatavalues') |
| 39 | + .only("sensor", "timestamp") |
| 40 | + .prefetch_related("sensordatavalues") |
30 | 41 | ) |
31 | 42 |
|
32 | 43 |
|
33 | | -class FilterView(mixins.ListModelMixin, viewsets.GenericViewSet): |
| 44 | +class NodeView( |
| 45 | + mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet |
| 46 | +): |
| 47 | + """Show all nodes belonging to authenticated user""" |
| 48 | + |
| 49 | + authentication_classes = [SessionAuthentication, TokenAuthentication] |
| 50 | + pagination_class = StandardResultsSetPagination |
| 51 | + permission_classes = [IsAuthenticatedOrReadOnly] |
| 52 | + queryset = SensorData.objects.none() |
| 53 | + serializer_class = NodeSerializer |
| 54 | + filter_class = NodeFilter |
| 55 | + |
| 56 | + def get_queryset(self): |
| 57 | + if self.request.user.is_authenticated(): |
| 58 | + if self.request.user.groups.filter(name="show_me_everything").exists(): |
| 59 | + return Node.objects.all() |
| 60 | + |
| 61 | + return Node.objects.filter( |
| 62 | + Q(owner=self.request.user) |
| 63 | + | Q( |
| 64 | + owner__groups__name__in=[ |
| 65 | + g.name for g in self.request.user.groups.all() |
| 66 | + ] |
| 67 | + ) |
| 68 | + ) |
| 69 | + |
| 70 | + return Node.objects.none() |
| 71 | + |
| 72 | + |
| 73 | +class NowView(mixins.ListModelMixin, viewsets.GenericViewSet): |
| 74 | + """Show all public sensors active in the last 5 minutes with newest value""" |
| 75 | + |
| 76 | + permission_classes = [] |
| 77 | + serializer_class = NowSerializer |
| 78 | + queryset = SensorData.objects.none() |
| 79 | + |
| 80 | + def get_queryset(self): |
| 81 | + now = timezone.now() |
| 82 | + startdate = now - datetime.timedelta(minutes=5) |
| 83 | + return SensorData.objects.filter( |
| 84 | + sensor__public=True, modified__range=[startdate, now] |
| 85 | + ) |
| 86 | + |
| 87 | +class PostSensorDataView(mixins.CreateModelMixin, |
| 88 | + viewsets.GenericViewSet): |
| 89 | + """ This endpoint is to POST data from the sensor to the api. |
| 90 | + """ |
| 91 | + authentication_classes = (NodeUidAuthentication,) |
| 92 | + permission_classes = tuple() |
| 93 | + serializer_class = LastNotifySensorDataSerializer |
| 94 | + queryset = SensorData.objects.all() |
| 95 | + |
| 96 | + |
| 97 | +class VerboseSensorDataView(SensorDataView): |
| 98 | + filter_class = SensorFilter |
| 99 | + |
| 100 | +class SensorsAfricaSensorDataView(mixins.ListModelMixin, viewsets.GenericViewSet): |
34 | 101 | serializer_class = SensorDataSerializer |
35 | 102 |
|
36 | 103 | def get_queryset(self): |
37 | | - sensor_type = self.request.GET.get('type', r'\w+') |
38 | | - country = self.request.GET.get('country', r'\w+') |
39 | | - city = self.request.GET.get('city', r'\w+') |
40 | 104 | return ( |
41 | | - SensorData.objects |
42 | | - .filter( |
| 105 | + SensorData.objects.filter( |
43 | 106 | timestamp__gte=timezone.now() - datetime.timedelta(minutes=5), |
44 | | - sensor__sensor_type__uid__iregex=sensor_type, |
45 | | - location__country__iregex=country, |
46 | | - location__city__iregex=city |
| 107 | + sensor=self.kwargs["sensor_id"], |
47 | 108 | ) |
48 | | - .only('sensor', 'timestamp') |
49 | | - .prefetch_related('sensordatavalues') |
| 109 | + .only("sensor", "timestamp") |
| 110 | + .prefetch_related("sensordatavalues") |
50 | 111 | ) |
| 112 | + |
0 commit comments