diff --git a/django/trancendence/accounts/forms/change_password.py b/django/trancendence/accounts/forms/change_password.py new file mode 100644 index 0000000..35705b0 --- /dev/null +++ b/django/trancendence/accounts/forms/change_password.py @@ -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) \ No newline at end of file diff --git a/django/trancendence/accounts/forms/delete.py b/django/trancendence/accounts/forms/delete.py new file mode 100644 index 0000000..2ebe431 --- /dev/null +++ b/django/trancendence/accounts/forms/delete.py @@ -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) \ No newline at end of file diff --git a/django/trancendence/accounts/forms/login.py b/django/trancendence/accounts/forms/login.py new file mode 100644 index 0000000..21f8568 --- /dev/null +++ b/django/trancendence/accounts/forms/login.py @@ -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) \ No newline at end of file diff --git a/django/trancendence/accounts/forms/register.py b/django/trancendence/accounts/forms/register.py new file mode 100644 index 0000000..16e9fff --- /dev/null +++ b/django/trancendence/accounts/forms/register.py @@ -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) \ No newline at end of file diff --git a/django/trancendence/accounts/templates/delete.html b/django/trancendence/accounts/templates/delete.html index 2d0a7d9..6445c3e 100644 --- a/django/trancendence/accounts/templates/delete.html +++ b/django/trancendence/accounts/templates/delete.html @@ -1,8 +1,7 @@
{% csrf_token %} - - + {{ form }}
\ No newline at end of file diff --git a/django/trancendence/accounts/templates/login.html b/django/trancendence/accounts/templates/login.html index 2d0a7d9..6445c3e 100644 --- a/django/trancendence/accounts/templates/login.html +++ b/django/trancendence/accounts/templates/login.html @@ -1,8 +1,7 @@
{% csrf_token %} - - + {{ form }}
\ No newline at end of file diff --git a/django/trancendence/accounts/templates/register.html b/django/trancendence/accounts/templates/register.html index 2d0a7d9..6445c3e 100644 --- a/django/trancendence/accounts/templates/register.html +++ b/django/trancendence/accounts/templates/register.html @@ -1,8 +1,7 @@
{% csrf_token %} - - + {{ form }}
\ No newline at end of file diff --git a/django/trancendence/accounts/tests.py b/django/trancendence/accounts/tests.py index 80f2630..e0bfaf1 100644 --- a/django/trancendence/accounts/tests.py +++ b/django/trancendence/accounts/tests.py @@ -20,12 +20,12 @@ class RegisterTest(TestCase): def test_incomplet_form_no_username_no_password(self): response: HttpResponse = self.client.post(self.url) 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): response: HttpResponse = self.client.post(self.url, {"username": self.username}) 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): response: HttpResponse = self.client.post(self.url, {"password": self.password}) @@ -35,7 +35,7 @@ class RegisterTest(TestCase): def test_incomplet_form_no_username(self): response: HttpResponse = self.client.post(self.url, {"username": self.username}) 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): 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): 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") - self.assertEqual(response_text, INVALID_USERNAME) + self.assertEqual(response_text, INVALID_USERNAME_PASSWORD) def test_username_too_long(self): response: HttpResponse = self.client.post(self.url, {"username": "a" * (USERNAME_MAX_SIZE + 1), "password": self.password}) 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): 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") - self.assertEqual(response_text, INVALID_PASSWORD) + self.assertEqual(response_text, INVALID_USERNAME_PASSWORD) def test_password_too_long(self): response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": "a" * (PASSWORD_MAX_SIZE + 1)}) 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): self.client.post(self.url, {"username": self.username, "password": self.password}) @@ -246,17 +246,17 @@ class ChangePasswordTest(TestCase): def test_no_new_password(self): response: HttpResponse = self.client.post(self.url, {"username": self.username, "current_password": self.password}) 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): 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") - self.assertEqual(response_text, INVALID_PASSWORD) + self.assertEqual(response_text, INVALID_USERNAME_PASSWORD) 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_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): response: HttpResponse = self.client.post(self.url, {"username": self.username, "current_password": self.password, "new_password": self.new_password}) diff --git a/django/trancendence/accounts/urls.py b/django/trancendence/accounts/urls.py index a551d6e..ffbca49 100644 --- a/django/trancendence/accounts/urls.py +++ b/django/trancendence/accounts/urls.py @@ -3,8 +3,8 @@ from django.urls import path from .views import login, register, delete, change_password urlpatterns = [ - path("login", login.Login.as_view(), name="login"), - path("register", register.Register.as_view(), name="register"), - path("delete", delete.Delete.as_view(), name="delete"), - path("change_password", change_password.ChangePassword.as_view(), name="change_password"), + path("login", login.LoginView.as_view(), name="login"), + path("register", register.RegisterView.as_view(), name="register"), + path("delete", delete.DeleteView.as_view(), name="delete"), + path("change_password", change_password.ChangePasswordView.as_view(), name="change_password"), ] \ No newline at end of file diff --git a/django/trancendence/accounts/views/change_password.py b/django/trancendence/accounts/views/change_password.py index 778839a..a0e4031 100644 --- a/django/trancendence/accounts/views/change_password.py +++ b/django/trancendence/accounts/views/change_password.py @@ -1,24 +1,26 @@ from django.shortcuts import render from django.views import View -from django.http import HttpResponse +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 ChangePassword(View): - def get(self, request): +class ChangePasswordView(View): + def get(self, request: HttpRequest): return render(request, "change_password.html") - def post(self, request): - username = request.POST.get("username") - if (username == None): + def post(self, request: HttpRequest): + + form: ChangePasswordForm = ChangePasswordForm(request.POST) + if not form.is_valid(): return HttpResponse(INVALID_USERNAME_PASSWORD) - current_password = request.POST.get("current_password") - if (current_password == None): - 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()): @@ -28,10 +30,6 @@ class ChangePassword(View): 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() diff --git a/django/trancendence/accounts/views/delete.py b/django/trancendence/accounts/views/delete.py index 15409b2..a9d5f5f 100644 --- a/django/trancendence/accounts/views/delete.py +++ b/django/trancendence/accounts/views/delete.py @@ -1,24 +1,25 @@ from django.shortcuts import render from django.views import View -from django.http import HttpResponse +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 Delete(View): - def get(self, request): - return render(request, "delete.html") +class DeleteView(View): + def get(self, request: HttpRequest): + return render(request, "delete.html", {"form": DeleteForm}) - def post(self, request): - username = request.POST.get("username") - if (username == None): + def post(self, request: HttpRequest): + + form: DeleteForm = DeleteForm(request.POST) + if (not form.is_valid()): return HttpResponse(INVALID_USERNAME_PASSWORD) - password = request.POST.get("password") - if (password == None): - 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()): diff --git a/django/trancendence/accounts/views/login.py b/django/trancendence/accounts/views/login.py index 3962ce5..27c9f0d 100644 --- a/django/trancendence/accounts/views/login.py +++ b/django/trancendence/accounts/views/login.py @@ -4,22 +4,21 @@ 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 Login(View): +class LoginView(View): def get(self, request): - return render(request, "login.html") + return render(request, "login.html", {"form": LoginForm}) def post(self, request): - username = request.POST.get("username") - if (username == None): + form: LoginForm = LoginForm(request.POST) + if not form.is_valid(): return HttpResponse(INVALID_USERNAME_PASSWORD) - password = request.POST.get("password") - if (password == None): - 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()): diff --git a/django/trancendence/accounts/views/register.py b/django/trancendence/accounts/views/register.py index 5b64396..d0e5361 100644 --- a/django/trancendence/accounts/views/register.py +++ b/django/trancendence/accounts/views/register.py @@ -1,23 +1,24 @@ from django.shortcuts import render from django.views import View -from django.http import HttpResponse +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 Register(View): - def get(self, request): - return render(request, "register.html") +class RegisterView(View): + def get(self, request: HttpRequest): + return render(request, "register.html", {"form": RegisterForm}) - 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) + 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)