Skip to content

Commit

Permalink
fix file upload
Browse files Browse the repository at this point in the history
  • Loading branch information
nicokant committed May 2, 2024
1 parent 800d93e commit ca81ddd
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions metadata_catalogue/maps/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.parsers import MultiPartParser
from rest_framework.response import Response

from ..models import Layer, LayerGroup, Map, RasterSource, VectorSource
from .serializers import (
Expand Down Expand Up @@ -29,16 +30,16 @@ class MapViewSet(viewsets.ModelViewSet):


class UploadableMixin:
@action(detail=True, methods=["post"], parser_classes=(MultiPartParser,), serializer_class=FileUploadSerializer)
def upload(self, request):
@action(detail=True, methods=["post"], parser_classes=(MultiPartParser,))
def upload(self, request, *args, **kwargs):
instance = self.get_object()
serializer = self.get_serializer(data=request.data)
serializer = FileUploadSerializer(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
return Response(self.get_serializer_class()(instance).data)


class RasterSourceViewSet(UploadableMixin, viewsets.ModelViewSet):
Expand All @@ -47,9 +48,9 @@ class RasterSourceViewSet(UploadableMixin, viewsets.ModelViewSet):
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)
@action(detail=True, methods=["post"], parser_classes=(MultiPartParser,))
def upload(self, request, *args, **kwargs):
return super().upload(request, *args, **kwargs)


class VectorSourceViewSet(UploadableMixin, viewsets.ModelViewSet):
Expand All @@ -58,9 +59,9 @@ class VectorSourceViewSet(UploadableMixin, viewsets.ModelViewSet):
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)
@action(detail=True, methods=["post"], parser_classes=(MultiPartParser,))
def upload(self, request, *args, **kwargs):
return super().upload(request, *args, **kwargs)


class LayerViewSet(viewsets.ModelViewSet):
Expand Down

0 comments on commit ca81ddd

Please sign in to comment.