From faa999150449ff1bd80bff78079d4929fce27d91 Mon Sep 17 00:00:00 2001 From: kshitijrajsharma Date: Tue, 15 Aug 2023 17:40:24 +0545 Subject: [PATCH] Fixes #153 , Adds validation for feedbackparam --- backend/core/serializers.py | 51 ++++++++++++++++++++++++++++++++----- backend/core/views.py | 10 +++++--- 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/backend/core/serializers.py b/backend/core/serializers.py index 9d655aca..a1e6fa43 100644 --- a/backend/core/serializers.py +++ b/backend/core/serializers.py @@ -1,3 +1,4 @@ +from django.conf import settings from login.models import OsmUser from rest_framework import serializers from rest_framework_gis.serializers import ( @@ -164,18 +165,54 @@ class FeedbackParamSerializer(serializers.Serializer): training_id = serializers.IntegerField(required=True) epochs = serializers.IntegerField(required=False) batch_size = serializers.IntegerField(required=False) - zoom_level = serializers.ListField(required=False) + zoom_level = serializers.ListField(child=serializers.IntegerField(), required=False) + + def validate_training_id(self, value): + try: + Training.objects.get(id=value) + except Training.DoesNotExist: + raise serializers.ValidationError("Training doesn't exist") + + return value def validate(self, data): - """ - Check supplied data - """ + training_id = data.get("training_id") + + try: + fd_aois = FeedbackAOI.objects.filter(training=training_id) + except FeedbackAOI.DoesNotExist: + raise serializers.ValidationError( + "No feedback AOI is associated with Training" + ) + + if fd_aois.filter( + label_status=FeedbackAOI.DownloadStatus.NOT_DOWNLOADED + ).exists(): + raise serializers.ValidationError( + "Not all AOIs have their labels downloaded" + ) + + if "epochs" in data and ( + data["epochs"] > settings.EPOCHS_LIMIT or data["epochs"] <= 0 + ): + raise serializers.ValidationError( + f"Epochs should be 1 - {settings.EPOCHS_LIMIT} on this server" + ) + + if "batch_size" in data and ( + data["batch_size"] > settings.BATCH_SIZE_LIMIT or data["batch_size"] <= 0 + ): + raise serializers.ValidationError( + f"Batch size should be 1 - {settings.BATCH_SIZE_LIMIT} on this server" + ) + if "zoom_level" in data: - for i in data["zoom_level"]: - if int(i) < 19 or int(i) > 21: + for zoom in data["zoom_level"]: + if zoom < 19 or zoom > 21: raise serializers.ValidationError( - "Zoom level Supported between 19-21" + "Zoom level must be between 19 and 21" ) + return data diff --git a/backend/core/views.py b/backend/core/views.py index c9c4dd16..8b4a0470 100644 --- a/backend/core/views.py +++ b/backend/core/views.py @@ -425,7 +425,8 @@ class FeedbackView(APIView): ) def post(self, request, *args, **kwargs): res_serializer = FeedbackParamSerializer(data=request.data) - if res_serializer.is_valid(raise_exception=True): + + if res_serializer.is_valid(): deserialized_data = res_serializer.data training_id = deserialized_data["training_id"] training_instance = Training.objects.get(id=training_id) @@ -461,6 +462,8 @@ def post(self, request, *args, **kwargs): print(f"Saved Feedback train model request to queue with id {task.id}") return HttpResponse(status=200) + return Response(res_serializer.errors, status=status.HTTP_400_BAD_REQUEST) + DEFAULT_TILE_SIZE = 256 @@ -624,7 +627,7 @@ def get(self, request, aoi_id: int): # Convert the polygon field to GPX format geom_json = json.loads(aoi.geom.json) # Create a new GPX object - gpx_xml=gpx_generator(geom_json) + gpx_xml = gpx_generator(geom_json) return HttpResponse(gpx_xml, content_type="application/xml") @@ -634,11 +637,10 @@ def get(self, request, feedback_aoi_id: int): # Convert the polygon field to GPX format geom_json = json.loads(aoi.geom.json) # Create a new GPX object - gpx_xml=gpx_generator(geom_json) + gpx_xml = gpx_generator(geom_json) return HttpResponse(gpx_xml, content_type="application/xml") - class TrainingWorkspaceView(APIView): def get(self, request, lookup_dir=None): """List out status of training workspace : size in bytes"""