107 lines
3.6 KiB
Python
107 lines
3.6 KiB
Python
from django.shortcuts import render
|
|
from django.views import View
|
|
|
|
# Create your views here.
|
|
from django.http import HttpResponse
|
|
from django.contrib.auth.models import User
|
|
from django.db.models.query import QuerySet
|
|
|
|
from .status_code 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)
|
|
|
|
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):
|
|
return HttpResponse(INVALID_PASSWORD)
|
|
username = request.POST.get("username")
|
|
if (username == None or len(username) < 3):
|
|
return HttpResponse(INVALID_USERNAME)
|
|
|
|
if User.objects.filter(username=username).exists():
|
|
return HttpResponse(USERNAME_ALREADY_USED)
|
|
|
|
user = User.objects.create_user(username, password=password)
|
|
user.save()
|
|
|
|
return HttpResponse(USER_ADDED)
|
|
|
|
class Delete(View):
|
|
def get(self, request):
|
|
return render(request, "delete.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)
|
|
|
|
user.delete()
|
|
|
|
return HttpResponse(USER_DELETED)
|
|
|
|
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):
|
|
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
|
|
|
current_password = request.POST.get("current_password")
|
|
if (current_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(current_password)):
|
|
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
|
|
|
new_password = request.POST.get("new_password")
|
|
if (new_password == None or len(new_password) < 3):
|
|
return HttpResponse(INVALID_PASSWORD)
|
|
|
|
user.set_password(new_password)
|
|
user.save()
|
|
|
|
return HttpResponse(PASSWORD_UPDATED) |