2023-10-24 07:36:42 -04:00
|
|
|
from django.shortcuts import render
|
2023-10-24 07:56:16 -04:00
|
|
|
from django.views import View
|
2023-10-24 07:36:42 -04:00
|
|
|
|
|
|
|
# Create your views here.
|
2023-10-24 10:31:26 -04:00
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.contrib.auth.models import User
|
2023-10-24 11:06:43 -04:00
|
|
|
from django.db.models.query import QuerySet
|
2023-10-24 10:31:26 -04:00
|
|
|
|
2023-10-24 16:33:13 -04:00
|
|
|
from .status_code import *
|
|
|
|
|
2023-10-24 07:56:16 -04:00
|
|
|
class Login(View):
|
2023-10-24 11:06:43 -04:00
|
|
|
def get(self, request):
|
|
|
|
return render(request, "login.html")
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
username = request.POST.get("username")
|
|
|
|
if (username == None):
|
2023-10-24 16:33:13 -04:00
|
|
|
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
2023-10-24 11:06:43 -04:00
|
|
|
|
|
|
|
password = request.POST.get("password")
|
|
|
|
if (password == None):
|
2023-10-24 16:33:13 -04:00
|
|
|
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
2023-10-24 11:06:43 -04:00
|
|
|
|
|
|
|
query: QuerySet = User.objects.filter(username=username)
|
|
|
|
if (not query.exists()):
|
2023-10-24 16:33:13 -04:00
|
|
|
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
2023-10-24 11:06:43 -04:00
|
|
|
|
|
|
|
user: User = User.objects.get(username=username)
|
|
|
|
if (not user.check_password(password)):
|
2023-10-24 16:33:13 -04:00
|
|
|
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
2023-10-24 11:06:43 -04:00
|
|
|
|
2023-10-24 16:33:13 -04:00
|
|
|
return HttpResponse(USER_VALID)
|
2023-10-24 07:56:16 -04:00
|
|
|
|
2023-10-24 10:31:26 -04:00
|
|
|
class Register(View):
|
|
|
|
def get(self, request):
|
|
|
|
return render(request, "register.html")
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
password = request.POST.get("password")
|
|
|
|
if (password == None or len(password) < 3):
|
2023-10-24 16:33:13 -04:00
|
|
|
return HttpResponse(INVALID_PASSWORD)
|
2023-10-24 10:31:26 -04:00
|
|
|
username = request.POST.get("username")
|
|
|
|
if (username == None or len(username) < 3):
|
2023-10-24 16:33:13 -04:00
|
|
|
return HttpResponse(INVALID_USERNAME)
|
2023-10-24 10:31:26 -04:00
|
|
|
|
|
|
|
if User.objects.filter(username=username).exists():
|
2023-10-24 16:33:13 -04:00
|
|
|
return HttpResponse(USERNAME_ALREADY_USED)
|
2023-10-24 10:31:26 -04:00
|
|
|
|
|
|
|
user = User.objects.create_user(username, password=password)
|
|
|
|
user.save()
|
|
|
|
|
2023-10-24 16:33:13 -04:00
|
|
|
return HttpResponse(USER_ADDED)
|
2023-10-24 07:56:16 -04:00
|
|
|
|
|
|
|
class Delete(View):
|
2023-10-24 11:10:32 -04:00
|
|
|
def get(self, request):
|
|
|
|
return render(request, "delete.html")
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
username = request.POST.get("username")
|
|
|
|
if (username == None):
|
2023-10-24 16:33:13 -04:00
|
|
|
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
2023-10-24 11:10:32 -04:00
|
|
|
|
|
|
|
password = request.POST.get("password")
|
|
|
|
if (password == None):
|
2023-10-24 16:33:13 -04:00
|
|
|
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
2023-10-24 11:10:32 -04:00
|
|
|
|
|
|
|
query: QuerySet = User.objects.filter(username=username)
|
|
|
|
if (not query.exists()):
|
2023-10-24 16:33:13 -04:00
|
|
|
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
2023-10-24 11:10:32 -04:00
|
|
|
|
|
|
|
user: User = User.objects.get(username=username)
|
|
|
|
if (not user.check_password(password)):
|
2023-10-24 16:33:13 -04:00
|
|
|
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
2023-10-24 11:10:32 -04:00
|
|
|
|
|
|
|
user.delete()
|
|
|
|
|
2023-10-24 16:33:13 -04:00
|
|
|
return HttpResponse(USER_DELETED)
|
2023-10-24 11:20:16 -04:00
|
|
|
|
|
|
|
class ChangePassword(View):
|
|
|
|
def get(self, request):
|
|
|
|
return render(request, "change_password.html")
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
username = request.POST.get("username")
|
|
|
|
if (username == None):
|
2023-10-24 16:33:13 -04:00
|
|
|
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
2023-10-24 11:20:16 -04:00
|
|
|
|
|
|
|
current_password = request.POST.get("current_password")
|
|
|
|
if (current_password == None):
|
2023-10-24 16:33:13 -04:00
|
|
|
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
2023-10-24 11:20:16 -04:00
|
|
|
|
|
|
|
query: QuerySet = User.objects.filter(username=username)
|
|
|
|
if (not query.exists()):
|
2023-10-24 16:33:13 -04:00
|
|
|
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
2023-10-24 11:20:16 -04:00
|
|
|
|
|
|
|
user: User = User.objects.get(username=username)
|
|
|
|
if (not user.check_password(current_password)):
|
2023-10-24 16:33:13 -04:00
|
|
|
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
2023-10-24 11:20:16 -04:00
|
|
|
|
|
|
|
new_password = request.POST.get("new_password")
|
|
|
|
if (new_password == None or len(new_password) < 3):
|
2023-10-24 16:33:13 -04:00
|
|
|
return HttpResponse(INVALID_PASSWORD)
|
2023-10-24 11:20:16 -04:00
|
|
|
|
|
|
|
user.set_password(new_password)
|
|
|
|
user.save()
|
|
|
|
|
2023-10-24 16:33:13 -04:00
|
|
|
return HttpResponse(PASSWORD_UPDATED)
|