diff --git a/django/trancendence/accounts/status_code.py b/django/trancendence/accounts/status_code.py new file mode 100644 index 0000000..c5d6964 --- /dev/null +++ b/django/trancendence/accounts/status_code.py @@ -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" \ No newline at end of file diff --git a/django/trancendence/accounts/views.py b/django/trancendence/accounts/views.py index 173a0ac..69e6485 100644 --- a/django/trancendence/accounts/views.py +++ b/django/trancendence/accounts/views.py @@ -6,6 +6,8 @@ from django.http import HttpResponse from django.contrib.auth.models import User from django.db.models.query import QuerySet +from .status_code import * + class Login(View): def get(self, request): return render(request, "login.html") @@ -13,21 +15,21 @@ class Login(View): def post(self, request): username = request.POST.get("username") if (username == None): - return HttpResponse("error: username or password invalid") + return HttpResponse(INVALID_USERNAME_PASSWORD) password = request.POST.get("password") if (password == None): - return HttpResponse("error: username or password invalid") + return HttpResponse(INVALID_USERNAME_PASSWORD) query: QuerySet = User.objects.filter(username=username) if (not query.exists()): - return HttpResponse("error: username or password invalid") + return HttpResponse(INVALID_USERNAME_PASSWORD) user: User = User.objects.get(username=username) 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): def get(self, request): @@ -36,18 +38,18 @@ class Register(View): def post(self, request): password = request.POST.get("password") if (password == None or len(password) < 3): - return HttpResponse("error: password invalid") + return HttpResponse(INVALID_PASSWORD) username = request.POST.get("username") if (username == None or len(username) < 3): - return HttpResponse("error: username invalid") + return HttpResponse(INVALID_USERNAME) 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.save() - return HttpResponse("ok: user added") + return HttpResponse(USER_ADDED) class Delete(View): def get(self, request): @@ -56,23 +58,23 @@ class Delete(View): def post(self, request): username = request.POST.get("username") if (username == None): - return HttpResponse("error: username or password invalid") + return HttpResponse(INVALID_USERNAME_PASSWORD) password = request.POST.get("password") if (password == None): - return HttpResponse("error: username or password invalid") + return HttpResponse(INVALID_USERNAME_PASSWORD) query: QuerySet = User.objects.filter(username=username) if (not query.exists()): - return HttpResponse("error: username or password invalid") + return HttpResponse(INVALID_USERNAME_PASSWORD) user: User = User.objects.get(username=username) if (not user.check_password(password)): - return HttpResponse("error: username or password invalid") + return HttpResponse(INVALID_USERNAME_PASSWORD) user.delete() - return HttpResponse("ok: account has been deleted") + return HttpResponse(USER_DELETED) class ChangePassword(View): def get(self, request): @@ -81,25 +83,25 @@ class ChangePassword(View): def post(self, request): username = request.POST.get("username") if (username == None): - return HttpResponse("error: username or password invalid") + return HttpResponse(INVALID_USERNAME_PASSWORD) current_password = request.POST.get("current_password") if (current_password == None): - return HttpResponse("error: username or password invalid") + return HttpResponse(INVALID_USERNAME_PASSWORD) query: QuerySet = User.objects.filter(username=username) if (not query.exists()): - return HttpResponse("error: username or password invalid") + return HttpResponse(INVALID_USERNAME_PASSWORD) user: User = User.objects.get(username=username) 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") 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.save() - return HttpResponse("ok: password has been updated") \ No newline at end of file + return HttpResponse(PASSWORD_UPDATED) \ No newline at end of file