Commit d08f6fb93496f40804dacfb89950f55b14609255

Authored by Chung Wang
1 parent abb2506d35
Exists in master

In PATCH, added email confirmation

Showing 1 changed file with 10 additions and 2 deletions Inline Diff

flashcards/api.py View file @ d08f6fb
from django.core.mail import send_mail 1 1 from django.core.mail import send_mail
from rest_framework.views import APIView 2 2 from rest_framework.views import APIView
from rest_framework.response import Response 3 3 from rest_framework.response import Response
from rest_framework import status 4 4 from rest_framework import status
from rest_framework.exceptions import ValidationError 5 5 from rest_framework.exceptions import ValidationError
from flashcards.serializers import * 6 6 from flashcards.serializers import *
7 7
8 8
class UserDetail(APIView): 9 9 class UserDetail(APIView):
def patch(self, request, format=None): 10 10 def patch(self, request, format=None):
""" 11 11 """
Updates a user's password after they enter a valid old password. 12 12 Updates a user's password after they enter a valid old password.
TODO: email verification 13 13 TODO: email verification
""" 14 14 """
15 15
if 'old_password' not in request.data: 16 16 if 'old_password' not in request.data:
raise ValidationError('Old password is required') 17 17 raise ValidationError('Old password is required')
if 'new_password' not in request.data: 18 18 if 'new_password' not in request.data:
raise ValidationError('New password is required') 19 19 raise ValidationError('New password is required')
if not request.data['new_password']: 20 20 if not request.data['new_password']:
raise ValidationError('Password cannot be blank') 21 21 raise ValidationError('Password cannot be blank')
22 22
currentuser = request.user 23 23 currentuser = request.user
24 24
if not currentuser.check_password(request.data['old_password']): 25 25 if not currentuser.check_password(request.data['old_password']):
raise ValidationError('Invalid old password') 26 26 raise ValidationError('Invalid old password')
27 27
currentuser.set_password(request.data['new_password']) 28 28 send_mail("Please verify your Flashy account",
currentuser.save() 29 29 body % currentuser.confirmation_key,
30 "noreply@flashy.cards",
31 [currentuser.email])
32
33 currentuser.confirm_email( currentuser.confirmation_key )
34
35 if currentuser.isconfirmed
36 currentuser.set_password(request.data['new_password'])
37 currentuser.save()
30 38
return Response(status=status.HTTP_204_NO_CONTENT) 31 39 return Response(status=status.HTTP_204_NO_CONTENT)
32 40
def get(self, request, format=None): 33 41 def get(self, request, format=None):
serializer = UserSerializer(request.user) 34 42 serializer = UserSerializer(request.user)
return Response(serializer.data) 35 43 return Response(serializer.data)
36 44
def post(self, request, format=None): 37 45 def post(self, request, format=None):
if 'email' not in request.data: 38 46 if 'email' not in request.data:
raise ValidationError('Email is required') 39 47 raise ValidationError('Email is required')
if 'password' not in request.data: 40 48 if 'password' not in request.data:
raise ValidationError('Password is required') 41 49 raise ValidationError('Password is required')
42 50
email = request.data['email'] 43 51 email = request.data['email']
user = User.objects.create_user(email) 44 52 user = User.objects.create_user(email)
45 53
body = ''' 46 54 body = '''