From a907efc06b6b31971a79df17139256649b1440fa Mon Sep 17 00:00:00 2001 From: Maxim Kukhtenkov Date: Mon, 31 Oct 2016 20:45:33 -0700 Subject: [PATCH 1/3] Allow using custom UserDetailsSerializer with JWTSerializer --- rest_auth/serializers.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/rest_auth/serializers.py b/rest_auth/serializers.py index a2c11ad..864b016 100644 --- a/rest_auth/serializers.py +++ b/rest_auth/serializers.py @@ -135,7 +135,16 @@ class JWTSerializer(serializers.Serializer): Serializer for JWT authentication. """ token = serializers.CharField() - user = UserDetailsSerializer() + + def __init__(self, *args, **kwargs): + """ + Need to add `user` field dynamically, to allow using + custom UserDetailsSerializer + """ + from app_settings import UserDetailsSerializer + + super(JWTSerializer, self).__init__(*args, **kwargs) + self.fields['user'] = UserDetailsSerializer() class PasswordResetSerializer(serializers.Serializer): From dd6db3563f9205e4a42d171f25948cc110e13b8b Mon Sep 17 00:00:00 2001 From: Maxim Kukhtenkov Date: Sun, 27 Nov 2016 17:57:55 -0800 Subject: [PATCH 2/3] Allow using custom UserDetailsSerializer with JWTSerializer - update --- rest_auth/serializers.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/rest_auth/serializers.py b/rest_auth/serializers.py index 864b016..4d5d194 100644 --- a/rest_auth/serializers.py +++ b/rest_auth/serializers.py @@ -6,11 +6,12 @@ from django.utils.http import urlsafe_base64_decode as uid_decoder from django.utils.translation import ugettext_lazy as _ from django.utils.encoding import force_text -from .models import TokenModel - from rest_framework import serializers, exceptions from rest_framework.exceptions import ValidationError +from .models import TokenModel +from .utils import import_callable + # Get the UserModel UserModel = get_user_model() @@ -130,21 +131,20 @@ class UserDetailsSerializer(serializers.ModelSerializer): read_only_fields = ('email', ) +# Required to allow using custom UserDetailsSerializer in +# JWTSerializer +rest_auth_serializers = getattr(settings, 'REST_AUTH_SERIALIZERS', {}) +JWTUserDetailsSerializer = import_callable( + rest_auth_serializers.get('USER_DETAILS_SERIALIZER', UserDetailsSerializer) +) + + class JWTSerializer(serializers.Serializer): """ Serializer for JWT authentication. """ token = serializers.CharField() - - def __init__(self, *args, **kwargs): - """ - Need to add `user` field dynamically, to allow using - custom UserDetailsSerializer - """ - from app_settings import UserDetailsSerializer - - super(JWTSerializer, self).__init__(*args, **kwargs) - self.fields['user'] = UserDetailsSerializer() + user = JWTUserDetailsSerializer() class PasswordResetSerializer(serializers.Serializer): From ca62f440611e5c225cf7c98ed30fe33998eeb8e6 Mon Sep 17 00:00:00 2001 From: Maxim Kukhtenkov Date: Wed, 30 Nov 2016 20:03:34 -0800 Subject: [PATCH 3/3] Append more information to comment Explain why we are defining JWTUserDetailsSerializer in registration/serializers.py instead of getting it from app_settings.py --- rest_auth/serializers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rest_auth/serializers.py b/rest_auth/serializers.py index 4d5d194..d682566 100644 --- a/rest_auth/serializers.py +++ b/rest_auth/serializers.py @@ -131,8 +131,8 @@ class UserDetailsSerializer(serializers.ModelSerializer): read_only_fields = ('email', ) -# Required to allow using custom UserDetailsSerializer in -# JWTSerializer +# Required to allow using custom USER_DETAILS_SERIALIZER in +# JWTSerializer. Defining it here to avoid circular imports rest_auth_serializers = getattr(settings, 'REST_AUTH_SERIALIZERS', {}) JWTUserDetailsSerializer = import_callable( rest_auth_serializers.get('USER_DETAILS_SERIALIZER', UserDetailsSerializer)