diff --git a/django/trancendence/accounts/templates/change_password.html b/django/trancendence/accounts/templates/change_password.html new file mode 100644 index 0000000..dd2240c --- /dev/null +++ b/django/trancendence/accounts/templates/change_password.html @@ -0,0 +1,9 @@ + +
+ \ No newline at end of file diff --git a/django/trancendence/accounts/urls.py b/django/trancendence/accounts/urls.py index 6729508..076f9fb 100644 --- a/django/trancendence/accounts/urls.py +++ b/django/trancendence/accounts/urls.py @@ -6,4 +6,5 @@ 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"), ] \ No newline at end of file diff --git a/django/trancendence/accounts/views.py b/django/trancendence/accounts/views.py index fe70c26..173a0ac 100644 --- a/django/trancendence/accounts/views.py +++ b/django/trancendence/accounts/views.py @@ -72,4 +72,34 @@ class Delete(View): user.delete() - return HttpResponse("ok: account has been deleted") \ No newline at end of file + return HttpResponse("ok: account has been 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("error: username or password invalid") + + current_password = request.POST.get("current_password") + if (current_password == None): + return HttpResponse("error: username or password invalid") + + query: QuerySet = User.objects.filter(username=username) + if (not query.exists()): + return HttpResponse("error: username or password invalid") + + user: User = User.objects.get(username=username) + if (not user.check_password(current_password)): + return HttpResponse("error: username or password invalid") + + new_password = request.POST.get("new_password") + if (new_password == None or len(new_password) < 3): + return HttpResponse("error: new password too short") + + user.set_password(new_password) + user.save() + + return HttpResponse("ok: password has been updated") \ No newline at end of file