register: use ModelForm, and print all errors

This commit is contained in:
starnakin 2023-10-31 21:14:24 +01:00
parent 54cc1b1705
commit a0c0d813b6
20 changed files with 203 additions and 258 deletions

View File

@ -1,7 +1,10 @@
from django import forms from django import forms
from ..settings import * from django.contrib.auth.models import User
from django.contrib.auth import authenticate
from ..status_code import *
class ChangePasswordForm(forms.Form): class ChangePasswordForm(forms.Form):
username = forms.CharField(label="username", max_length=USERNAME_MAX_SIZE, min_length=USERNAME_MIN_SIZE, required=True) new_password = forms.CharField(required=True, error_messages = {
current_password = forms.CharField(label="current_password", max_length=PASSWORD_MAX_SIZE, min_length=PASSWORD_MIN_SIZE, required=True) 'required': PASSWORD_MISSING,
new_password = forms.CharField(label="new_password", max_length=PASSWORD_MAX_SIZE, min_length=PASSWORD_MIN_SIZE, required=True) })

View File

@ -1,6 +0,0 @@
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

@ -1,6 +1,15 @@
from django import forms from django import forms
from ..settings import * from django.contrib.auth.models import User
from django.contrib.auth import authenticate
from django.db.models.query import QuerySet
from django.core.exceptions import ValidationError
from ..status_code import *
class LoginForm(forms.Form): class LoginForm(forms.Form):
username = forms.CharField(label="username", max_length=USERNAME_MAX_SIZE, min_length=USERNAME_MIN_SIZE, required=True) username = forms.CharField(required=True, error_messages={
password = forms.CharField(label="password", max_length=PASSWORD_MAX_SIZE, min_length=PASSWORD_MIN_SIZE, required=True) 'required': USERNAME_MISSING,
})
password = forms.CharField(required=True, error_messages = {
'required': PASSWORD_MISSING,
})

View File

@ -1,6 +1,22 @@
from django import forms from django.forms import ModelForm
from ..settings import * from django.contrib.auth.models import User
from ..status_code import *
class RegisterForm(forms.Form): class RegisterForm(ModelForm):
username = forms.CharField(label="username", max_length=USERNAME_MAX_SIZE, min_length=USERNAME_MIN_SIZE, required=True) class Meta:
password = forms.CharField(label="password", max_length=PASSWORD_MAX_SIZE, min_length=PASSWORD_MIN_SIZE, required=True) model = User
fields = ['username', 'password']
error_messages = {
'username': {
'max_length': USERNAME_TOO_LONG,
'min_length': USERNAME_TOO_SHORT,
'required': USERNAME_MISSING,
'unique': USERNAME_ALREADY_USED,
},
'password': {
'max_length': PASSWORD_TOO_LONG,
'min_length': PASSWORD_TOO_SHORT,
'required': PASSWORD_MISSING,
}
}

View File

@ -1,3 +0,0 @@
from django.db import models
# Create your models here.

View File

@ -1,4 +0,0 @@
PASSWORD_MIN_SIZE = 3
PASSWORD_MAX_SIZE = 128
USERNAME_MIN_SIZE = 3
USERNAME_MAX_SIZE = 40

View File

@ -1,9 +1,18 @@
INVALID_USERNAME: str = "error: username invalid" USERNAME_TOO_LONG: str = "error: username too long"
INVALID_PASSWORD: str = "error: password invalid" USERNAME_TOO_SHORT: str = "error: username too short"
INVALID_USERNAME_PASSWORD: str = "error: username or password invalid" USERNAME_MISSING: str = "error: username is missing"
PASSWORD_TOO_LONG: str = "error: password too long"
PASSWORD_TOO_SHORT: str = "error: password too short"
PASSWORD_MISSING: str = "error: password is missing"
USERNAME_ALREADY_USED: str = "error: username already used" USERNAME_ALREADY_USED: str = "error: username already used"
USER_ALREADY_LOGGED: str = "error: user already logged"
USER_INVALID: str = "error: username or password invalid"
USER_ADDED: str = "ok: user added" USER_ADDED: str = "ok: user added"
USER_DELETED: str = "ok: account has been deleted" USER_DELETED: str = "ok: account has been deleted"
USER_VALID: str = "ok: account valid" USER_LOGGED: str = "ok: account valid"
PASSWORD_UPDATED: str = "ok: password has been updated" USER_PASSWORD_UPDATED: str = "ok: password has been updated"
USER_LOGOUT: str = "ok: user logout"
METHOD_INVALID: str = "error: method invalid"

View File

@ -1,9 +1,7 @@
<html> <html>
<form method='post'> <form method='post'>
{% csrf_token %} {% csrf_token %}
<input type="text" name="username" placeholder="username"> {{form.as_p}}
<input type="text" name="current_password" placeholder="current_password">
<input type="text" name="new_password" placeholder="new_password">
<input type='submit'> <input type='submit'>
</form> </form>
</html> </html>

View File

@ -1,7 +1,7 @@
<html> <html>
<form method='post'> <form method='post'>
{% csrf_token %} {% csrf_token %}
{{ form }} {{ form.as_p }}
<input type='submit'> <input type='submit'>
</form> </form>
</html> </html>

View File

@ -1,7 +1,7 @@
<html> <html>
<form method='post'> <form method='post'>
{% csrf_token %} {% csrf_token %}
{{ form }} {{ form.as_p }}
<input type='submit'> <input type='submit'>
</form> </form>
</html> </html>

View File

@ -3,10 +3,11 @@ from django.test import TestCase
# Create your tests here. # Create your tests here.
from django.test.client import Client from django.test.client import Client
from django.http import HttpResponse from django.http import HttpResponse
from django.contrib.auth.models import User
import uuid import uuid
from ..status_code import * from ..status_code import *
from ..settings import *
class ChangePasswordTest(TestCase): class ChangePasswordTest(TestCase):
def setUp(self): def setUp(self):
@ -14,63 +15,19 @@ class ChangePasswordTest(TestCase):
self.url = "/api/accounts/change_password" self.url = "/api/accounts/change_password"
self.username: str = str(uuid.uuid4())[:USERNAME_MAX_SIZE] self.username: str = str(uuid.uuid4())
self.password: str = str(uuid.uuid4())[:PASSWORD_MAX_SIZE] self.password: str = str(uuid.uuid4())
self.new_password: str = str(uuid.uuid4())[:PASSWORD_MAX_SIZE] self.new_password: str = str(uuid.uuid4())
self.client.post("/api/accounts/register", {"username": self.username, "password": self.password}) User.objects.create_user(username = self.username, password = self.password)
def test_normal_login(self): def test_normal(self):
response: HttpResponse = self.client.post("/api/accounts/login", {"username": self.username, "password": self.password}) self.client.login(username = self.username, password = self.password)
response_text: str = response.content.decode("utf-8") response: HttpResponse = self.client.post(self.url, {"new_password": self.new_password})
self.assertEqual(response_text, USER_VALID) response_text: str = response.content.decode('utf-8')
self.assertEqual(response_text, USER_PASSWORD_UPDATED)
def test_invalid_username(self): def test_nologged(self):
response: HttpResponse = self.client.post(self.url, {"username": self.password, "password": self.password}) response: HttpResponse = self.client.post(self.url, {"new_password": self.new_password})
response_text: str = response.content.decode("utf-8") response_text: str = response.content.decode('utf-8')
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD) self.assertEqual(response_text, '')
def test_invalid_password(self):
response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": self.username})
response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
def test_invalid_no_username(self):
response: HttpResponse = self.client.post(self.url, {"password": self.password})
response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
def test_invalid_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_USERNAME_PASSWORD)
def test_invalid_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_USERNAME_PASSWORD)
def test_invalid_no_password_no_username(self):
response: HttpResponse = self.client.post(self.url, {})
response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
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_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_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_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})
response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, PASSWORD_UPDATED)

View File

@ -3,10 +3,11 @@ from django.test import TestCase
# Create your tests here. # Create your tests here.
from django.test.client import Client from django.test.client import Client
from django.http import HttpResponse from django.http import HttpResponse
from django.contrib.auth.models import User
import uuid import uuid
from ..status_code import * from ..status_code import *
from ..settings import *
class DeleteTest(TestCase): class DeleteTest(TestCase):
def setUp(self): def setUp(self):
@ -14,42 +15,20 @@ class DeleteTest(TestCase):
self.url = "/api/accounts/delete" self.url = "/api/accounts/delete"
self.username: str = str(uuid.uuid4())[:USERNAME_MAX_SIZE] self.username: str = str(uuid.uuid4())
self.password: str = str(uuid.uuid4())[:PASSWORD_MAX_SIZE] self.password: str = str(uuid.uuid4())
self.client.post("/api/accounts/register", {"username": self.username, "password": self.password}) user: User = User.objects.create_user(username=self.username, password=self.password)
self.client.login(username=self.username, password=self.password)
def test_invalid_username(self):
response: HttpResponse = self.client.post(self.url, {"username": self.password, "password": self.password})
response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
def test_invalid_password(self):
response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": self.username})
response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
def test_invalid_no_username(self):
response: HttpResponse = self.client.post(self.url, {"password": self.password})
response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
def test_invalid_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_USERNAME_PASSWORD)
def test_invalid_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_USERNAME_PASSWORD)
def test_invalid_no_password_no_username(self):
response: HttpResponse = self.client.post(self.url, {})
response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
def test_normal_delete(self): def test_normal_delete(self):
response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": self.password}) 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, USER_DELETED) self.assertEqual(response_text, USER_DELETED)
def test_no_logged(self):
self.client.logout()
response: HttpResponse = self.client.post(self.url)
response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, '')

View File

@ -2,11 +2,11 @@ from django.test import TestCase
# Create your tests here. # Create your tests here.
from django.test.client import Client from django.test.client import Client
from django.contrib.auth.models import User
from django.http import HttpResponse from django.http import HttpResponse
import uuid import uuid
from ..status_code import * from ..status_code import *
from ..settings import *
class LoginTest(TestCase): class LoginTest(TestCase):
def setUp(self): def setUp(self):
@ -14,37 +14,43 @@ class LoginTest(TestCase):
self.url = "/api/accounts/login" self.url = "/api/accounts/login"
self.username: str = str(uuid.uuid4())[:USERNAME_MAX_SIZE] self.username: str = str(uuid.uuid4())
self.password: str = str(uuid.uuid4())[:PASSWORD_MAX_SIZE] self.password: str = str(uuid.uuid4())
self.client.post("/api/accounts/register", {"username": self.username, "password": self.password}) User.objects.create_user(username=self.username, password=self.password)
def test_normal_login(self): def test_normal_login(self):
response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": self.password}) #User(username=self.username, password=self.password).save()
response_text: str = response.content.decode("utf-8") response: HttpResponse = self.client.post(self.url, {'username': self.username, 'password': self.password})
self.assertEqual(response_text, USER_VALID) response_text = response.content.decode('utf-8')
self.assertEqual(response_text, USER_LOGGED)
def test_invalid_username(self): def test_invalid_username(self):
response: HttpResponse = self.client.post(self.url, {"username": self.password, "password": self.password}) response: HttpResponse = self.client.post(self.url, {"username": self.password, "password": self.password})
response_text: str = response.content.decode("utf-8") errors: dict = eval(response.content)
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD) errors_expected: dict = {'user': [USER_INVALID]}
self.assertEqual(errors, errors_expected)
def test_invalid_password(self): def test_invalid_password(self):
response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": self.username}) response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": self.username})
response_text: str = response.content.decode("utf-8") errors: dict = eval(response.content)
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD) errors_expected: dict = {'user': [USER_INVALID]}
self.assertEqual(errors, errors_expected)
def test_invalid_no_username(self): def test_invalid_no_username(self):
response: HttpResponse = self.client.post(self.url, {"password": self.password}) response: HttpResponse = self.client.post(self.url, {"password": self.password})
response_text: str = response.content.decode("utf-8") errors: dict = eval(response.content)
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD) errors_expected: dict = {'username': [USERNAME_MISSING]}
self.assertEqual(errors, errors_expected)
def test_invalid_no_password(self): def test_invalid_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") errors: dict = eval(response.content)
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD) errors_expected: dict = {'password': [PASSWORD_MISSING]}
self.assertEqual(errors, errors_expected)
def test_invalid_no_password_no_username(self): def test_invalid_no_password_no_username(self):
response: HttpResponse = self.client.post(self.url, {}) response: HttpResponse = self.client.post(self.url, {})
response_text: str = response.content.decode("utf-8") errors: dict = eval(response.content)
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD) errors_expected: dict = {'username': [USERNAME_MISSING], 'password': [PASSWORD_MISSING]}
self.assertEqual(errors, errors_expected)

View File

@ -2,11 +2,11 @@ from django.test import TestCase
# Create your tests here. # Create your tests here.
from django.test.client import Client from django.test.client import Client
from django.contrib.auth.models import User
from django.http import HttpResponse from django.http import HttpResponse
import uuid import uuid
from ..status_code import * from ..status_code import *
from ..settings import *
class RegisterTest(TestCase): class RegisterTest(TestCase):
def setUp(self): def setUp(self):
@ -14,56 +14,41 @@ class RegisterTest(TestCase):
self.url: str = "/api/accounts/register" self.url: str = "/api/accounts/register"
self.username: str = str(uuid.uuid4())[:USERNAME_MAX_SIZE] self.username: str = str(uuid.uuid4())
self.password: str = str(uuid.uuid4())[:PASSWORD_MAX_SIZE] self.password: str = str(uuid.uuid4())
def test_normal_register(self):
response: HttpResponse = self.client.post(self.url, {'username': self.username, 'password': self.password})
response_text: str = response.content.decode('utf-8')
self.assertEqual(USER_ADDED, response_text)
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") errors: dict = eval(response.content)
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD) errors_expected: dict = {'username': [USERNAME_MISSING], 'password': [PASSWORD_MISSING]}
self.assertEqual(errors, errors_expected)
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") errors: dict = eval(response.content)
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD) errors_expected: dict = {'password': [PASSWORD_MISSING]}
self.assertEqual(errors, errors_expected)
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})
response_text: str = response.content.decode("utf-8") errors: dict = eval(response.content)
self.assertEqual(response_text, "error: username invalid") errors_expected: dict = {}
self.assertEqual(errors, errors_expected)
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, {"password": self.password})
response_text: str = response.content.decode("utf-8") errors: dict = eval(response.content)
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD) errors_expected: dict = {'username': [USERNAME_MISSING]}
self.assertEqual(errors, errors_expected)
def test_normal_register(self):
response: HttpResponse = self.client.post(self.url, {"username": self.username, "password": self.password})
response_text: str = response.content.decode("utf-8")
self.assertEqual(response_text, USER_ADDED)
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_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_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_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_USERNAME_PASSWORD)
def test_already_registered(self): def test_already_registered(self):
self.client.post(self.url, {"username": self.username, "password": self.password}) User(username=self.username, password=self.password).save()
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})
response_text: str = response.content.decode("utf-8") errors: dict = eval(response.content)
self.assertEqual(response_text, USERNAME_ALREADY_USED) errors_expected: dict = {'username': [USERNAME_ALREADY_USED]}
self.assertEqual(errors, errors_expected)

View File

@ -1,9 +1,10 @@
from django.urls import path from django.urls import path
from .views import login, register, delete, change_password from .views import login, logout, register, delete, change_password
urlpatterns = [ urlpatterns = [
path("login", login.LoginView.as_view(), name="login"), path("login", login.LoginView.as_view(), name="login"),
path("logout", logout.LogoutView.as_view(), name="logout"),
path("register", register.RegisterView.as_view(), name="register"), path("register", register.RegisterView.as_view(), name="register"),
path("delete", delete.DeleteView.as_view(), name="delete"), path("delete", delete.DeleteView.as_view(), name="delete"),
path("change_password", change_password.ChangePasswordView.as_view(), name="change_password"), path("change_password", change_password.ChangePasswordView.as_view(), name="change_password"),

View File

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

View File

@ -1,35 +1,19 @@
from django.shortcuts import render from django.shortcuts import render
from django.views import View from django.views import View
from django.http import HttpResponse, HttpRequest from django.http import HttpResponse, HttpRequest
from django.contrib.auth.models import User from django.utils.decorators import method_decorator
from django.db.models.query import QuerySet from django.contrib.auth.decorators import login_required
from ..status_code import * from ..status_code import *
from ..settings import *
from ..forms.delete import DeleteForm
class DeleteView(View): class DeleteView(View):
@method_decorator(login_required, name='dispatch')
def get(self, request: HttpRequest): def get(self, request: HttpRequest):
return render(request, "delete.html", {"form": DeleteForm}) return HttpResponse(METHOD_INVALID)
@method_decorator(login_required, name='dispatch')
def post(self, request: HttpRequest): def post(self, request: HttpRequest):
request.user.delete()
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) return HttpResponse(USER_DELETED)

View File

@ -1,31 +1,31 @@
from django.shortcuts import render from django.shortcuts import render
from django.views import View from django.views import View
from django.http import HttpResponse from django.http import HttpResponse, HttpRequest, JsonResponse
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from django.db.models.query import QuerySet from django.db.models.query import QuerySet
from ..status_code import * from ..status_code import *
from ..settings import *
from ..forms.login import LoginForm from ..forms.login import LoginForm
class LoginView(View): class LoginView(View):
def get(self, request):
def get(self, request: HttpRequest):
if request.user.is_authenticated:
logout(request)
return render(request, "login.html", {"form": LoginForm}) return render(request, "login.html", {"form": LoginForm})
def post(self, request): def post(self, request: HttpRequest):
if request.user.is_authenticated:
logout(request)
form: LoginForm = LoginForm(request.POST) form: LoginForm = LoginForm(request.POST)
if not form.is_valid(): if not form.is_valid():
return HttpResponse(INVALID_USERNAME_PASSWORD) return JsonResponse(form.errors)
username: str = form.cleaned_data["username"] user: User = authenticate(username=form.cleaned_data['username'], password=form.cleaned_data['password'])
password: str = form.cleaned_data["password"] if user is None:
return JsonResponse({'user': [USER_INVALID]})
query: QuerySet = User.objects.filter(username=username) login(request, user)
if (not query.exists()): return HttpResponse(USER_LOGGED)
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)

15
accounts/views/logout.py Normal file
View File

@ -0,0 +1,15 @@
from django.shortcuts import render
from django.views import View
from django.http import HttpResponse, HttpRequest
from django.contrib.auth import logout
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required
from ..status_code import *
class LogoutView(View):
@method_decorator(login_required, name='dispatch')
def get(self, request: HttpRequest):
logout(request)
return HttpResponse(USER_LOGOUT)

View File

@ -1,29 +1,32 @@
from django.shortcuts import render from django.shortcuts import render
from django.views import View from django.views import View
from django.http import HttpResponse, HttpRequest from django.http import HttpResponse, HttpRequest, JsonResponse
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login, logout
from django.db.models.query import QuerySet from django.db.models.query import QuerySet
from django.contrib.auth.decorators import user_passes_test
from ..status_code import * from ..status_code import *
from ..settings import *
from ..forms.register import RegisterForm from ..forms.register import RegisterForm
class RegisterView(View): class RegisterView(View):
def get(self, request: HttpRequest): def get(self, request: HttpRequest):
return render(request, "register.html", {"form": RegisterForm}) if request.user.is_authenticated:
logout(request)
return render(request, 'register.html', {'form': RegisterForm})
def post(self, request: HttpRequest): def post(self, request: HttpRequest):
if request.user.is_authenticated:
logout(request)
form: RegisterForm = RegisterForm(request.POST) form: RegisterForm = RegisterForm(request.POST)
if not form.is_valid(): if not form.is_valid():
return HttpResponse(INVALID_USERNAME_PASSWORD) return JsonResponse(form.errors)
username: str = form.cleaned_data["username"] user: User = User.objects.create_user(username=form.cleaned_data['username'], password=form.cleaned_data['password'])
password: str = form.cleaned_data["password"]
if User.objects.filter(username=username).exists(): login(request, user)
return HttpResponse(USERNAME_ALREADY_USED)
user: User = User.objects.create_user(username, password=password)
user.save()
return HttpResponse(USER_ADDED) return HttpResponse(USER_ADDED)