Skip to content

Commit

Permalink
Merge pull request #287 from hotosm/feature/model-description
Browse files Browse the repository at this point in the history
Feature : Model description
  • Loading branch information
kshitijrajsharma authored Oct 7, 2024
2 parents 0d413b3 + 9e51af5 commit 527827e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions backend/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) #
Expand Down
28 changes: 24 additions & 4 deletions backend/core/serializers.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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):
"""
Expand All @@ -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
Expand Down
8 changes: 5 additions & 3 deletions backend/core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -267,6 +268,7 @@ class ModelCentroidView(ListAPIView):
)
filterset_fields = ["id"]
search_fields = ["name"]
pagination_class = None


class UsersView(ListAPIView):
Expand Down

0 comments on commit 527827e

Please sign in to comment.