Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix : Adds validation for Feedback Param #155

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 44 additions & 7 deletions backend/core/serializers.py
Original file line number Diff line number Diff line change
@@ -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 (
Expand Down Expand Up @@ -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


Expand Down
10 changes: 6 additions & 4 deletions backend/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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")


Expand All @@ -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"""
Expand Down