From 4cda3de144a6ff34f9add45cc088f4e30454433c Mon Sep 17 00:00:00 2001 From: starnakin Date: Wed, 25 Oct 2023 15:35:24 +0200 Subject: [PATCH] core: accounts: separate views i multiple files --- django/trancendence/accounts/urls.py | 10 +- django/trancendence/accounts/views.py | 108 ------------------ .../accounts/views/change_password.py | 38 ++++++ django/trancendence/accounts/views/delete.py | 34 ++++++ django/trancendence/accounts/views/login.py | 32 ++++++ .../trancendence/accounts/views/register.py | 28 +++++ 6 files changed, 137 insertions(+), 113 deletions(-) delete mode 100644 django/trancendence/accounts/views.py create mode 100644 django/trancendence/accounts/views/change_password.py create mode 100644 django/trancendence/accounts/views/delete.py create mode 100644 django/trancendence/accounts/views/login.py create mode 100644 django/trancendence/accounts/views/register.py diff --git a/django/trancendence/accounts/urls.py b/django/trancendence/accounts/urls.py index 076f9fb..a551d6e 100644 --- a/django/trancendence/accounts/urls.py +++ b/django/trancendence/accounts/urls.py @@ -1,10 +1,10 @@ from django.urls import path -from . import views +from .views import login, register, delete, change_password urlpatterns = [ - path("login", views.Login.as_view(), name="login"), - path("register", views.Register.as_view(), name="register"), - path("delete", views.Delete.as_view(), name="delete"), - path("change_password", views.ChangePassword.as_view(), name="delete"), + 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"), ] \ No newline at end of file diff --git a/django/trancendence/accounts/views.py b/django/trancendence/accounts/views.py deleted file mode 100644 index 78f34c5..0000000 --- a/django/trancendence/accounts/views.py +++ /dev/null @@ -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) \ No newline at end of file diff --git a/django/trancendence/accounts/views/change_password.py b/django/trancendence/accounts/views/change_password.py new file mode 100644 index 0000000..778839a --- /dev/null +++ b/django/trancendence/accounts/views/change_password.py @@ -0,0 +1,38 @@ +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 * + +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) \ No newline at end of file diff --git a/django/trancendence/accounts/views/delete.py b/django/trancendence/accounts/views/delete.py new file mode 100644 index 0000000..15409b2 --- /dev/null +++ b/django/trancendence/accounts/views/delete.py @@ -0,0 +1,34 @@ +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 * + +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) + \ No newline at end of file diff --git a/django/trancendence/accounts/views/login.py b/django/trancendence/accounts/views/login.py new file mode 100644 index 0000000..3962ce5 --- /dev/null +++ b/django/trancendence/accounts/views/login.py @@ -0,0 +1,32 @@ +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 * + +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) \ No newline at end of file diff --git a/django/trancendence/accounts/views/register.py b/django/trancendence/accounts/views/register.py new file mode 100644 index 0000000..5b64396 --- /dev/null +++ b/django/trancendence/accounts/views/register.py @@ -0,0 +1,28 @@ +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 * + +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) \ No newline at end of file