Skip to content

Commit

Permalink
Merge pull request #133 from aaronn/improvement/pass-serializer-context
Browse files Browse the repository at this point in the history
Pass context to serializer and bump version.
  • Loading branch information
aaronn committed Oct 11, 2023
2 parents 36592ea + 9e237e2 commit 2604dfb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/runtests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python: [3.6, 3.7]
python: [3.7, 3.8]

steps:
- uses: actions/checkout@v1
Expand Down
4 changes: 2 additions & 2 deletions drfpasswordless/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# -*- coding: utf-8 -*-

__title__ = 'drfpasswordless'
__version__ = '1.5.8'
__version__ = '1.5.9'
__author__ = 'Aaron Ng'
__license__ = 'MIT'
__copyright__ = 'Copyright 2022 Aaron Ng'
__copyright__ = 'Copyright 2023 Aaron Ng'

# Version synonym
VERSION = __version__
Expand Down
2 changes: 1 addition & 1 deletion drfpasswordless/__version__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
VERSION = (1, 5, 8)
VERSION = (1, 5, 9)

__version__ = '.'.join(map(str, VERSION))
38 changes: 19 additions & 19 deletions drfpasswordless/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def post(self, request, *args, **kwargs):
else:
status_code = status.HTTP_400_BAD_REQUEST
response_detail = self.failure_response
return Response({'detail': response_detail}, status=status_code)
return Response({"detail": response_detail}, status=status_code)
else:
return Response(serializer.error_messages, status=status.HTTP_400_BAD_REQUEST)

Expand All @@ -73,15 +73,15 @@ class ObtainEmailCallbackToken(AbstractBaseObtainCallbackToken):
success_response = "A login token has been sent to your email."
failure_response = "Unable to email you a login code. Try again later."

alias_type = 'email'
alias_type = "email"
token_type = CallbackToken.TOKEN_TYPE_AUTH

email_subject = api_settings.PASSWORDLESS_EMAIL_SUBJECT
email_plaintext = api_settings.PASSWORDLESS_EMAIL_PLAINTEXT_MESSAGE
email_html = api_settings.PASSWORDLESS_EMAIL_TOKEN_HTML_TEMPLATE_NAME
message_payload = {'email_subject': email_subject,
'email_plaintext': email_plaintext,
'email_html': email_html}
message_payload = {"email_subject": email_subject,
"email_plaintext": email_plaintext,
"email_html": email_html}


class ObtainMobileCallbackToken(AbstractBaseObtainCallbackToken):
Expand All @@ -90,11 +90,11 @@ class ObtainMobileCallbackToken(AbstractBaseObtainCallbackToken):
success_response = "We texted you a login code."
failure_response = "Unable to send you a login code. Try again later."

alias_type = 'mobile'
alias_type = "mobile"
token_type = CallbackToken.TOKEN_TYPE_AUTH

mobile_message = api_settings.PASSWORDLESS_MOBILE_MESSAGE
message_payload = {'mobile_message': mobile_message}
message_payload = {"mobile_message": mobile_message}


class ObtainEmailVerificationCallbackToken(AbstractBaseObtainCallbackToken):
Expand All @@ -103,16 +103,16 @@ class ObtainEmailVerificationCallbackToken(AbstractBaseObtainCallbackToken):
success_response = "A verification token has been sent to your email."
failure_response = "Unable to email you a verification code. Try again later."

alias_type = 'email'
alias_type = "email"
token_type = CallbackToken.TOKEN_TYPE_VERIFY

email_subject = api_settings.PASSWORDLESS_EMAIL_VERIFICATION_SUBJECT
email_plaintext = api_settings.PASSWORDLESS_EMAIL_VERIFICATION_PLAINTEXT_MESSAGE
email_html = api_settings.PASSWORDLESS_EMAIL_VERIFICATION_TOKEN_HTML_TEMPLATE_NAME
message_payload = {
'email_subject': email_subject,
'email_plaintext': email_plaintext,
'email_html': email_html
"email_subject": email_subject,
"email_plaintext": email_plaintext,
"email_html": email_html
}


Expand All @@ -122,11 +122,11 @@ class ObtainMobileVerificationCallbackToken(AbstractBaseObtainCallbackToken):
success_response = "We texted you a verification code."
failure_response = "Unable to send you a verification code. Try again later."

alias_type = 'mobile'
alias_type = "mobile"
token_type = CallbackToken.TOKEN_TYPE_VERIFY

mobile_message = api_settings.PASSWORDLESS_MOBILE_MESSAGE
message_payload = {'mobile_message': mobile_message}
message_payload = {"mobile_message": mobile_message}


class AbstractBaseObtainAuthToken(APIView):
Expand All @@ -139,19 +139,19 @@ class AbstractBaseObtainAuthToken(APIView):
def post(self, request, *args, **kwargs):
serializer = self.serializer_class(data=request.data)
if serializer.is_valid(raise_exception=True):
user = serializer.validated_data['user']
user = serializer.validated_data["user"]
token_creator = import_string(api_settings.PASSWORDLESS_AUTH_TOKEN_CREATOR)
(token, _) = token_creator(user)

if token:
TokenSerializer = import_string(api_settings.PASSWORDLESS_AUTH_TOKEN_SERIALIZER)
token_serializer = TokenSerializer(data=token.__dict__, partial=True)
token_serializer = TokenSerializer(data=token.__dict__, partial=True, context={"request": request})
if token_serializer.is_valid():
# Return our key for consumption.
return Response(token_serializer.data, status=status.HTTP_200_OK)
else:
logger.error("Couldn't log in unknown user. Errors on serializer: {}".format(serializer.error_messages))
return Response({'detail': 'Couldn\'t log you in. Try again later.'}, status=status.HTTP_400_BAD_REQUEST)
return Response({"detail": "Couldn't log you in. Try again later."}, status=status.HTTP_400_BAD_REQUEST)


class ObtainAuthTokenFromCallbackToken(AbstractBaseObtainAuthToken):
Expand All @@ -171,10 +171,10 @@ class VerifyAliasFromCallbackToken(APIView):
serializer_class = CallbackTokenVerificationSerializer

def post(self, request, *args, **kwargs):
serializer = self.serializer_class(data=request.data, context={'user_id': self.request.user.id})
serializer = self.serializer_class(data=request.data, context={"user_id": self.request.user.id})
if serializer.is_valid(raise_exception=True):
return Response({'detail': 'Alias verified.'}, status=status.HTTP_200_OK)
return Response({"detail": "Alias verified."}, status=status.HTTP_200_OK)
else:
logger.error("Couldn't verify unknown user. Errors on serializer: {}".format(serializer.error_messages))

return Response({'detail': 'We couldn\'t verify this alias. Try again later.'}, status.HTTP_400_BAD_REQUEST)
return Response({"detail": "We couldn't verify this alias. Try again later."}, status.HTTP_400_BAD_REQUEST)

0 comments on commit 2604dfb

Please sign in to comment.