forked from VNG-Realisatie/vng-api-common
-
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.
[VNG-Realisatie#210] add FileFieldExtension
Based on https://drf-spectacular.readthedocs.io/en/latest/blueprints.html#drf-extra-fields-base64filefield
- Loading branch information
Sonny Bakker
committed
Sep 20, 2022
1 parent
e7bf3d6
commit e199a8d
Showing
2 changed files
with
27 additions
and
0 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,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 |