Skip to content

Commit

Permalink
164047059-fix(authentication): changes email subject and font
Browse files Browse the repository at this point in the history
- 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]
  • Loading branch information
Kibetchirchir committed Mar 10, 2019
1 parent 7010e00 commit 02d8dc2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 38 deletions.
4 changes: 2 additions & 2 deletions authors/apps/authentication/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
55 changes: 28 additions & 27 deletions authors/apps/authentication/views.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,7 +15,7 @@
from .serializers import (
LoginSerializer, RegistrationSerializer, UserSerializer,
PasswordResetRequestSerializer, SetNewPasswordSerializer,
EmailVerificationSerializer,SocialSerializer
EmailVerificationSerializer, SocialSerializer
)
from .utils import send_email, verify_message

Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -104,46 +103,47 @@ 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:
backend = load_backend(strategy=strategy, name=provider, redirect_uri=None)
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,)
Expand All @@ -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
Expand Down Expand Up @@ -202,4 +203,4 @@ def put(self, request, reset_token):

return Response(
{'message': message},
status=status.HTTP_202_ACCEPTED)
status=status.HTTP_202_ACCEPTED)
11 changes: 2 additions & 9 deletions authors/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand All @@ -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',
Expand All @@ -189,7 +184,6 @@
'social_core.pipeline.user.user_details',
)


AUTHENTICATION_BACKENDS = (
# Google OAuth2
'social_core.backends.google.GoogleOAuth2',
Expand All @@ -202,6 +196,5 @@
'django.contrib.auth.backends.ModelBackend',
)


# activate django_heroku
django_heroku.settings(locals())

0 comments on commit 02d8dc2

Please sign in to comment.