Added to delete a user account in the Mia core application.

This commit is contained in:
依瑪貓 2020-08-09 22:53:56 +08:00
parent 1c67d0f5ac
commit b2aad32579
2 changed files with 32 additions and 3 deletions

View File

@ -32,8 +32,7 @@ urlpatterns = [
path("users/<user:user>", views.UserView.as_view(), name="users.detail"),
path("users/<user:user>/edit", views.user_form, name="users.edit"),
path("users/<user:user>/update", views.user_store, name="users.update"),
# TODO: To be done.
path("users/<user:user>/delete", views.todo, name="users.delete"),
path("users/<user:user>/delete", views.user_delete, name="users.delete"),
path("api/users/<str:login_id>/exists", views.api_users_exists,
name="api.users.exists"),
# TODO: To be done.

View File

@ -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.