From fbf5552a62b770bdf5b2246da128b065ae75c9df Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Mon, 29 Jun 2020 13:20:23 +0300 Subject: [PATCH] Modify binary64 field to support binary renderers/parsers Fixes 39c1dfc53c30e65bcbff9e0ba0bb07bfc8bfc577 --- django_etebase/serializers.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/django_etebase/serializers.py b/django_etebase/serializers.py index e7c0d50..1876572 100644 --- a/django_etebase/serializers.py +++ b/django_etebase/serializers.py @@ -58,19 +58,16 @@ def b64decode(data): class BinaryBase64Field(serializers.Field): def to_representation(self, value): - return b64encode(value) - - def to_internal_value(self, data): - return b64decode(data) - - -# This field does nothing to the data. It's useful for raw binary data -class RawField(serializers.Field): - def to_representation(self, value): - return value + if self.context.get('supports_binary', False): + return value + else: + return b64encode(value) def to_internal_value(self, data): - return data + if isinstance(data, bytes): + return data + else: + return b64decode(data) class CollectionEncryptionKeyField(BinaryBase64Field): @@ -422,7 +419,7 @@ class AuthenticationLoginSerializer(serializers.Serializer): class AuthenticationLoginInnerSerializer(AuthenticationLoginChallengeSerializer): - challenge = RawField() + challenge = BinaryBase64Field() host = serializers.CharField() action = serializers.CharField()