Skip to content

Commit

Permalink
[VNG-Realisatie#210] add FileFieldExtension
Browse files Browse the repository at this point in the history
  • Loading branch information
Sonny Bakker committed Sep 20, 2022
1 parent e7bf3d6 commit e199a8d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions vng_api_common/inspectors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
HyperlinkedRelatedFieldExtension,
ReadOnlyFieldExtension,
)
from .files import FileFieldExtension
from .geojson import GeometryFieldExtension
from .polymorphic import PolymorphicSerializerExtension
from .query import FilterExtension
26 changes: 26 additions & 0 deletions vng_api_common/inspectors/files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from django.utils.translation import gettext as _

from drf_spectacular.extensions import OpenApiSerializerFieldExtension
from drf_spectacular.plumbing import build_basic_type
from drf_spectacular.types import OpenApiTypes


class FileFieldExtension(OpenApiSerializerFieldExtension):
target_class = "drf_extra_fields.fields.Base64FileField"
match_subclasses = True

def map_serializer_field(self, auto_schema, direction):
base64_schema = {
**build_basic_type(OpenApiTypes.BYTE),
"description": _("Base64 encoded binary content."),
}

uri_schema = {
**build_basic_type(OpenApiTypes.URI),
"description": _("Download URL of the binary content."),
}

if direction == "request":
return base64_schema
elif direction == "response":
return uri_schema if not self.target.represent_in_base64 else base64_schema

0 comments on commit e199a8d

Please sign in to comment.