core: accounts: use django form
This commit is contained in:
parent
4cda3de144
commit
2c7bbb5a5f
7
django/trancendence/accounts/forms/change_password.py
Normal file
7
django/trancendence/accounts/forms/change_password.py
Normal 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)
|
6
django/trancendence/accounts/forms/delete.py
Normal file
6
django/trancendence/accounts/forms/delete.py
Normal 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)
|
6
django/trancendence/accounts/forms/login.py
Normal file
6
django/trancendence/accounts/forms/login.py
Normal 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)
|
6
django/trancendence/accounts/forms/register.py
Normal file
6
django/trancendence/accounts/forms/register.py
Normal 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)
|
@ -1,8 +1,7 @@
|
||||
<html>
|
||||
<form method='post'>
|
||||
{% csrf_token %}
|
||||
<input type="text" name="username" placeholder="username">
|
||||
<input type="text" name="password" placeholder="password">
|
||||
{{ form }}
|
||||
<input type='submit'>
|
||||
</form>
|
||||
</html>
|
@ -1,8 +1,7 @@
|
||||
<html>
|
||||
<form method='post'>
|
||||
{% csrf_token %}
|
||||
<input type="text" name="username" placeholder="username">
|
||||
<input type="text" name="password" placeholder="password">
|
||||
{{ form }}
|
||||
<input type='submit'>
|
||||
</form>
|
||||
</html>
|
@ -1,8 +1,7 @@
|
||||
<html>
|
||||
<form method='post'>
|
||||
{% csrf_token %}
|
||||
<input type="text" name="username" placeholder="username">
|
||||
<input type="text" name="password" placeholder="password">
|
||||
{{ form }}
|
||||
<input type='submit'>
|
||||
</form>
|
||||
</html>
|
@ -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})
|
||||
|
@ -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"),
|
||||
]
|
@ -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()
|
||||
|
||||
|
@ -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()):
|
||||
|
@ -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()):
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user