From b6d0fb85c98b568699b1a8aa02667dd57f6e9cb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniele=20Vigan=C3=B2?= Date: Sun, 21 Mar 2021 16:16:50 +0100 Subject: [PATCH] Validate date in telemetry get --- bite/telemetry/views.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/bite/telemetry/views.py b/bite/telemetry/views.py index 916f7be..7f5f675 100644 --- a/bite/telemetry/views.py +++ b/bite/telemetry/views.py @@ -17,8 +17,9 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from datetime import datetime from django.http import Http404 +from django.utils import timezone +from django.utils.dateparse import parse_datetime from rest_framework.views import APIView from rest_framework.viewsets import ModelViewSet @@ -26,6 +27,7 @@ from telemetry.models import Telemetry from telemetry.serializers import (TelemetrySerializer, TelemetrySummarySerializer) from rest_framework.response import Response +from rest_framework.exceptions import ParseError class TelemetryView(ModelViewSet): @@ -66,8 +68,21 @@ class TelemetryRange(ModelViewSet): serializer_class = TelemetrySerializer lookup_field = 'device' + @staticmethod + def datetime_validation(datetime): + parsed_datetime = parse_datetime(datetime) + if parsed_datetime is None: + raise ParseError({ + datetime: 'Invalid date format' + }) + return timezone.make_aware(parsed_datetime) + def list(self, request, device, time_from, time_to=None): - time_to = datetime.now() if time_to is None else time_to + time_from = self.datetime_validation(time_from) + time_to = ( + timezone.now() if time_to is None else + self.datetime_validation(time_to) + ) queryset = Telemetry.objects.filter( device__serial=device, time__range=[time_from, time_to])