Skip to content

Commit

Permalink
#327 flake8
Browse files Browse the repository at this point in the history
  • Loading branch information
martinjrobins committed Jan 10, 2024
1 parent ba15c97 commit 189ca74
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions pkpdapp/pkpdapp/api/views/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,52 +10,53 @@
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


@ensure_csrf_cookie
@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):
Expand All @@ -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):
Expand All @@ -74,4 +76,4 @@ class WhoAmIView(APIView):

@staticmethod
def get(request, format=None):
return JsonResponse({'user': request.user})
return JsonResponse({"user": request.user})

0 comments on commit 189ca74

Please sign in to comment.