From dd7a2a53114f038f8089c00491dcfca29f471f0c Mon Sep 17 00:00:00 2001 From: Andrzej Pragacz Date: Mon, 24 Jun 2019 01:35:50 +0200 Subject: [PATCH] Issue #57: Pass request in serializer context in all views --- .../api/views/change_password.py | 6 +++-- rest_registration/api/views/login.py | 10 +++++++-- rest_registration/api/views/profile.py | 6 ++++- rest_registration/api/views/register.py | 22 ++++++++++++++----- rest_registration/api/views/register_email.py | 17 ++++++++++---- rest_registration/api/views/reset_password.py | 17 ++++++++++---- 6 files changed, 60 insertions(+), 18 deletions(-) diff --git a/rest_registration/api/views/change_password.py b/rest_registration/api/views/change_password.py index bd01897..7298f49 100644 --- a/rest_registration/api/views/change_password.py +++ b/rest_registration/api/views/change_password.py @@ -46,8 +46,10 @@ def change_password(request): ''' Change the user password. ''' - serializer = ChangePasswordSerializer(data=request.data, - context={'request': request}) + serializer = ChangePasswordSerializer( + data=request.data, + context={'request': request}, + ) serializer.is_valid(raise_exception=True) user = request.user diff --git a/rest_registration/api/views/login.py b/rest_registration/api/views/login.py index 3a0cf6a..a2ba881 100644 --- a/rest_registration/api/views/login.py +++ b/rest_registration/api/views/login.py @@ -27,7 +27,10 @@ def login(request): Logs in the user via given login and password. ''' serializer_class = registration_settings.LOGIN_SERIALIZER_CLASS - serializer = serializer_class(data=request.data) + serializer = serializer_class( + data=request.data, + context={'request': request}, + ) serializer.is_valid(raise_exception=True) user = serializer.get_authenticated_user() @@ -52,7 +55,10 @@ def logout(request): authenticated. ''' user = request.user - serializer = LogoutSerializer(data=request.data) + serializer = LogoutSerializer( + data=request.data, + context={'request': request}, + ) serializer.is_valid(raise_exception=True) data = serializer.validated_data diff --git a/rest_registration/api/views/profile.py b/rest_registration/api/views/profile.py index a1e4248..e77afe2 100644 --- a/rest_registration/api/views/profile.py +++ b/rest_registration/api/views/profile.py @@ -21,10 +21,14 @@ def profile(request): instance=request.user, data=request.data, partial=partial, + context={'request': request}, ) serializer.is_valid(raise_exception=True) serializer.save() else: # request.method == 'GET': - serializer = serializer_class(instance=request.user) + serializer = serializer_class( + instance=request.user, + context={'request': request}, + ) return Response(serializer.data) diff --git a/rest_registration/api/views/register.py b/rest_registration/api/views/register.py index be12b42..f9627a7 100644 --- a/rest_registration/api/views/register.py +++ b/rest_registration/api/views/register.py @@ -61,7 +61,10 @@ def register(request): Register new user. ''' serializer_class = registration_settings.REGISTER_SERIALIZER_CLASS - serializer = serializer_class(data=request.data) + serializer = serializer_class( + data=request.data, + context={'request': request}, + ) serializer.is_valid(raise_exception=True) kwargs = {} @@ -78,7 +81,10 @@ def register(request): signals.user_registered.send(sender=None, user=user, request=request) output_serializer_class = registration_settings.REGISTER_OUTPUT_SERIALIZER_CLASS # noqa: E501 - output_serializer = output_serializer_class(instance=user) + output_serializer = output_serializer_class( + instance=user, + context={'request': request}, + ) user_data = output_serializer.data if registration_settings.REGISTER_VERIFICATION_ENABLED: @@ -105,7 +111,8 @@ def verify_registration(request): """ Verify registration via signature. """ - user = process_verify_registration_data(request.data) + user = process_verify_registration_data( + request.data, serializer_context={'request': request}) signals.user_activated.send(sender=None, user=user, request=request) extra_data = None if registration_settings.REGISTER_VERIFICATION_AUTO_LOGIN: @@ -113,10 +120,15 @@ def verify_registration(request): return get_ok_response('User verified successfully', extra_data=extra_data) -def process_verify_registration_data(input_data): +def process_verify_registration_data(input_data, serializer_context=None): + if serializer_context is None: + serializer_context = {} if not registration_settings.REGISTER_VERIFICATION_ENABLED: raise Http404() - serializer = VerifyRegistrationSerializer(data=input_data) + serializer = VerifyRegistrationSerializer( + data=input_data, + context=serializer_context, + ) serializer.is_valid(raise_exception=True) data = serializer.validated_data diff --git a/rest_registration/api/views/register_email.py b/rest_registration/api/views/register_email.py index 6d55cb0..ca6ebbf 100644 --- a/rest_registration/api/views/register_email.py +++ b/rest_registration/api/views/register_email.py @@ -41,7 +41,10 @@ def register_email(request): user = request.user serializer_class = registration_settings.REGISTER_EMAIL_SERIALIZER_CLASS - serializer = serializer_class(data=request.data) + serializer = serializer_class( + data=request.data, + context={'request': request}, + ) serializer.is_valid(raise_exception=True) email = serializer.get_email() @@ -77,14 +80,20 @@ def verify_email(request): ''' Verify email via signature. ''' - process_verify_email_data(request.data) + process_verify_email_data( + request.data, serializer_context={'request': request}) return get_ok_response('Email verified successfully') -def process_verify_email_data(input_data): +def process_verify_email_data(input_data, serializer_context=None): + if serializer_context is None: + serializer_context = {} if not registration_settings.REGISTER_EMAIL_VERIFICATION_ENABLED: raise Http404() - serializer = VerifyEmailSerializer(data=input_data) + serializer = VerifyEmailSerializer( + data=input_data, + context=serializer_context, + ) serializer.is_valid(raise_exception=True) data = serializer.validated_data diff --git a/rest_registration/api/views/reset_password.py b/rest_registration/api/views/reset_password.py index 7ce8279..7365472 100644 --- a/rest_registration/api/views/reset_password.py +++ b/rest_registration/api/views/reset_password.py @@ -58,7 +58,10 @@ def send_reset_password_link(request): if not registration_settings.RESET_PASSWORD_VERIFICATION_ENABLED: raise Http404() serializer_class = registration_settings.SEND_RESET_PASSWORD_LINK_SERIALIZER_CLASS # noqa: E501 - serializer = serializer_class(data=request.data) + serializer = serializer_class( + data=request.data, + context={'request': request}, + ) serializer.is_valid(raise_exception=True) user = serializer.get_user_or_none() if not user: @@ -88,14 +91,20 @@ def reset_password(request): ''' Reset password, given the signature and timestamp from the link. ''' - process_reset_password_data(request.data) + process_reset_password_data( + request.data, serializer_context={'request': request}) return get_ok_response('Reset password successful') -def process_reset_password_data(input_data): +def process_reset_password_data(input_data, serializer_context=None): + if serializer_context is None: + serializer_context = {} if not registration_settings.RESET_PASSWORD_VERIFICATION_ENABLED: raise Http404() - serializer = ResetPasswordSerializer(data=input_data) + serializer = ResetPasswordSerializer( + data=input_data, + context=serializer_context, + ) serializer.is_valid(raise_exception=True) data = serializer.validated_data.copy()