diff --git a/backend/core/models.py b/backend/core/models.py index 7ad9afd6..60247ff9 100644 --- a/backend/core/models.py +++ b/backend/core/models.py @@ -56,6 +56,7 @@ class ModelStatus(models.IntegerChoices): name = models.CharField(max_length=255) created_at = models.DateTimeField(auto_now_add=True) last_modified = models.DateTimeField(auto_now=True) + description = models.TextField(max_length=500, null=True, blank=True) created_by = models.ForeignKey(OsmUser, to_field="osm_id", on_delete=models.CASCADE) published_training = models.PositiveIntegerField(null=True, blank=True) status = models.IntegerField(default=-1, choices=ModelStatus.choices) # diff --git a/backend/core/serializers.py b/backend/core/serializers.py index 33d03d76..3f4cc556 100644 --- a/backend/core/serializers.py +++ b/backend/core/serializers.py @@ -1,10 +1,11 @@ from django.conf import settings -from login.models import OsmUser from rest_framework import serializers from rest_framework_gis.serializers import ( GeoFeatureModelSerializer, # this will be used if we used to serialize as geojson ) +from login.models import OsmUser + from .models import * # from .tasks import train_model @@ -48,10 +49,11 @@ class ModelSerializer( serializers.ModelSerializer ): # serializers are used to translate models objects to api created_by = UserSerializer(read_only=True) + accuracy = serializers.SerializerMethodField() class Meta: model = Model - fields = "__all__" # defining all the fields to be included in curd for now , we can restrict few if we want + fields = "__all__" read_only_fields = ( "created_at", "last_modified", @@ -64,13 +66,23 @@ def create(self, validated_data): validated_data["created_by"] = user return super().create(validated_data) + def get_accuracy( + self, obj + ): ## this might have performance problem when db grows bigger , consider adding indexes / view in db + training = Training.objects.filter(id=obj.published_training).first() + if training: + return training.accuracy + return None + -class ModelCentroidSerializer(serializers.ModelSerializer): +class ModelCentroidSerializer(GeoFeatureModelSerializer): geometry = serializers.SerializerMethodField() + mid = serializers.IntegerField(source="id") class Meta: model = Model - fields = ("id", "name", "geometry") + geo_field = "geometry" + fields = ("mid", "name", "geometry") def get_geometry(self, obj): """ @@ -84,6 +96,14 @@ def get_geometry(self, obj): } return None + # def to_representation(self, instance): + # """ + # Override to_representation to customize GeoJSON structure. + # """ + # representation = super().to_representation(instance) + # representation["properties"]["id"] = representation.pop("id") + # return representation + class AOISerializer( GeoFeatureModelSerializer diff --git a/backend/core/views.py b/backend/core/views.py index 1d26b9ce..c89e5974 100644 --- a/backend/core/views.py +++ b/backend/core/views.py @@ -26,8 +26,6 @@ from django_filters.rest_framework import DjangoFilterBackend from drf_yasg.utils import swagger_auto_schema from geojson2osm import geojson2osm -from login.authentication import OsmAuthentication -from login.permissions import IsOsmAuthenticated from orthogonalizer import othogonalize_poly from osmconflator import conflate_geojson from rest_framework import decorators, filters, serializers, status, viewsets @@ -38,6 +36,9 @@ from rest_framework.views import APIView from rest_framework_gis.filters import InBBoxFilter, TMSTileFilter +from login.authentication import OsmAuthentication +from login.permissions import IsOsmAuthenticated + from .models import ( AOI, ApprovedPredictions, @@ -258,7 +259,7 @@ class ModelViewSet( class ModelCentroidView(ListAPIView): - queryset = Model.objects.all() + queryset = Model.objects.filter(status=0) ## only deliver the published model serializer_class = ModelCentroidSerializer filter_backends = ( # InBBoxFilter, @@ -267,6 +268,7 @@ class ModelCentroidView(ListAPIView): ) filterset_fields = ["id"] search_fields = ["name"] + pagination_class = None class UsersView(ListAPIView):