2023-10-25 09:35:24 -04:00
|
|
|
from django.shortcuts import render
|
|
|
|
from django.views import View
|
2023-10-31 16:14:24 -04:00
|
|
|
from django.http import HttpResponse, HttpRequest, JsonResponse
|
2023-10-25 09:35:24 -04:00
|
|
|
from django.contrib.auth.models import User
|
2023-11-03 16:33:48 -04:00
|
|
|
from django.contrib.auth import authenticate, login, logout
|
2023-10-31 16:14:24 -04:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2023-10-25 09:35:24 -04:00
|
|
|
from django.db.models.query import QuerySet
|
|
|
|
|
|
|
|
from ..status_code import *
|
2023-10-25 10:10:23 -04:00
|
|
|
from ..forms.login import LoginForm
|
2023-10-25 09:35:24 -04:00
|
|
|
|
2023-10-25 10:10:23 -04:00
|
|
|
class LoginView(View):
|
2023-10-31 16:14:24 -04:00
|
|
|
|
|
|
|
def get(self, request: HttpRequest):
|
|
|
|
if request.user.is_authenticated:
|
|
|
|
logout(request)
|
2023-10-25 10:10:23 -04:00
|
|
|
return render(request, "login.html", {"form": LoginForm})
|
2023-10-25 09:35:24 -04:00
|
|
|
|
2023-10-31 16:14:24 -04:00
|
|
|
def post(self, request: HttpRequest):
|
|
|
|
if request.user.is_authenticated:
|
|
|
|
logout(request)
|
2023-10-25 10:10:23 -04:00
|
|
|
form: LoginForm = LoginForm(request.POST)
|
|
|
|
if not form.is_valid():
|
2023-10-31 16:14:24 -04:00
|
|
|
return JsonResponse(form.errors)
|
2023-10-25 09:35:24 -04:00
|
|
|
|
2023-10-31 16:14:24 -04:00
|
|
|
user: User = authenticate(username=form.cleaned_data['username'], password=form.cleaned_data['password'])
|
|
|
|
if user is None:
|
|
|
|
return JsonResponse({'user': [USER_INVALID]})
|
2023-10-25 09:35:24 -04:00
|
|
|
|
2023-10-31 16:14:24 -04:00
|
|
|
login(request, user)
|
|
|
|
return HttpResponse(USER_LOGGED)
|