diff --git a/pkpdapp/pkpdapp/api/views/login.py b/pkpdapp/pkpdapp/api/views/login.py index 8ac15309..a7960143 100644 --- a/pkpdapp/pkpdapp/api/views/login.py +++ b/pkpdapp/pkpdapp/api/views/login.py @@ -10,19 +10,16 @@ from django.middleware.csrf import get_token from django.views.decorators.csrf import ensure_csrf_cookie from django.views.decorators.http import require_POST -from rest_framework.authentication import ( - SessionAuthentication, BasicAuthentication -) +from rest_framework.authentication import SessionAuthentication, BasicAuthentication from rest_framework.permissions import IsAuthenticated from rest_framework.views import APIView from pkpdapp.api.serializers import UserSerializer def get_csrf(request): - response = JsonResponse({ - 'X-CSRFToken': get_token(request), - 'detail': 'CSRF cookie set' - }) + response = JsonResponse( + {"X-CSRFToken": get_token(request), "detail": "CSRF cookie set"} + ) return response @@ -30,32 +27,36 @@ def get_csrf(request): @require_POST def login_view(request): data = json.loads(request.body) - username = data.get('username') - password = data.get('password') + username = data.get("username") + password = data.get("password") if username is None or password is None: - return JsonResponse({ - 'detail': 'Please provide username and password.' - }, status=400) + return JsonResponse( + {"detail": "Please provide username and password."}, status=400 + ) user = authenticate(username=username, password=password) if user is None: - return JsonResponse({'detail': 'Invalid credentials. Either you have supplied an incorrect username/password combination, or you do not have sufficient access'}, status=400) + return JsonResponse( + { + "detail": "Invalid credentials. Either you have supplied an incorrect username/password combination, or you do not have sufficient access" # noqa E501 + }, + status=400, + ) login(request, user) - return JsonResponse({ - 'user': UserSerializer(user).data, - 'detail': 'Successfully logged in.' - }) + return JsonResponse( + {"user": UserSerializer(user).data, "detail": "Successfully logged in."} + ) @ensure_csrf_cookie def logout_view(request): if not request.user.is_authenticated: - return JsonResponse({'detail': 'You\'re not logged in.'}, status=400) + return JsonResponse({"detail": "You're not logged in."}, status=400) logout(request) - return JsonResponse({'detail': 'Successfully logged out.'}) + return JsonResponse({"detail": "Successfully logged out."}) class SessionView(APIView): @@ -64,8 +65,9 @@ class SessionView(APIView): @staticmethod def get(request, format=None): - return JsonResponse({'isAuthenticated': True, 'user': - UserSerializer(request.user).data}) + return JsonResponse( + {"isAuthenticated": True, "user": UserSerializer(request.user).data} + ) class WhoAmIView(APIView): @@ -74,4 +76,4 @@ class WhoAmIView(APIView): @staticmethod def get(request, format=None): - return JsonResponse({'user': request.user}) + return JsonResponse({"user": request.user})