From b2aad325792488b50edfc4295b5d4e9064dd6375 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BE=9D=E7=91=AA=E8=B2=93?= Date: Sun, 9 Aug 2020 22:53:56 +0800 Subject: [PATCH] Added to delete a user account in the Mia core application. --- mia_core/urls.py | 3 +-- mia_core/views.py | 32 +++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/mia_core/urls.py b/mia_core/urls.py index e0596eb..db44290 100644 --- a/mia_core/urls.py +++ b/mia_core/urls.py @@ -32,8 +32,7 @@ urlpatterns = [ path("users/", views.UserView.as_view(), name="users.detail"), path("users//edit", views.user_form, name="users.edit"), path("users//update", views.user_store, name="users.update"), - # TODO: To be done. - path("users//delete", views.todo, name="users.delete"), + path("users//delete", views.user_delete, name="users.delete"), path("api/users//exists", views.api_users_exists, name="api.users.exists"), # TODO: To be done. diff --git a/mia_core/views.py b/mia_core/views.py index 0ca57c2..5476ec2 100644 --- a/mia_core/views.py +++ b/mia_core/views.py @@ -138,7 +138,7 @@ def user_store(request, user=None): user.name = form["name"].value() user.is_disabled = form["is_disabled"].value() if not user.is_dirty(): - message = gettext_noop("This user account was not modified.") + message = gettext_noop("This user account was not changed.") else: user.save(current_user=request.user) message = gettext_noop("This user account was saved successfully.") @@ -146,6 +146,36 @@ def user_store(request, user=None): return redirect("mia_core:users.detail", user) +@require_POST +@login_required +def user_delete(request, user): + """The view to delete an user. + + Args: + request (HttpRequest): The request. + user (User): The user. + + Returns: + HttpResponseRedirect: The response. + """ + message = None + if user.pk == request.user.pk: + message = gettext_noop("You cannot delete your own account.") + elif user.is_in_use(): + message = gettext_noop( + "You cannot delete this account because it is in use.") + elif user.is_deleted: + message = gettext_noop("This account is already deleted.") + if message is not None: + messages.error(request, message) + return redirect("mia_core:users.detail", user) + user.delete() + message = gettext_noop("This user account was deleted successfully.") + messages.success(request, message) + return redirect("mia_core:users") + + + def api_users_exists(request, login_id): """The view to check whether a user with a log in ID exists.