replace: hardcoded html return by variable
This commit is contained in:
parent
fb5ff0dad7
commit
07ad080551
9
django/trancendence/accounts/status_code.py
Normal file
9
django/trancendence/accounts/status_code.py
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
INVALID_USERNAME: str = "error: username invalid"
|
||||||
|
INVALID_PASSWORD: str = "error: password invalid"
|
||||||
|
INVALID_USERNAME_PASSWORD: str = "error: username or password invalid"
|
||||||
|
USERNAME_ALREADY_USED: str = "error: username already used"
|
||||||
|
|
||||||
|
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"
|
@ -6,6 +6,8 @@ from django.http import HttpResponse
|
|||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.db.models.query import QuerySet
|
from django.db.models.query import QuerySet
|
||||||
|
|
||||||
|
from .status_code import *
|
||||||
|
|
||||||
class Login(View):
|
class Login(View):
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
return render(request, "login.html")
|
return render(request, "login.html")
|
||||||
@ -13,21 +15,21 @@ class Login(View):
|
|||||||
def post(self, request):
|
def post(self, request):
|
||||||
username = request.POST.get("username")
|
username = request.POST.get("username")
|
||||||
if (username == None):
|
if (username == None):
|
||||||
return HttpResponse("error: username or password invalid")
|
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
||||||
|
|
||||||
password = request.POST.get("password")
|
password = request.POST.get("password")
|
||||||
if (password == None):
|
if (password == None):
|
||||||
return HttpResponse("error: username or password invalid")
|
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
||||||
|
|
||||||
query: QuerySet = User.objects.filter(username=username)
|
query: QuerySet = User.objects.filter(username=username)
|
||||||
if (not query.exists()):
|
if (not query.exists()):
|
||||||
return HttpResponse("error: username or password invalid")
|
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
||||||
|
|
||||||
user: User = User.objects.get(username=username)
|
user: User = User.objects.get(username=username)
|
||||||
if (not user.check_password(password)):
|
if (not user.check_password(password)):
|
||||||
return HttpResponse("error: username or password invalid")
|
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
||||||
|
|
||||||
return HttpResponse("ok: account valid")
|
return HttpResponse(USER_VALID)
|
||||||
|
|
||||||
class Register(View):
|
class Register(View):
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
@ -36,18 +38,18 @@ class Register(View):
|
|||||||
def post(self, request):
|
def post(self, request):
|
||||||
password = request.POST.get("password")
|
password = request.POST.get("password")
|
||||||
if (password == None or len(password) < 3):
|
if (password == None or len(password) < 3):
|
||||||
return HttpResponse("error: password invalid")
|
return HttpResponse(INVALID_PASSWORD)
|
||||||
username = request.POST.get("username")
|
username = request.POST.get("username")
|
||||||
if (username == None or len(username) < 3):
|
if (username == None or len(username) < 3):
|
||||||
return HttpResponse("error: username invalid")
|
return HttpResponse(INVALID_USERNAME)
|
||||||
|
|
||||||
if User.objects.filter(username=username).exists():
|
if User.objects.filter(username=username).exists():
|
||||||
return HttpResponse("error: username already used")
|
return HttpResponse(USERNAME_ALREADY_USED)
|
||||||
|
|
||||||
user = User.objects.create_user(username, password=password)
|
user = User.objects.create_user(username, password=password)
|
||||||
user.save()
|
user.save()
|
||||||
|
|
||||||
return HttpResponse("ok: user added")
|
return HttpResponse(USER_ADDED)
|
||||||
|
|
||||||
class Delete(View):
|
class Delete(View):
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
@ -56,23 +58,23 @@ class Delete(View):
|
|||||||
def post(self, request):
|
def post(self, request):
|
||||||
username = request.POST.get("username")
|
username = request.POST.get("username")
|
||||||
if (username == None):
|
if (username == None):
|
||||||
return HttpResponse("error: username or password invalid")
|
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
||||||
|
|
||||||
password = request.POST.get("password")
|
password = request.POST.get("password")
|
||||||
if (password == None):
|
if (password == None):
|
||||||
return HttpResponse("error: username or password invalid")
|
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
||||||
|
|
||||||
query: QuerySet = User.objects.filter(username=username)
|
query: QuerySet = User.objects.filter(username=username)
|
||||||
if (not query.exists()):
|
if (not query.exists()):
|
||||||
return HttpResponse("error: username or password invalid")
|
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
||||||
|
|
||||||
user: User = User.objects.get(username=username)
|
user: User = User.objects.get(username=username)
|
||||||
if (not user.check_password(password)):
|
if (not user.check_password(password)):
|
||||||
return HttpResponse("error: username or password invalid")
|
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
||||||
|
|
||||||
user.delete()
|
user.delete()
|
||||||
|
|
||||||
return HttpResponse("ok: account has been deleted")
|
return HttpResponse(USER_DELETED)
|
||||||
|
|
||||||
class ChangePassword(View):
|
class ChangePassword(View):
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
@ -81,25 +83,25 @@ class ChangePassword(View):
|
|||||||
def post(self, request):
|
def post(self, request):
|
||||||
username = request.POST.get("username")
|
username = request.POST.get("username")
|
||||||
if (username == None):
|
if (username == None):
|
||||||
return HttpResponse("error: username or password invalid")
|
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
||||||
|
|
||||||
current_password = request.POST.get("current_password")
|
current_password = request.POST.get("current_password")
|
||||||
if (current_password == None):
|
if (current_password == None):
|
||||||
return HttpResponse("error: username or password invalid")
|
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
||||||
|
|
||||||
query: QuerySet = User.objects.filter(username=username)
|
query: QuerySet = User.objects.filter(username=username)
|
||||||
if (not query.exists()):
|
if (not query.exists()):
|
||||||
return HttpResponse("error: username or password invalid")
|
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
||||||
|
|
||||||
user: User = User.objects.get(username=username)
|
user: User = User.objects.get(username=username)
|
||||||
if (not user.check_password(current_password)):
|
if (not user.check_password(current_password)):
|
||||||
return HttpResponse("error: username or password invalid")
|
return HttpResponse(INVALID_USERNAME_PASSWORD)
|
||||||
|
|
||||||
new_password = request.POST.get("new_password")
|
new_password = request.POST.get("new_password")
|
||||||
if (new_password == None or len(new_password) < 3):
|
if (new_password == None or len(new_password) < 3):
|
||||||
return HttpResponse("error: new password too short")
|
return HttpResponse(INVALID_PASSWORD)
|
||||||
|
|
||||||
user.set_password(new_password)
|
user.set_password(new_password)
|
||||||
user.save()
|
user.save()
|
||||||
|
|
||||||
return HttpResponse("ok: password has been updated")
|
return HttpResponse(PASSWORD_UPDATED)
|
Loading…
Reference in New Issue
Block a user