From 3fd0e4b481b05f97606ddf181affbe87fd264eeb Mon Sep 17 00:00:00 2001 From: johnthagen Date: Tue, 19 Oct 2021 13:39:52 -0400 Subject: [PATCH 1/2] Add blueprint for drf-extra-fields Base64FileField --- docs/blueprints.rst | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/docs/blueprints.rst b/docs/blueprints.rst index ee00810f..6f6a3231 100644 --- a/docs/blueprints.rst +++ b/docs/blueprints.rst @@ -104,3 +104,17 @@ Framework ones adding separated serializers for read and write operations. __ https://github.com/vintasoftware/drf-rw-serializers .. literalinclude:: blueprints/drf_rw_serializers.py + +drf-extra-fields Base64FileField +-------------------------------- + +`drf-extra-fields`__ provides a ``Base64ImageField`` and ``Base64FileField`` that automatically +represent binary files as base64 encoded strings. This is a useful way to embed files within a +larger JSON API and keep all data within the same tree and served with a single request or +response. + +Because requests to these fields require a base64 encoded string and responses can be either a +URI or base64 contents (if ``represent_as_base64`` is set to ``True``) custom schema generation +logic is required as this differs from the default DRF ``FileField``. + +__ https://github.com/Hipo/drf-extra-fields From 6c4c2f1402ea9062ea76106de1f333b4daaa69d0 Mon Sep 17 00:00:00 2001 From: johnthagen Date: Tue, 19 Oct 2021 13:49:36 -0400 Subject: [PATCH 2/2] Add blueprint --- docs/blueprints.rst | 6 ++++-- docs/blueprints/drf_extra_fields.py | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 docs/blueprints/drf_extra_fields.py diff --git a/docs/blueprints.rst b/docs/blueprints.rst index 6f6a3231..386e1217 100644 --- a/docs/blueprints.rst +++ b/docs/blueprints.rst @@ -108,13 +108,15 @@ __ https://github.com/vintasoftware/drf-rw-serializers drf-extra-fields Base64FileField -------------------------------- -`drf-extra-fields`__ provides a ``Base64ImageField`` and ``Base64FileField`` that automatically +`drf-extra-fields`__ provides a ``Base64FileField`` and ``Base64ImageField`` that automatically represent binary files as base64 encoded strings. This is a useful way to embed files within a larger JSON API and keep all data within the same tree and served with a single request or response. Because requests to these fields require a base64 encoded string and responses can be either a -URI or base64 contents (if ``represent_as_base64`` is set to ``True``) custom schema generation +URI or base64 contents (if ``represent_as_base64=True``) custom schema generation logic is required as this differs from the default DRF ``FileField``. +.. literalinclude:: blueprints/drf_extra_fields.py + __ https://github.com/Hipo/drf-extra-fields diff --git a/docs/blueprints/drf_extra_fields.py b/docs/blueprints/drf_extra_fields.py new file mode 100644 index 00000000..718fbc95 --- /dev/null +++ b/docs/blueprints/drf_extra_fields.py @@ -0,0 +1,20 @@ +from drf_spectacular.extensions import OpenApiSerializerFieldExtension +from drf_spectacular.plumbing import build_basic_type +from drf_spectacular.types import OpenApiTypes + + +class Base64FileFieldSchema(OpenApiSerializerFieldExtension): + target_class = "drf_extra_fields.fields.Base64FileField" + + def map_serializer_field(self, auto_schema, direction): + if direction == "request": + return build_basic_type(OpenApiTypes.BYTE) + elif direction == "response": + if self.target.represent_in_base64: + return build_basic_type(OpenApiTypes.BYTE) + else: + return build_basic_type(OpenApiTypes.URI) + + +class Base64ImageFieldSchema(Base64FileFieldSchema): + target_class = "drf_extra_fields.fields.Base64ImageField"