ft_transcendence/accounts/views/login.py

31 lines
1.1 KiB
Python
Raw Normal View History

from django.shortcuts import render
from django.views import View
from django.http import HttpResponse
from django.contrib.auth.models import User
from django.db.models.query import QuerySet
from ..status_code import *
from ..settings import *
2023-10-25 10:10:23 -04:00
from ..forms.login import LoginForm
2023-10-25 10:10:23 -04:00
class LoginView(View):
def get(self, request):
2023-10-25 10:10:23 -04:00
return render(request, "login.html", {"form": LoginForm})
def post(self, request):
2023-10-25 10:10:23 -04:00
form: LoginForm = LoginForm(request.POST)
if not form.is_valid():
return HttpResponse(INVALID_USERNAME_PASSWORD)
2023-10-25 10:10:23 -04:00
username: str = form.cleaned_data["username"]
password: str = form.cleaned_data["password"]
query: QuerySet = User.objects.filter(username=username)
if (not query.exists()):
return HttpResponse(INVALID_USERNAME_PASSWORD)
user: User = User.objects.get(username=username)
if (not user.check_password(password)):
return HttpResponse(INVALID_USERNAME_PASSWORD)
return HttpResponse(USER_VALID)