Commit 15af7095ef58d5edc8446edb5e019a01e665b42b
1 parent
08559ed68a
Exists in
master
api/users/me delete account added
Showing 1 changed file with 4 additions and 0 deletions Inline Diff
flashcards/api.py
View file @
15af709
from django.core.mail import send_mail | 1 | 1 | from django.core.mail import send_mail | |
from django.contrib.auth import authenticate, login | 2 | 2 | from django.contrib.auth import authenticate, login | |
from rest_framework.views import APIView | 3 | 3 | from rest_framework.views import APIView | |
from rest_framework.response import Response | 4 | 4 | from rest_framework.response import Response | |
from rest_framework import status | 5 | 5 | from rest_framework import status | |
from rest_framework.exceptions import ValidationError | 6 | 6 | from rest_framework.exceptions import ValidationError | |
from flashcards.serializers import * | 7 | 7 | from flashcards.serializers import * | |
8 | 8 | |||
9 | 9 | |||
class UserDetail(APIView): | 10 | 10 | class UserDetail(APIView): | |
def patch(self, request, format=None): | 11 | 11 | def patch(self, request, format=None): | |
""" | 12 | 12 | """ | |
Updates a user's password after they enter a valid old password. | 13 | 13 | Updates a user's password after they enter a valid old password. | |
TODO: email verification | 14 | 14 | TODO: email verification | |
""" | 15 | 15 | """ | |
16 | 16 | |||
if 'old_password' not in request.data: | 17 | 17 | if 'old_password' not in request.data: | |
raise ValidationError('Old password is required') | 18 | 18 | raise ValidationError('Old password is required') | |
if 'new_password' not in request.data: | 19 | 19 | if 'new_password' not in request.data: | |
raise ValidationError('New password is required') | 20 | 20 | raise ValidationError('New password is required') | |
if not request.data['new_password']: | 21 | 21 | if not request.data['new_password']: | |
raise ValidationError('Password cannot be blank') | 22 | 22 | raise ValidationError('Password cannot be blank') | |
23 | 23 | |||
currentuser = request.user | 24 | 24 | currentuser = request.user | |
25 | 25 | |||
if not currentuser.check_password(request.data['old_password']): | 26 | 26 | if not currentuser.check_password(request.data['old_password']): | |
raise ValidationError('Invalid old password') | 27 | 27 | raise ValidationError('Invalid old password') | |
28 | 28 | |||
currentuser.set_password(request.data['new_password']) | 29 | 29 | currentuser.set_password(request.data['new_password']) | |
currentuser.save() | 30 | 30 | currentuser.save() | |
31 | 31 | |||
return Response(status=status.HTTP_204_NO_CONTENT) | 32 | 32 | return Response(status=status.HTTP_204_NO_CONTENT) | |
33 | 33 | |||
def get(self, request, format=None): | 34 | 34 | def get(self, request, format=None): | |
serializer = UserSerializer(request.user) | 35 | 35 | serializer = UserSerializer(request.user) | |
return Response(serializer.data) | 36 | 36 | return Response(serializer.data) | |
37 | 37 | |||
def post(self, request, format=None): | 38 | 38 | def post(self, request, format=None): | |
if 'email' not in request.data: | 39 | 39 | if 'email' not in request.data: | |
raise ValidationError('Email is required') | 40 | 40 | raise ValidationError('Email is required') | |
if 'password' not in request.data: | 41 | 41 | if 'password' not in request.data: | |
raise ValidationError('Password is required') | 42 | 42 | raise ValidationError('Password is required') | |
43 | 43 | |||
email = request.data['email'] | 44 | 44 | email = request.data['email'] | |
user = User.objects.create_user(email) | 45 | 45 | user = User.objects.create_user(email) | |
46 | 46 | |||
body = ''' | 47 | 47 | body = ''' | |
Visit the following link to confirm your email address: | 48 | 48 | Visit the following link to confirm your email address: | |
http://flashy.cards/app/verify_email/%s | 49 | 49 | http://flashy.cards/app/verify_email/%s | |
50 | 50 | |||
If you did not register for Flashy, no action is required. | 51 | 51 | If you did not register for Flashy, no action is required. | |
''' | 52 | 52 | ''' | |
53 | 53 | |||
send_mail("Please verify your Flashy account", | 54 | 54 | send_mail("Please verify your Flashy account", | |
body % user.confirmation_key, | 55 | 55 | body % user.confirmation_key, | |
"noreply@flashy.cards", | 56 | 56 | "noreply@flashy.cards", | |
[user.email]) | 57 | 57 | [user.email]) | |
58 | 58 | |||
return Response(UserSerializer(User).data) | 59 | 59 | return Response(UserSerializer(User).data) | |
60 | 60 | |||
61 | def delete(self, request, format=None): | |||
62 | request.user.delete() | |||
63 | return Response(status=status.HTTP_204_NO_CONTENT) | |||
64 | ||||
class UserLogin(APIView): | 61 | 65 | class UserLogin(APIView): | |
""" | 62 | 66 | """ | |
Authenticates user and returns user data if valid. Handles invalid | 63 | 67 | Authenticates user and returns user data if valid. Handles invalid | |
users. | 64 | 68 | users. | |
""" | 65 | 69 | """ | |
def post(self, request, format=None): | 66 | 70 | def post(self, request, format=None): |