-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
257 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from django.urls import path | ||
from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView | ||
from rest_framework import routers | ||
|
||
from metadata_catalogue.maps.api import views | ||
|
||
router = routers.DefaultRouter() | ||
router.register(r"maps", views.MapViewSet, basename="maps") | ||
router.register(r"raster-sources", views.RasterSourceViewSet, basename="raster-sources") | ||
router.register(r"vector-sources", views.VectorSourceViewSet, basename="vector-sources") | ||
router.register(r"layers", views.LayerViewSet, basename="layers") | ||
router.register(r"layer-groups", views.LayerGroupViewSet, basename="layer-groups") | ||
|
||
|
||
urlpatterns = [ | ||
path("schema/", SpectacularAPIView.as_view(), name="schema"), | ||
path("docs/", SpectacularSwaggerView.as_view(url_name="schema"), name="swagger-ui"), | ||
path("docs/redoc/", SpectacularRedocView.as_view(url_name="schema"), name="redoc"), | ||
] + router.urls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
from rest_framework import serializers | ||
|
||
from ..models import Layer, LayerGroup, Map, RasterSource, Source, VectorSource | ||
|
||
|
||
class FileUploadSerializer(serializers.Serializer): | ||
file = serializers.FileField(required=True) | ||
field = serializers.CharField(required=True) | ||
|
||
|
||
class MapSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Map | ||
fields = [ | ||
"slug", | ||
"id", | ||
"title", | ||
"subtitle", | ||
"zoom", | ||
"description", | ||
"visibility", | ||
"config", | ||
"legend_config", | ||
"basemap_config", | ||
] | ||
|
||
|
||
class RasterSourceBaseSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = RasterSource | ||
fields = [ | ||
"name", | ||
"slug", | ||
"extra", | ||
"style", | ||
"url", | ||
"attribution", | ||
"protocol", | ||
] | ||
|
||
|
||
class RasterSourceSerializer(serializers.ModelSerializer): | ||
class Meta(RasterSourceBaseSerializer.Meta): | ||
fields = [ | ||
"name", | ||
"slug", | ||
"extra", | ||
"style", | ||
"url", | ||
"attribution", | ||
"protocol", | ||
"source", | ||
"original_data", | ||
] | ||
|
||
|
||
class VectorSourceSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = VectorSource | ||
fields = [ | ||
"name", | ||
"slug", | ||
"extra", | ||
"style", | ||
"url", | ||
"attribution", | ||
"protocol", | ||
"source", | ||
"original_data", | ||
"default_layer", | ||
] | ||
|
||
|
||
class LayerSerializer(serializers.ModelSerializer): | ||
map = serializers.SlugRelatedField(slug_field="slug", queryset=Map.objects.all()) | ||
source = serializers.SlugRelatedField(slug_field="slug", queryset=Source.objects.all()) | ||
|
||
class Meta: | ||
model = Layer | ||
fields = [ | ||
"name", | ||
"slug", | ||
"map", | ||
"source", | ||
"style", | ||
"group", | ||
"group_order", | ||
"downloadable", | ||
"description", | ||
"link", | ||
"legend", | ||
"is_basemap", | ||
"is_lazy", | ||
"hidden", | ||
] | ||
|
||
|
||
class LayerGroupSerializer(serializers.ModelSerializer): | ||
map = serializers.SlugRelatedField(slug_field="slug", queryset=Map.objects.all(), allow_null=True) | ||
parent = serializers.SlugRelatedField(slug_field="slug", queryset=LayerGroup.objects.all(), allow_null=True) | ||
|
||
class Meta: | ||
model = LayerGroup | ||
fields = ["name", "order", "slug", "map", "description", "link", "download_url", "parent"] | ||
|
||
def create(self, validated_data): | ||
parent = validated_data.pop("parent") | ||
if parent: | ||
return parent.add_child(**validated_data) | ||
else: | ||
return LayerGroup.add_root(**validated_data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from drf_spectacular.utils import extend_schema | ||
from rest_framework import viewsets | ||
from rest_framework.decorators import action | ||
from rest_framework.parsers import MultiPartParser | ||
|
||
from ..models import Layer, LayerGroup, Map, RasterSource, VectorSource | ||
from .serializers import ( | ||
FileUploadSerializer, | ||
LayerGroupSerializer, | ||
LayerSerializer, | ||
MapSerializer, | ||
RasterSourceSerializer, | ||
VectorSourceSerializer, | ||
) | ||
|
||
|
||
class MapViewSet(viewsets.ModelViewSet): | ||
queryset = Map.objects.all() | ||
serializer_class = MapSerializer | ||
lookup_field = "slug" | ||
|
||
|
||
UPLOAD_REQUEST_SCHEMA = { | ||
"multipart/form-data": { | ||
"type": "object", | ||
"properties": {"field": {"type": "string"}, "file": {"type": "string", "format": "binary"}}, | ||
} | ||
} | ||
|
||
|
||
class UploadableMixin: | ||
@action(detail=True, methods=["post"], parser_classes=(MultiPartParser,), serializer_class=FileUploadSerializer) | ||
def upload(self, request): | ||
instance = self.get_object() | ||
serializer = self.get_serializer(data=request.data) | ||
serializer.is_valid(raise_exception=True) | ||
file = request.FILES.get("file") | ||
field = serializer.validated_data.get("field") | ||
setattr(instance, field, file) | ||
instance.save() | ||
return instance | ||
|
||
|
||
class RasterSourceViewSet(UploadableMixin, viewsets.ModelViewSet): | ||
queryset = RasterSource.objects.all() | ||
lookup_field = "slug" | ||
serializer_class = RasterSourceSerializer | ||
|
||
@extend_schema(request=UPLOAD_REQUEST_SCHEMA, responses={"200": RasterSourceSerializer}) | ||
@action(detail=True, methods=["post"], parser_classes=(MultiPartParser,), serializer_class=FileUploadSerializer) | ||
def upload(self, request): | ||
return super().upload(request) | ||
|
||
|
||
class VectorSourceViewSet(UploadableMixin, viewsets.ModelViewSet): | ||
queryset = VectorSource.objects.all() | ||
serializer_class = VectorSourceSerializer | ||
lookup_field = "slug" | ||
|
||
@extend_schema(request=UPLOAD_REQUEST_SCHEMA, responses={"200": RasterSourceSerializer}) | ||
@action(detail=True, methods=["post"], parser_classes=(MultiPartParser,), serializer_class=FileUploadSerializer) | ||
def upload(self, request): | ||
return super().upload(request) | ||
|
||
|
||
class LayerViewSet(viewsets.ModelViewSet): | ||
queryset = Layer.objects.all() | ||
serializer_class = LayerSerializer | ||
lookup_field = "slug" | ||
|
||
|
||
class LayerGroupViewSet(viewsets.ModelViewSet): | ||
queryset = LayerGroup.objects.all() | ||
serializer_class = LayerGroupSerializer | ||
lookup_field = "slug" |
File renamed without changes.
21 changes: 21 additions & 0 deletions
21
metadata_catalogue/maps/migrations/0014_layergroup_slug_layergroup_group_unique_slug.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Generated by Django 4.2.8 on 2024-04-24 11:25 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("maps", "0013_layer_hidden_layer_is_lazy"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="layergroup", | ||
name="slug", | ||
field=models.SlugField(blank=True, max_length=250, null=True), | ||
), | ||
migrations.AddConstraint( | ||
model_name="layergroup", | ||
constraint=models.UniqueConstraint(models.F("slug"), name="group unique slug"), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
from ninja import NinjaAPI | ||
|
||
from .api import maps_router | ||
from .apis import maps_router | ||
|
||
api = NinjaAPI() | ||
api.add_router("/maps/", maps_router) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters