ft_transcendence/django/trancendence/accounts/views/login.py

32 lines
1.0 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 *
class Login(View):
def get(self, request):
return render(request, "login.html")
def post(self, request):
username = request.POST.get("username")
if (username == None):
return HttpResponse(INVALID_USERNAME_PASSWORD)
password = request.POST.get("password")
if (password == None):
return HttpResponse(INVALID_USERNAME_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)