Compare commits

..

2 Commits

Author SHA1 Message Date
2c7bbb5a5f core: accounts: use django form 2023-10-25 16:10:23 +02:00
4cda3de144 core: accounts: separate views i multiple files 2023-10-25 15:35:24 +02:00
14 changed files with 174 additions and 129 deletions

View File

@ -0,0 +1,7 @@
from django import forms
from ..settings import *
class ChangePasswordForm(forms.Form):
username = forms.CharField(label="username", max_length=USERNAME_MAX_SIZE, min_length=USERNAME_MIN_SIZE, required=True)
current_password = forms.CharField(label="current_password", max_length=PASSWORD_MAX_SIZE, min_length=PASSWORD_MIN_SIZE, required=True)
new_password = forms.CharField(label="new_password", max_length=PASSWORD_MAX_SIZE, min_length=PASSWORD_MIN_SIZE, required=True)

View File

@ -0,0 +1,6 @@
from django import forms
from ..settings import *
class DeleteForm(forms.Form):
username = forms.CharField(label="username", max_length=USERNAME_MAX_SIZE, min_length=USERNAME_MIN_SIZE, required=True)
password = forms.CharField(label="password", max_length=PASSWORD_MAX_SIZE, min_length=PASSWORD_MIN_SIZE, required=True)

View File

@ -0,0 +1,6 @@
from django import forms
from ..settings import *
class LoginForm(forms.Form):
username = forms.CharField(label="username", max_length=USERNAME_MAX_SIZE, min_length=USERNAME_MIN_SIZE, required=True)
password = forms.CharField(label="password", max_length=PASSWORD_MAX_SIZE, min_length=PASSWORD_MIN_SIZE, required=True)

View File

@ -0,0 +1,6 @@
from django import forms
from ..settings import *
class RegisterForm(forms.Form):
username = forms.CharField(label="username", max_length=USERNAME_MAX_SIZE, min_length=USERNAME_MIN_SIZE, required=True)
password = forms.CharField(label="password", max_length=PASSWORD_MAX_SIZE, min_length=PASSWORD_MIN_SIZE, required=True)

View File

@ -1,8 +1,7 @@
<html> <html>
<form method='post'> <form method='post'>
{% csrf_token %} {% csrf_token %}
<input type="text" name="username" placeholder="username"> {{ form }}
<input type="text" name="password" placeholder="password">
<input type='submit'> <input type='submit'>
</form> </form>
</html> </html>

View File

@ -1,8 +1,7 @@
<html> <html>
<form method='post'> <form method='post'>
{% csrf_token %} {% csrf_token %}
<input type="text" name="username" placeholder="username"> {{ form }}
<input type="text" name="password" placeholder="password">
<input type='submit'> <input type='submit'>
</form> </form>
</html> </html>

View File

@ -1,8 +1,7 @@
<html> <html>
<form method='post'> <form method='post'>
{% csrf_token %} {% csrf_token %}
<input type="text" name="username" placeholder="username"> {{ form }}
<input type="text" name="password" placeholder="password">
<input type='submit'> <input type='submit'>
</form> </form>
</html> </html>

View File

@ -20,12 +20,12 @@ class RegisterTest(TestCase):
def test_incomplet_form_no_username_no_password(self): def test_incomplet_form_no_username_no_password(self):
response: HttpResponse = self.client.post(self.url) response: HttpResponse = self.client.post(self.url)
response_text: str = response.content.decode("utf-8") response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, INVALID_PASSWORD) self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
def test_incomplet_form_no_password(self): def test_incomplet_form_no_password(self):
response: HttpResponse = self.client.post(self.url, {"username": self.username}) response: HttpResponse = self.client.post(self.url, {"username": self.username})
response_text: str = response.content.decode("utf-8") response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, INVALID_PASSWORD) self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
def test_incomplet_form_no_username(self): def test_incomplet_form_no_username(self):
response: HttpResponse = self.client.post(self.url, {"password": self.password}) response: HttpResponse = self.client.post(self.url, {"password": self.password})
@ -35,7 +35,7 @@ class RegisterTest(TestCase):
def test_incomplet_form_no_username(self): def test_incomplet_form_no_username(self):
response: HttpResponse = self.client.post(self.url, {"username": self.username}) response: HttpResponse = self.client.post(self.url, {"username": self.username})
response_text: str = response.content.decode("utf-8") response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, INVALID_PASSWORD) self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
def test_normal_register(self): def test_normal_register(self):
response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": self.password}) response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": self.password})
@ -45,22 +45,22 @@ class RegisterTest(TestCase):
def test_username_too_short(self): def test_username_too_short(self):
response: HttpResponse = self.client.post(self.url, {"username": "a" * (USERNAME_MIN_SIZE - (USERNAME_MIN_SIZE > 0)), "password": self.password}) response: HttpResponse = self.client.post(self.url, {"username": "a" * (USERNAME_MIN_SIZE - (USERNAME_MIN_SIZE > 0)), "password": self.password})
response_text: str = response.content.decode("utf-8") response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, INVALID_USERNAME) self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
def test_username_too_long(self): def test_username_too_long(self):
response: HttpResponse = self.client.post(self.url, {"username": "a" * (USERNAME_MAX_SIZE + 1), "password": self.password}) response: HttpResponse = self.client.post(self.url, {"username": "a" * (USERNAME_MAX_SIZE + 1), "password": self.password})
response_text: str = response.content.decode("utf-8") response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, INVALID_USERNAME) self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
def test_password_too_short(self): def test_password_too_short(self):
response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": "a" * (PASSWORD_MIN_SIZE - (PASSWORD_MIN_SIZE > 0))}) response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": "a" * (PASSWORD_MIN_SIZE - (PASSWORD_MIN_SIZE > 0))})
response_text: str = response.content.decode("utf-8") response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, INVALID_PASSWORD) self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
def test_password_too_long(self): def test_password_too_long(self):
response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": "a" * (PASSWORD_MAX_SIZE + 1)}) response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": "a" * (PASSWORD_MAX_SIZE + 1)})
response_text: str = response.content.decode("utf-8") response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, INVALID_PASSWORD) self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
def test_already_registered(self): def test_already_registered(self):
self.client.post(self.url, {"username": self.username, "password": self.password}) self.client.post(self.url, {"username": self.username, "password": self.password})
@ -246,17 +246,17 @@ class ChangePasswordTest(TestCase):
def test_no_new_password(self): def test_no_new_password(self):
response: HttpResponse = self.client.post(self.url, {"username": self.username, "current_password": self.password}) response: HttpResponse = self.client.post(self.url, {"username": self.username, "current_password": self.password})
response_text: str = response.content.decode("utf-8") response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, INVALID_PASSWORD) self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
def test_new_password_to_short(self): def test_new_password_to_short(self):
response: HttpResponse = self.client.post(self.url, {"username": self.username, "current_password": self.password, "new_password": "a" * (PASSWORD_MIN_SIZE - (PASSWORD_MIN_SIZE > 0))}) response: HttpResponse = self.client.post(self.url, {"username": self.username, "current_password": self.password, "new_password": "a" * (PASSWORD_MIN_SIZE - (PASSWORD_MIN_SIZE > 0))})
response_text: str = response.content.decode("utf-8") response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, INVALID_PASSWORD) self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
def test_new_password_to_long(self): def test_new_password_to_long(self):
response: HttpResponse = self.client.post(self.url, {"username": self.username, "current_password": self.password, "new_password": "a" * (PASSWORD_MAX_SIZE + 1)}) response: HttpResponse = self.client.post(self.url, {"username": self.username, "current_password": self.password, "new_password": "a" * (PASSWORD_MAX_SIZE + 1)})
response_text: str = response.content.decode("utf-8") response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, INVALID_PASSWORD) self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
def test_normal_change_password(self): def test_normal_change_password(self):
response: HttpResponse = self.client.post(self.url, {"username": self.username, "current_password": self.password, "new_password": self.new_password}) response: HttpResponse = self.client.post(self.url, {"username": self.username, "current_password": self.password, "new_password": self.new_password})

View File

@ -1,10 +1,10 @@
from django.urls import path from django.urls import path
from . import views from .views import login, register, delete, change_password
urlpatterns = [ urlpatterns = [
path("login", views.Login.as_view(), name="login"), path("login", login.LoginView.as_view(), name="login"),
path("register", views.Register.as_view(), name="register"), path("register", register.RegisterView.as_view(), name="register"),
path("delete", views.Delete.as_view(), name="delete"), path("delete", delete.DeleteView.as_view(), name="delete"),
path("change_password", views.ChangePassword.as_view(), name="delete"), path("change_password", change_password.ChangePasswordView.as_view(), name="change_password"),
] ]

View File

@ -1,108 +0,0 @@
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 *
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)
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 not PASSWORD_MAX_SIZE >= len(password) >= PASSWORD_MIN_SIZE):
return HttpResponse(INVALID_PASSWORD)
username = request.POST.get("username")
if (username == None or not USERNAME_MAX_SIZE >= len(username) >= USERNAME_MIN_SIZE):
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 not PASSWORD_MAX_SIZE >= len(new_password) >= PASSWORD_MIN_SIZE):
return HttpResponse(INVALID_PASSWORD)
user.set_password(new_password)
user.save()
return HttpResponse(PASSWORD_UPDATED)

View File

@ -0,0 +1,36 @@
from django.shortcuts import render
from django.views import View
from django.http import HttpResponse, HttpRequest
from django.contrib.auth.models import User
from django.db.models.query import QuerySet
from ..status_code import *
from ..settings import *
from ..forms.change_password import ChangePasswordForm
class ChangePasswordView(View):
def get(self, request: HttpRequest):
return render(request, "change_password.html")
def post(self, request: HttpRequest):
form: ChangePasswordForm = ChangePasswordForm(request.POST)
if not form.is_valid():
return HttpResponse(INVALID_USERNAME_PASSWORD)
username: str = form.cleaned_data['username']
current_password: str = form.cleaned_data['current_password']
new_password: str = form.cleaned_data['new_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)
user.set_password(new_password)
user.save()
return HttpResponse(PASSWORD_UPDATED)

View File

@ -0,0 +1,35 @@
from django.shortcuts import render
from django.views import View
from django.http import HttpResponse, HttpRequest
from django.contrib.auth.models import User
from django.db.models.query import QuerySet
from ..status_code import *
from ..settings import *
from ..forms.delete import DeleteForm
class DeleteView(View):
def get(self, request: HttpRequest):
return render(request, "delete.html", {"form": DeleteForm})
def post(self, request: HttpRequest):
form: DeleteForm = DeleteForm(request.POST)
if (not form.is_valid()):
return HttpResponse(INVALID_USERNAME_PASSWORD)
username: str = form.cleaned_data['username']
password: str = form.cleaned_data['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)

View File

@ -0,0 +1,31 @@
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 *
from ..forms.login import LoginForm
class LoginView(View):
def get(self, request):
return render(request, "login.html", {"form": LoginForm})
def post(self, request):
form: LoginForm = LoginForm(request.POST)
if not form.is_valid():
return HttpResponse(INVALID_USERNAME_PASSWORD)
username: str = form.cleaned_data["username"]
password: str = form.cleaned_data["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)

View File

@ -0,0 +1,29 @@
from django.shortcuts import render
from django.views import View
from django.http import HttpResponse, HttpRequest
from django.contrib.auth.models import User
from django.db.models.query import QuerySet
from ..status_code import *
from ..settings import *
from ..forms.register import RegisterForm
class RegisterView(View):
def get(self, request: HttpRequest):
return render(request, "register.html", {"form": RegisterForm})
def post(self, request: HttpRequest):
form: RegisterForm = RegisterForm(request.POST)
if not form.is_valid():
return HttpResponse(INVALID_USERNAME_PASSWORD)
username: str = form.cleaned_data["username"]
password: str = form.cleaned_data["password"]
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)