Skip to content

Commit

Permalink
Merge 686ede6 into 6e1ab47
Browse files Browse the repository at this point in the history
  • Loading branch information
Peace-Apple committed Apr 9, 2019
2 parents 6e1ab47 + 686ede6 commit 9b68c4c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion authors/apps/articles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ def post(self, request, slug):
data = {"subject": "[Article Reported]", "to":
serializer.data['article']['author']['email'],
"body": f"Your article was reported,These are the details:\n{data['reason']}"}
Utilities.send_email(data, 'article_reports')
Utilities.send_email(data,None,'article_reports')

return Response(serializer.data, status=status.HTTP_201_CREATED)

Expand Down
2 changes: 1 addition & 1 deletion authors/apps/authentication/tests/test_reset_password.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,4 @@ def test_password_change_provided(self):
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data['errors']
[0], "Password feild is required.")
[0], "Password field is required.")
15 changes: 9 additions & 6 deletions authors/apps/authentication/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from rest_framework import status, generics, exceptions
import jwt
from django.conf import settings
from django.contrib.sites.shortcuts import get_current_site
from django.core.mail import EmailMessage
from django.shortcuts import get_object_or_404
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.response import Response

from django.contrib.sites.shortcuts import get_current_site
from .models import User
from .renderers import UserJSONRenderer
from .serializers import (
Expand Down Expand Up @@ -41,7 +39,7 @@ def post(self, request):
"created an account on Authors heaven.",
user_data['email']
]
Utilities.send_email(message,'auth')
Utilities.send_email(message,None,'auth')

return Response(user_data, status=status.HTTP_201_CREATED)

Expand Down Expand Up @@ -120,6 +118,9 @@ class PasswordResetAPIView(generics.GenericAPIView):
serializer_class = ResetPasswordSerializer

def post(self, request):
domain=request.META.get('HTTP_ORIGIN', get_current_site(request).domain)


try:
get_object_or_404(User, email=request.data['email'])
message = [
Expand All @@ -132,7 +133,9 @@ def post(self, request):
"requested for password reset.",
request.data['email']
]
Utilities.send_email(message,'auth')


Utilities.send_email(message,domain,'password_rest')
return Response(
{
"message": "Please check your email for the reset password link."
Expand Down Expand Up @@ -173,7 +176,7 @@ def patch(self, request):
status.HTTP_400_BAD_REQUEST
)
except KeyError:
raise exceptions.ValidationError("Password feild is required.")
raise exceptions.ValidationError("Password field is required.")
except jwt.ExpiredSignatureError:
return Response({
"error": "verification link is expired"},
Expand Down
10 changes: 9 additions & 1 deletion authors/apps/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ class Utilities:
"""Utility class that contains helper function"""

@staticmethod
def send_email(data,intent=None):
def send_email(data,domain=None,intent=None):
"""This function sends email to users."""
if intent == 'article_reports':
EmailMessage(data['subject'],body=data['body'],to=[data['to']]).send(fail_silently=False)

elif intent=='password_rest':
url = f"{domain}/reset_password/{data[2]}"
subject = f"[Authors Heaven] {data[3]}"
body = f"Hello, \
\nYou are receiving this e-mail because you have {data[4]}' \
'\nClick the click below to verify your account.\n{url}"
EmailMessage(subject, body, to=[data[5]]).send(fail_silently=False)
else:
url = f"http://{get_current_site(data[0]).domain}/api/users/{data[1]}?token={data[2]}"
subject = f"[Authors Heaven] {data[3]}"
Expand Down

0 comments on commit 9b68c4c

Please sign in to comment.