2014-10-07 17:08:08 +04:00
|
|
|
from django.contrib.auth import login, logout
|
2014-04-30 23:52:05 +04:00
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
from rest_framework import status
|
|
|
|
from rest_framework.views import APIView
|
|
|
|
from rest_framework.response import Response
|
|
|
|
from rest_framework.generics import GenericAPIView
|
|
|
|
from rest_framework.permissions import IsAuthenticated, AllowAny
|
|
|
|
from rest_framework.authtoken.models import Token
|
2014-10-02 13:18:23 +04:00
|
|
|
from rest_framework.generics import RetrieveUpdateAPIView
|
2014-04-30 23:52:05 +04:00
|
|
|
|
2015-04-28 11:22:08 +03:00
|
|
|
from .app_settings import (
|
|
|
|
TokenSerializer, UserDetailsSerializer, LoginSerializer,
|
|
|
|
PasswordResetSerializer, PasswordResetConfirmSerializer,
|
|
|
|
PasswordChangeSerializer
|
|
|
|
)
|
2014-04-30 23:52:05 +04:00
|
|
|
|
|
|
|
|
2014-10-24 17:52:07 +04:00
|
|
|
class Login(GenericAPIView):
|
2014-04-30 23:52:05 +04:00
|
|
|
|
|
|
|
"""
|
|
|
|
Check the credentials and return the REST Token
|
|
|
|
if the credentials are valid and authenticated.
|
|
|
|
Calls Django Auth login method to register User ID
|
|
|
|
in Django session framework
|
|
|
|
|
|
|
|
Accept the following POST parameters: username, password
|
|
|
|
Return the REST Framework Token Object's key.
|
|
|
|
"""
|
2014-10-24 17:52:07 +04:00
|
|
|
permission_classes = (AllowAny,)
|
2014-04-30 23:52:05 +04:00
|
|
|
serializer_class = LoginSerializer
|
2014-05-30 13:17:25 +04:00
|
|
|
token_model = Token
|
2014-10-02 13:18:23 +04:00
|
|
|
response_serializer = TokenSerializer
|
2014-04-30 23:52:05 +04:00
|
|
|
|
2014-10-02 13:18:23 +04:00
|
|
|
def login(self):
|
2015-01-09 14:05:14 +03:00
|
|
|
self.user = self.serializer.validated_data['user']
|
2014-10-02 13:18:23 +04:00
|
|
|
self.token, created = self.token_model.objects.get_or_create(
|
|
|
|
user=self.user)
|
2014-10-01 17:31:10 +04:00
|
|
|
if getattr(settings, 'REST_SESSION_LOGIN', True):
|
2014-10-02 13:18:23 +04:00
|
|
|
login(self.request, self.user)
|
2014-10-01 17:31:10 +04:00
|
|
|
|
2014-10-02 13:18:23 +04:00
|
|
|
def get_response(self):
|
2015-04-28 11:22:08 +03:00
|
|
|
return Response(
|
|
|
|
self.response_serializer(self.token).data, status=status.HTTP_200_OK
|
|
|
|
)
|
2014-10-02 13:18:23 +04:00
|
|
|
|
|
|
|
def get_error_response(self):
|
2015-04-28 11:22:08 +03:00
|
|
|
return Response(
|
|
|
|
self.serializer.errors, status=status.HTTP_400_BAD_REQUEST
|
|
|
|
)
|
2014-10-02 13:18:23 +04:00
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
2014-11-12 12:33:29 +03:00
|
|
|
self.serializer = self.get_serializer(data=self.request.DATA)
|
2014-10-02 13:18:23 +04:00
|
|
|
if not self.serializer.is_valid():
|
|
|
|
return self.get_error_response()
|
|
|
|
self.login()
|
|
|
|
return self.get_response()
|
2014-04-30 23:52:05 +04:00
|
|
|
|
|
|
|
|
2014-10-24 17:52:07 +04:00
|
|
|
class Logout(APIView):
|
2014-04-30 23:52:05 +04:00
|
|
|
|
|
|
|
"""
|
|
|
|
Calls Django logout method and delete the Token object
|
|
|
|
assigned to the current User object.
|
|
|
|
|
|
|
|
Accepts/Returns nothing.
|
|
|
|
"""
|
2014-11-12 12:33:29 +03:00
|
|
|
permission_classes = (AllowAny,)
|
2014-04-30 23:52:05 +04:00
|
|
|
|
2014-10-02 13:18:23 +04:00
|
|
|
def post(self, request):
|
2014-04-30 23:52:05 +04:00
|
|
|
try:
|
|
|
|
request.user.auth_token.delete()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
logout(request)
|
|
|
|
|
|
|
|
return Response({"success": "Successfully logged out."},
|
|
|
|
status=status.HTTP_200_OK)
|
|
|
|
|
|
|
|
|
2014-10-24 17:52:07 +04:00
|
|
|
class UserDetails(RetrieveUpdateAPIView):
|
2014-04-30 23:52:05 +04:00
|
|
|
|
|
|
|
"""
|
|
|
|
Returns User's details in JSON format.
|
|
|
|
|
|
|
|
Accepts the following GET parameters: token
|
|
|
|
Accepts the following POST parameters:
|
|
|
|
Required: token
|
|
|
|
Optional: email, first_name, last_name and UserProfile fields
|
|
|
|
Returns the updated UserProfile and/or User object.
|
|
|
|
"""
|
2014-10-02 13:18:23 +04:00
|
|
|
serializer_class = UserDetailsSerializer
|
2014-10-24 17:52:07 +04:00
|
|
|
permission_classes = (IsAuthenticated,)
|
2014-04-30 23:52:05 +04:00
|
|
|
|
2014-10-02 13:18:23 +04:00
|
|
|
def get_object(self):
|
|
|
|
return self.request.user
|
2014-04-30 23:52:05 +04:00
|
|
|
|
|
|
|
|
2014-10-24 17:52:07 +04:00
|
|
|
class PasswordReset(GenericAPIView):
|
2014-04-30 23:52:05 +04:00
|
|
|
|
|
|
|
"""
|
|
|
|
Calls Django Auth PasswordResetForm save method.
|
|
|
|
|
|
|
|
Accepts the following POST parameters: email
|
|
|
|
Returns the success/fail message.
|
|
|
|
"""
|
|
|
|
|
|
|
|
serializer_class = PasswordResetSerializer
|
2014-10-24 17:52:07 +04:00
|
|
|
permission_classes = (AllowAny,)
|
2014-04-30 23:52:05 +04:00
|
|
|
|
2014-10-07 17:08:08 +04:00
|
|
|
def post(self, request, *args, **kwargs):
|
2014-04-30 23:52:05 +04:00
|
|
|
# Create a serializer with request.DATA
|
2014-10-07 17:08:08 +04:00
|
|
|
serializer = self.get_serializer(data=request.DATA)
|
2014-04-30 23:52:05 +04:00
|
|
|
|
2014-10-07 17:08:08 +04:00
|
|
|
if not serializer.is_valid():
|
2014-04-30 23:52:05 +04:00
|
|
|
return Response(serializer.errors,
|
|
|
|
status=status.HTTP_400_BAD_REQUEST)
|
2014-10-07 17:08:08 +04:00
|
|
|
serializer.save()
|
|
|
|
# Return the success message with OK HTTP status
|
2015-04-28 11:22:08 +03:00
|
|
|
return Response(
|
|
|
|
{"success": "Password reset e-mail has been sent."},
|
|
|
|
status=status.HTTP_200_OK
|
|
|
|
)
|
2014-04-30 23:52:05 +04:00
|
|
|
|
2014-05-01 00:55:04 +04:00
|
|
|
|
2014-10-24 17:52:07 +04:00
|
|
|
class PasswordResetConfirm(GenericAPIView):
|
2014-05-01 00:55:04 +04:00
|
|
|
|
2014-04-30 23:52:05 +04:00
|
|
|
"""
|
|
|
|
Password reset e-mail link is confirmed, therefore this resets the user's password.
|
|
|
|
|
|
|
|
Accepts the following POST parameters: new_password1, new_password2
|
2014-05-06 02:53:06 +04:00
|
|
|
Accepts the following Django URL arguments: token, uid
|
2014-04-30 23:52:05 +04:00
|
|
|
Returns the success/fail message.
|
|
|
|
"""
|
|
|
|
|
2014-10-07 17:08:08 +04:00
|
|
|
serializer_class = PasswordResetConfirmSerializer
|
2014-10-24 17:52:07 +04:00
|
|
|
permission_classes = (AllowAny,)
|
2014-04-30 23:52:05 +04:00
|
|
|
|
2014-10-07 17:08:08 +04:00
|
|
|
def post(self, request):
|
|
|
|
serializer = self.get_serializer(data=request.DATA)
|
|
|
|
if not serializer.is_valid():
|
2015-04-28 11:22:08 +03:00
|
|
|
return Response(
|
|
|
|
serializer.errors, status=status.HTTP_400_BAD_REQUEST
|
|
|
|
)
|
2014-10-07 17:08:08 +04:00
|
|
|
serializer.save()
|
|
|
|
return Response({"success": "Password has been reset with the new password."})
|
2014-04-30 23:52:05 +04:00
|
|
|
|
|
|
|
|
2014-10-24 17:52:07 +04:00
|
|
|
class PasswordChange(GenericAPIView):
|
2014-04-30 23:52:05 +04:00
|
|
|
|
|
|
|
"""
|
|
|
|
Calls Django Auth SetPasswordForm save method.
|
|
|
|
|
|
|
|
Accepts the following POST parameters: new_password1, new_password2
|
|
|
|
Returns the success/fail message.
|
|
|
|
"""
|
|
|
|
|
2014-10-07 17:08:08 +04:00
|
|
|
serializer_class = PasswordChangeSerializer
|
2014-10-24 17:52:07 +04:00
|
|
|
permission_classes = (IsAuthenticated,)
|
2014-04-30 23:52:05 +04:00
|
|
|
|
|
|
|
def post(self, request):
|
2014-10-07 17:08:08 +04:00
|
|
|
serializer = self.get_serializer(data=request.DATA)
|
|
|
|
if not serializer.is_valid():
|
2015-04-28 11:22:08 +03:00
|
|
|
return Response(
|
|
|
|
serializer.errors, status=status.HTTP_400_BAD_REQUEST
|
|
|
|
)
|
2014-10-07 17:08:08 +04:00
|
|
|
serializer.save()
|
|
|
|
return Response({"success": "New password has been saved."})
|