register: use ModelForm, and print all errors
This commit is contained in:
parent
54cc1b1705
commit
a0c0d813b6
@ -1,7 +1,10 @@
|
||||
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):
|
||||
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)
|
||||
new_password = forms.CharField(required=True, error_messages = {
|
||||
'required': PASSWORD_MISSING,
|
||||
})
|
@ -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)
|
@ -1,6 +1,15 @@
|
||||
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):
|
||||
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)
|
||||
username = forms.CharField(required=True, error_messages={
|
||||
'required': USERNAME_MISSING,
|
||||
})
|
||||
password = forms.CharField(required=True, error_messages = {
|
||||
'required': PASSWORD_MISSING,
|
||||
})
|
@ -1,6 +1,22 @@
|
||||
from django import forms
|
||||
from ..settings import *
|
||||
from django.forms import ModelForm
|
||||
from django.contrib.auth.models import User
|
||||
from ..status_code 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)
|
||||
class RegisterForm(ModelForm):
|
||||
class Meta:
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +0,0 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
@ -1,4 +0,0 @@
|
||||
PASSWORD_MIN_SIZE = 3
|
||||
PASSWORD_MAX_SIZE = 128
|
||||
USERNAME_MIN_SIZE = 3
|
||||
USERNAME_MAX_SIZE = 40
|
@ -1,9 +1,18 @@
|
||||
INVALID_USERNAME: str = "error: username invalid"
|
||||
INVALID_PASSWORD: str = "error: password invalid"
|
||||
INVALID_USERNAME_PASSWORD: str = "error: username or password invalid"
|
||||
USERNAME_TOO_LONG: str = "error: username too long"
|
||||
USERNAME_TOO_SHORT: str = "error: username too short"
|
||||
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"
|
||||
|
||||
USER_ALREADY_LOGGED: str = "error: user already logged"
|
||||
USER_INVALID: str = "error: username or password invalid"
|
||||
|
||||
USER_ADDED: str = "ok: user added"
|
||||
USER_DELETED: str = "ok: account has been deleted"
|
||||
USER_VALID: str = "ok: account valid"
|
||||
PASSWORD_UPDATED: str = "ok: password has been updated"
|
||||
USER_LOGGED: str = "ok: account valid"
|
||||
USER_PASSWORD_UPDATED: str = "ok: password has been updated"
|
||||
USER_LOGOUT: str = "ok: user logout"
|
||||
|
||||
METHOD_INVALID: str = "error: method invalid"
|
@ -1,9 +1,7 @@
|
||||
<html>
|
||||
<form method='post'>
|
||||
{% csrf_token %}
|
||||
<input type="text" name="username" placeholder="username">
|
||||
<input type="text" name="current_password" placeholder="current_password">
|
||||
<input type="text" name="new_password" placeholder="new_password">
|
||||
{{form.as_p}}
|
||||
<input type='submit'>
|
||||
</form>
|
||||
</html>
|
@ -1,7 +1,7 @@
|
||||
<html>
|
||||
<form method='post'>
|
||||
{% csrf_token %}
|
||||
{{ form }}
|
||||
{{ form.as_p }}
|
||||
<input type='submit'>
|
||||
</form>
|
||||
</html>
|
@ -1,7 +1,7 @@
|
||||
<html>
|
||||
<form method='post'>
|
||||
{% csrf_token %}
|
||||
{{ form }}
|
||||
{{ form.as_p }}
|
||||
<input type='submit'>
|
||||
</form>
|
||||
</html>
|
@ -3,10 +3,11 @@ from django.test import TestCase
|
||||
# Create your tests here.
|
||||
from django.test.client import Client
|
||||
from django.http import HttpResponse
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
import uuid
|
||||
|
||||
from ..status_code import *
|
||||
from ..settings import *
|
||||
|
||||
class ChangePasswordTest(TestCase):
|
||||
def setUp(self):
|
||||
@ -14,63 +15,19 @@ class ChangePasswordTest(TestCase):
|
||||
|
||||
self.url = "/api/accounts/change_password"
|
||||
|
||||
self.username: str = str(uuid.uuid4())[:USERNAME_MAX_SIZE]
|
||||
self.password: str = str(uuid.uuid4())[:PASSWORD_MAX_SIZE]
|
||||
self.new_password: str = str(uuid.uuid4())[:PASSWORD_MAX_SIZE]
|
||||
self.username: str = str(uuid.uuid4())
|
||||
self.password: str = str(uuid.uuid4())
|
||||
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):
|
||||
response: HttpResponse = self.client.post("/api/accounts/login", {"username": self.username, "password": self.password})
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, USER_VALID)
|
||||
def test_normal(self):
|
||||
self.client.login(username = self.username, password = self.password)
|
||||
response: HttpResponse = self.client.post(self.url, {"new_password": self.new_password})
|
||||
response_text: str = response.content.decode('utf-8')
|
||||
self.assertEqual(response_text, USER_PASSWORD_UPDATED)
|
||||
|
||||
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_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)
|
||||
def test_nologged(self):
|
||||
response: HttpResponse = self.client.post(self.url, {"new_password": self.new_password})
|
||||
response_text: str = response.content.decode('utf-8')
|
||||
self.assertEqual(response_text, '')
|
@ -3,10 +3,11 @@ from django.test import TestCase
|
||||
# Create your tests here.
|
||||
from django.test.client import Client
|
||||
from django.http import HttpResponse
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
import uuid
|
||||
|
||||
from ..status_code import *
|
||||
from ..settings import *
|
||||
|
||||
class DeleteTest(TestCase):
|
||||
def setUp(self):
|
||||
@ -14,42 +15,20 @@ class DeleteTest(TestCase):
|
||||
|
||||
self.url = "/api/accounts/delete"
|
||||
|
||||
self.username: str = str(uuid.uuid4())[:USERNAME_MAX_SIZE]
|
||||
self.password: str = str(uuid.uuid4())[:PASSWORD_MAX_SIZE]
|
||||
self.username: str = str(uuid.uuid4())
|
||||
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):
|
||||
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")
|
||||
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, '')
|
@ -2,11 +2,11 @@ from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
from django.test.client import Client
|
||||
from django.contrib.auth.models import User
|
||||
from django.http import HttpResponse
|
||||
import uuid
|
||||
|
||||
from ..status_code import *
|
||||
from ..settings import *
|
||||
|
||||
class LoginTest(TestCase):
|
||||
def setUp(self):
|
||||
@ -14,37 +14,43 @@ class LoginTest(TestCase):
|
||||
|
||||
self.url = "/api/accounts/login"
|
||||
|
||||
self.username: str = str(uuid.uuid4())[:USERNAME_MAX_SIZE]
|
||||
self.password: str = str(uuid.uuid4())[:PASSWORD_MAX_SIZE]
|
||||
self.username: str = str(uuid.uuid4())
|
||||
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):
|
||||
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_VALID)
|
||||
#User(username=self.username, password=self.password).save()
|
||||
response: HttpResponse = self.client.post(self.url, {'username': self.username, 'password': self.password})
|
||||
response_text = response.content.decode('utf-8')
|
||||
self.assertEqual(response_text, USER_LOGGED)
|
||||
|
||||
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)
|
||||
errors: dict = eval(response.content)
|
||||
errors_expected: dict = {'user': [USER_INVALID]}
|
||||
self.assertEqual(errors, errors_expected)
|
||||
|
||||
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)
|
||||
errors: dict = eval(response.content)
|
||||
errors_expected: dict = {'user': [USER_INVALID]}
|
||||
self.assertEqual(errors, errors_expected)
|
||||
|
||||
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)
|
||||
errors: dict = eval(response.content)
|
||||
errors_expected: dict = {'username': [USERNAME_MISSING]}
|
||||
self.assertEqual(errors, errors_expected)
|
||||
|
||||
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)
|
||||
errors: dict = eval(response.content)
|
||||
errors_expected: dict = {'password': [PASSWORD_MISSING]}
|
||||
self.assertEqual(errors, errors_expected)
|
||||
|
||||
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)
|
||||
errors: dict = eval(response.content)
|
||||
errors_expected: dict = {'username': [USERNAME_MISSING], 'password': [PASSWORD_MISSING]}
|
||||
self.assertEqual(errors, errors_expected)
|
||||
|
@ -2,11 +2,11 @@ from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
from django.test.client import Client
|
||||
from django.contrib.auth.models import User
|
||||
from django.http import HttpResponse
|
||||
import uuid
|
||||
|
||||
from ..status_code import *
|
||||
from ..settings import *
|
||||
|
||||
class RegisterTest(TestCase):
|
||||
def setUp(self):
|
||||
@ -14,56 +14,41 @@ class RegisterTest(TestCase):
|
||||
|
||||
self.url: str = "/api/accounts/register"
|
||||
|
||||
self.username: str = str(uuid.uuid4())[:USERNAME_MAX_SIZE]
|
||||
self.password: str = str(uuid.uuid4())[:PASSWORD_MAX_SIZE]
|
||||
self.username: str = str(uuid.uuid4())
|
||||
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):
|
||||
response: HttpResponse = self.client.post(self.url)
|
||||
response_text: str = response.content.decode("utf-8")
|
||||
self.assertEqual(response_text, INVALID_USERNAME_PASSWORD)
|
||||
errors: dict = eval(response.content)
|
||||
errors_expected: dict = {'username': [USERNAME_MISSING], 'password': [PASSWORD_MISSING]}
|
||||
self.assertEqual(errors, errors_expected)
|
||||
|
||||
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_USERNAME_PASSWORD)
|
||||
errors: dict = eval(response.content)
|
||||
errors_expected: dict = {'password': [PASSWORD_MISSING]}
|
||||
self.assertEqual(errors, errors_expected)
|
||||
|
||||
def test_incomplet_form_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, "error: username invalid")
|
||||
errors: dict = eval(response.content)
|
||||
errors_expected: dict = {}
|
||||
self.assertEqual(errors, errors_expected)
|
||||
|
||||
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_USERNAME_PASSWORD)
|
||||
|
||||
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)
|
||||
response: HttpResponse = self.client.post(self.url, {"password": self.password})
|
||||
errors: dict = eval(response.content)
|
||||
errors_expected: dict = {'username': [USERNAME_MISSING]}
|
||||
self.assertEqual(errors, errors_expected)
|
||||
|
||||
def test_already_registered(self):
|
||||
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")
|
||||
self.assertEqual(response_text, USERNAME_ALREADY_USED)
|
||||
User(username=self.username, password=self.password).save()
|
||||
response: HttpResponse = self.client.post(self.url, {'username': self.username, 'password': self.password})
|
||||
errors: dict = eval(response.content)
|
||||
errors_expected: dict = {'username': [USERNAME_ALREADY_USED]}
|
||||
self.assertEqual(errors, errors_expected)
|
@ -1,9 +1,10 @@
|
||||
from django.urls import path
|
||||
|
||||
from .views import login, register, delete, change_password
|
||||
from .views import login, logout, register, delete, change_password
|
||||
|
||||
urlpatterns = [
|
||||
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("delete", delete.DeleteView.as_view(), name="delete"),
|
||||
path("change_password", change_password.ChangePasswordView.as_view(), name="change_password"),
|
||||
|
@ -1,36 +1,29 @@
|
||||
from django.shortcuts import render
|
||||
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.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 ..status_code import *
|
||||
|
||||
class ChangePasswordView(View):
|
||||
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):
|
||||
|
||||
form: ChangePasswordForm = ChangePasswordForm(request.POST)
|
||||
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']
|
||||
|
||||
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: User = request.user
|
||||
|
||||
user.set_password(new_password)
|
||||
user.save()
|
||||
|
||||
return HttpResponse(PASSWORD_UPDATED)
|
||||
return HttpResponse(USER_PASSWORD_UPDATED)
|
@ -1,35 +1,19 @@
|
||||
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 django.utils.decorators import method_decorator
|
||||
from django.contrib.auth.decorators import login_required
|
||||
|
||||
from ..status_code import *
|
||||
from ..settings import *
|
||||
from ..forms.delete import DeleteForm
|
||||
|
||||
class DeleteView(View):
|
||||
|
||||
@method_decorator(login_required, name='dispatch')
|
||||
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):
|
||||
|
||||
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()
|
||||
|
||||
request.user.delete()
|
||||
return HttpResponse(USER_DELETED)
|
||||
|
@ -1,31 +1,31 @@
|
||||
from django.shortcuts import render
|
||||
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 import authenticate, login
|
||||
from django.contrib.auth.decorators import login_required
|
||||
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):
|
||||
|
||||
def get(self, request: HttpRequest):
|
||||
if request.user.is_authenticated:
|
||||
logout(request)
|
||||
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)
|
||||
if not form.is_valid():
|
||||
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
||||
return JsonResponse(form.errors)
|
||||
|
||||
username: str = form.cleaned_data["username"]
|
||||
password: str = form.cleaned_data["password"]
|
||||
user: User = authenticate(username=form.cleaned_data['username'], password=form.cleaned_data['password'])
|
||||
if user is None:
|
||||
return JsonResponse({'user': [USER_INVALID]})
|
||||
|
||||
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)
|
||||
login(request, user)
|
||||
return HttpResponse(USER_LOGGED)
|
15
accounts/views/logout.py
Normal file
15
accounts/views/logout.py
Normal 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)
|
@ -1,29 +1,32 @@
|
||||
from django.shortcuts import render
|
||||
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 import authenticate, login, logout
|
||||
from django.db.models.query import QuerySet
|
||||
from django.contrib.auth.decorators import user_passes_test
|
||||
|
||||
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})
|
||||
if request.user.is_authenticated:
|
||||
logout(request)
|
||||
return render(request, 'register.html', {'form': RegisterForm})
|
||||
|
||||
def post(self, request: HttpRequest):
|
||||
if request.user.is_authenticated:
|
||||
logout(request)
|
||||
|
||||
form: RegisterForm = RegisterForm(request.POST)
|
||||
if not form.is_valid():
|
||||
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
||||
return JsonResponse(form.errors)
|
||||
|
||||
username: str = form.cleaned_data["username"]
|
||||
password: str = form.cleaned_data["password"]
|
||||
user: User = User.objects.create_user(username=form.cleaned_data['username'], password=form.cleaned_data['password'])
|
||||
|
||||
if User.objects.filter(username=username).exists():
|
||||
return HttpResponse(USERNAME_ALREADY_USED)
|
||||
|
||||
user: User = User.objects.create_user(username, password=password)
|
||||
user.save()
|
||||
login(request, user)
|
||||
|
||||
return HttpResponse(USER_ADDED)
|
Loading…
Reference in New Issue
Block a user