From 02d8dc29d5d6d4fd9f84131d55893e15059e7207 Mon Sep 17 00:00:00 2001 From: Kelvin Chirchir Date: Sun, 10 Mar 2019 16:58:29 +0300 Subject: [PATCH] 164047059-fix(authentication): changes email subject and font - change email subject to (Verify Your Email to Complete Your Authors Haven Registration) - changes the font of the email to be constant - edits settings.py to follow PEP8 standards [Finishes #164047059] --- authors/apps/authentication/utils.py | 4 +- authors/apps/authentication/views.py | 55 ++++++++++++++-------------- authors/settings.py | 11 +----- 3 files changed, 32 insertions(+), 38 deletions(-) diff --git a/authors/apps/authentication/utils.py b/authors/apps/authentication/utils.py index f2fa2e2..224f3d4 100644 --- a/authors/apps/authentication/utils.py +++ b/authors/apps/authentication/utils.py @@ -29,8 +29,8 @@ def send_email(to_email, subject, message): def verify_message(name, token): - message = "Thank you " + name + " for registering with us please verify your email\n" \ - " by clicking on the following link\n" \ + message = " Thank you " + name + " for registering with us please verify your email\n " \ + "by clicking on the following link\n " \ + os.getenv("URL") + "/verify/" + token + "\n Welcome" return message diff --git a/authors/apps/authentication/views.py b/authors/apps/authentication/views.py index e341443..cc5f4f6 100644 --- a/authors/apps/authentication/views.py +++ b/authors/apps/authentication/views.py @@ -1,11 +1,11 @@ from rest_framework import status -from rest_framework.generics import RetrieveUpdateAPIView,CreateAPIView +from rest_framework.generics import RetrieveUpdateAPIView, CreateAPIView from rest_framework.permissions import AllowAny, IsAuthenticated from rest_framework.response import Response from rest_framework.views import APIView from social_django.utils import load_strategy, load_backend from social_core.exceptions import MissingBackend -from social_core.backends.oauth import BaseOAuth2,BaseOAuth1 +from social_core.backends.oauth import BaseOAuth2, BaseOAuth1 from .utils import send_email, verify_message from .models import EmailVerification from .renderers import UserJSONRenderer @@ -15,7 +15,7 @@ from .serializers import ( LoginSerializer, RegistrationSerializer, UserSerializer, PasswordResetRequestSerializer, SetNewPasswordSerializer, - EmailVerificationSerializer,SocialSerializer + EmailVerificationSerializer, SocialSerializer ) from .utils import send_email, verify_message @@ -38,7 +38,7 @@ def post(self, request): user_email = user.email username = user.username sign_up_message = verify_message(username, token) - send_email(user_email, "verify", sign_up_message) + send_email(user_email, "Verify Your Email to Complete Your Authors Haven Registration", sign_up_message) return Response(serializer.data, status=status.HTTP_201_CREATED) @@ -93,8 +93,7 @@ class SocialAuthentication(CreateAPIView): renderer_classes = (UserJSONRenderer,) serializer_class = SocialSerializer - - def create(self,request,*args,**kwargs): + def create(self, request, *args, **kwargs): ''' Fetch the access token and then create a user or authenticate a user @@ -104,7 +103,7 @@ def create(self,request,*args,**kwargs): serializer.is_valid(raise_exception=True) authenticated_user = request.user if not request.user.is_anonymous else None - provider = serializer.data['provider'] + provider = serializer.data['provider'] strategy = load_strategy(request) try: @@ -112,38 +111,39 @@ def create(self,request,*args,**kwargs): except MissingBackend as error: return Response( { - "errors":{ - "provider":["provider was not found",str(error)] - } - },status=status.HTTP_404_NOT_FOUND) - - if isinstance(backend,BaseOAuth1): + "errors": { + "provider": ["provider was not found", str(error)] + } + }, status=status.HTTP_404_NOT_FOUND) + + if isinstance(backend, BaseOAuth1): token = { - "oauth_token":serializer.data.get('access_token'), - "oauth_token_secret":serializer.data.get('access_token_secret') + "oauth_token": serializer.data.get('access_token'), + "oauth_token_secret": serializer.data.get('access_token_secret') } - if isinstance(backend,BaseOAuth2): - #Fetch the access token + if isinstance(backend, BaseOAuth2): + # Fetch the access token token = serializer.data['access_token'] try: - #check if there is an authenticated user,if true create a new one - user = backend.do_auth(token,user=authenticated_user) + # check if there is an authenticated user,if true create a new one + user = backend.do_auth(token, user=authenticated_user) print(user) except BaseException as error: # you cannot associate a social account with more than one user - return Response({"errors":str(error)},status=status.HTTP_400_BAD_REQUEST - ) - + return Response({"errors": str(error)}, status=status.HTTP_400_BAD_REQUEST + ) + if user and user.is_active: serializer = UserSerializer(user) - + serializer.instance = user - return Response(serializer.data,status=status.HTTP_201_CREATED) + return Response(serializer.data, status=status.HTTP_201_CREATED) else: - return Response({'errors':"Social aunthentication error"}, - status=status.HTTP_400_BAD_REQUEST) + return Response({'errors': "Social aunthentication error"}, + status=status.HTTP_400_BAD_REQUEST) + class UserEmailVerification(APIView): renderer_classes = (UserJSONRenderer,) @@ -163,6 +163,7 @@ def post(self, request): except EmailVerification.DoesNotExist: return Response({"error": "invalid token"}, status=status.HTTP_403_FORBIDDEN) + class ResetPasswordRequestAPIView(APIView): """This view class provides a view to request a password reset. :return: http Response object @@ -202,4 +203,4 @@ def put(self, request, reset_token): return Response( {'message': message}, - status=status.HTTP_202_ACCEPTED) \ No newline at end of file + status=status.HTTP_202_ACCEPTED) diff --git a/authors/settings.py b/authors/settings.py index 6d8d76b..2330438 100644 --- a/authors/settings.py +++ b/authors/settings.py @@ -155,14 +155,12 @@ AUTHENTICATION_BACKENDS = [ 'rest_framework_social_oauth2.backends.DjangoOAuth2', - 'django.contrib.auth.backends.ModelBackend', + 'django.contrib.auth.backends.ModelBackend', ] - - SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = os.environ['SOCIAL_AUTH_GOOGLE_OAUTH2_KEY'] SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = os.environ['SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET'] -#Scope +# Scope SOCIAL_AUTH_GOOGLE_OAUTH_SCOPE = ['email', 'username'] SOCIAL_AUTH_TWITTER_KEY = os.environ['SOCIAL_AUTH_TWITTER_KEY'] @@ -173,9 +171,6 @@ SOCIAL_AUTH_FACEBOOK_SCOPE = ['email'] SOCIAL_AUTH_FACEBOOK_SECRET = os.environ['SOCIAL_AUTH_FACEBOOK_SECRET'] - - - SOCIAL_AUTH_PIPELINE = ( 'social_core.pipeline.social_auth.social_details', 'social_core.pipeline.social_auth.social_uid', @@ -189,7 +184,6 @@ 'social_core.pipeline.user.user_details', ) - AUTHENTICATION_BACKENDS = ( # Google OAuth2 'social_core.backends.google.GoogleOAuth2', @@ -202,6 +196,5 @@ 'django.contrib.auth.backends.ModelBackend', ) - # activate django_heroku django_heroku.settings(locals())