Skip to content

Commit

Permalink
Fix API and Update maptunik to be able to receive incoming independen…
Browse files Browse the repository at this point in the history
…t style (#15)

* Add upload API

* Fix pagination

* Fix order

* Update maptunik to be able to receive incoming style
  • Loading branch information
meomancer authored Oct 11, 2024
1 parent a686d30 commit 60fecc9
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 113 deletions.
3 changes: 3 additions & 0 deletions django_project/cloud_native_gis/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from rest_framework.response import Response
from rest_framework.viewsets import mixins, GenericViewSet

from cloud_native_gis.pagination import Pagination


class BaseReadApi(
mixins.ListModelMixin,
Expand All @@ -21,6 +23,7 @@ class BaseReadApi(

form_class = None
lookup_field = 'id'
pagination_class = Pagination

def filter_query(self, request, query, ignores: list, fields: list = None):
"""Return filter query."""
Expand Down
51 changes: 44 additions & 7 deletions django_project/cloud_native_gis/api/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import copy

from django.core.exceptions import PermissionDenied
from django.core.files.storage import FileSystemStorage
from django.http import Http404
from rest_framework.generics import get_object_or_404
from rest_framework.response import Response
Expand All @@ -11,8 +13,10 @@
from cloud_native_gis.forms.layer import LayerForm
from cloud_native_gis.forms.style import StyleForm
from cloud_native_gis.models.layer import Layer
from cloud_native_gis.models.layer_upload import LayerUpload
from cloud_native_gis.models.style import Style
from cloud_native_gis.serializer.layer import LayerSerializer
from cloud_native_gis.serializer.layer_upload import LayerUploadSerializer
from cloud_native_gis.serializer.style import LayerStyleSerializer
from cloud_native_gis.utils.layer import layer_style_url, maputnik_url

Expand All @@ -34,11 +38,8 @@ def get_serializer_context(self):
}


class LayerStyleViewSet(BaseReadApi):
"""API layer style."""

form_class = StyleForm
serializer_class = LayerStyleSerializer
class LayerObjectViewSet(BaseReadApi):
"""Abstract base class for layer objects."""

def _get_layer(self) -> Layer: # noqa: D102
layer_id = self.kwargs.get('layer_id')
Expand All @@ -62,11 +63,17 @@ def get_serializer(self, *args, **kwargs):
kwargs.setdefault('context', self.get_serializer_context())
return serializer_class(*args, **kwargs)


class LayerStyleViewSet(LayerObjectViewSet):
"""API layer style."""

form_class = StyleForm
serializer_class = LayerStyleSerializer

def get_queryset(self):
"""Return queryset of API."""
layer = self._get_layer()
ids = layer.styles.values_list('id', flat=True)
return Style.objects.filter(id__in=ids)
return layer.styles.all()

def list(self, request, *args, **kwargs):
"""Return just default style."""
Expand Down Expand Up @@ -132,3 +139,33 @@ def update(self, request, *args, **kwargs):
f'{maputnik_url()}?styleUrl='
f'{layer_style_url(layer, style, self.request)}'
)


class LayerUploadViewSet(LayerObjectViewSet):
"""API layer upload style."""

serializer_class = LayerUploadSerializer

def get_queryset(self):
"""Return queryset of API."""
layer = self._get_layer()
return layer.layerupload_set.all().order_by('-pk')

def post(self, request, layer_id):
"""Post file."""
layer = get_object_or_404(Layer, id=layer_id)
if layer.created_by != self.request.user:
raise PermissionDenied

instance = LayerUpload(
created_by=request.user, layer=layer
)
instance.emptying_folder()

# Save files
file = request.FILES['file']
FileSystemStorage(
location=instance.folder
).save(file.name, file)
instance.save()
return Response('Uploaded')
27 changes: 27 additions & 0 deletions django_project/cloud_native_gis/pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# coding=utf-8
"""Cloud Native GIS."""

from rest_framework.pagination import PageNumberPagination
from rest_framework.response import Response


class Pagination(PageNumberPagination):
"""Pagination for API."""

page_size_query_param = 'page_size'

def get_paginated_response_data(self, data):
"""Return paginated only data."""
return {
'next': self.get_next_link(),
'previous': self.get_previous_link(),
'count': self.page.paginator.count,
'page': self.page.number,
'total_page': self.page.paginator.num_pages,
'page_size': self.page.paginator.per_page,
'results': data,
}

def get_paginated_response(self, data):
"""Response for pagination."""
return Response(self.get_paginated_response_data(data))
27 changes: 27 additions & 0 deletions django_project/cloud_native_gis/serializer/layer_upload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# coding=utf-8
"""
GeoSight is UNICEF's geospatial web-based business intelligence platform.
Contact : geosight-no-reply@unicef.org
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
"""
__author__ = 'irwan@kartoza.com'
__date__ = '23/07/2024'
__copyright__ = ('Copyright 2023, Unicef')

from rest_framework import serializers

from cloud_native_gis.models.layer_upload import LayerUpload


class LayerUploadSerializer(serializers.ModelSerializer):
"""Serializer for LayerUpload."""

class Meta: # noqa: D106
model = LayerUpload
exclude = ()

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@
}

</style>
<script type="module" crossorigin src="{% static "cloud_native_gis/index-DRZ64yjZ.js" %}"></script>
<script>
let inputStyle = window.inputStyle;
if (inputStyle){
inputStyle = JSON.parse(inputStyle)
}
</script>
<script type="module" crossorigin src="{% static "cloud_native_gis/index-CHkHdgsP.js" %}"></script>
<link rel="stylesheet" crossorigin href="{% static "cloud_native_gis/index-DW0d2Ij5.css" %}">

<script>
Expand Down
8 changes: 6 additions & 2 deletions django_project/cloud_native_gis/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
from drf_yasg import openapi

from cloud_native_gis.api.layer import (
LayerViewSet, LayerStyleViewSet
LayerViewSet, LayerStyleViewSet, LayerUploadViewSet
)
from cloud_native_gis.api.vector_tile import (VectorTileLayer)
from cloud_native_gis.api.vector_tile import VectorTileLayer

schema_view = get_schema_view(
openapi.Info(
Expand All @@ -40,6 +40,10 @@
'style', LayerStyleViewSet,
basename='cloud-native-gis-style'
)
layer_router.register(
'layer-upload', LayerUploadViewSet,
basename='cloud-native-gis-layer-upload'
)

urlpatterns = [
path(
Expand Down
2 changes: 1 addition & 1 deletion maputnik

0 comments on commit 60fecc9

Please sign in to comment.