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 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("error: username or password invalid") password = request.POST.get("password") if (password == None): return HttpResponse("error: username or password invalid") query: QuerySet = User.objects.filter(username=username) if (not query.exists()): return HttpResponse("error: username or password invalid") user: User = User.objects.get(username=username) if (not user.check_password(password)): return HttpResponse("error: username or password invalid") return HttpResponse("ok: account 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("error: password invalid") username = request.POST.get("username") if (username == None or len(username) < 3): return HttpResponse("error: username invalid") if User.objects.filter(username=username).exists(): return HttpResponse("error: username already used") user = User.objects.create_user(username, password=password) user.save() return HttpResponse("ok: 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("error: username or password invalid") password = request.POST.get("password") if (password == None): return HttpResponse("error: username or password invalid") query: QuerySet = User.objects.filter(username=username) if (not query.exists()): return HttpResponse("error: username or password invalid") user: User = User.objects.get(username=username) if (not user.check_password(password)): return HttpResponse("error: username or password invalid") user.delete() return HttpResponse("ok: account has been 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("error: username or password invalid") current_password = request.POST.get("current_password") if (current_password == None): return HttpResponse("error: username or password invalid") query: QuerySet = User.objects.filter(username=username) if (not query.exists()): return HttpResponse("error: username or password invalid") user: User = User.objects.get(username=username) if (not user.check_password(current_password)): return HttpResponse("error: username or password invalid") new_password = request.POST.get("new_password") if (new_password == None or len(new_password) < 3): return HttpResponse("error: new password too short") user.set_password(new_password) user.save() return HttpResponse("ok: password has been updated")