From 20ee634cda99421038be00906b29925d279adbea Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Sun, 11 Oct 2020 21:54:00 +0200 Subject: [PATCH] admin: add buttons to disable and enable users --- .../administration/user/disable.html | 42 ++++++++++++++++ .../templates/administration/user/list.html | 6 ++- passbook/admin/urls.py | 4 ++ passbook/admin/views/users.py | 48 +++++++++++++++++++ 4 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 passbook/admin/templates/administration/user/disable.html diff --git a/passbook/admin/templates/administration/user/disable.html b/passbook/admin/templates/administration/user/disable.html new file mode 100644 index 000000000..0a3113330 --- /dev/null +++ b/passbook/admin/templates/administration/user/disable.html @@ -0,0 +1,42 @@ +{% extends "administration/base.html" %} + +{% load i18n %} +{% load passbook_utils %} + +{% block content %} +
+
+ {% block above_form %} +

+ {% blocktrans with object_type=object|verbose_name %} + Disable {{ object_type }} + {% endblocktrans %} +

+ {% endblock %} +
+
+
+
+
+
+
+
+ {% csrf_token %} +

+ {% blocktrans with object_type=object|verbose_name name=object %} + Are you sure you want to disable {{ object_type }} "{{ object }}"? + {% endblocktrans %} +

+
+ +
+
+
+
+
+
+
+{% endblock %} diff --git a/passbook/admin/templates/administration/user/list.html b/passbook/admin/templates/administration/user/list.html index fccf30750..7868b95b9 100644 --- a/passbook/admin/templates/administration/user/list.html +++ b/passbook/admin/templates/administration/user/list.html @@ -54,7 +54,11 @@ {% trans 'Edit' %} - {% trans 'Delete' %} + {% if user.is_active %} + {% trans 'Disable' %} + {% else %} + {% trans 'Enable' %} + {% endif %} {% trans 'Reset Password' %} {% trans 'Impersonate' %} diff --git a/passbook/admin/urls.py b/passbook/admin/urls.py index b4221586e..86a52ca40 100644 --- a/passbook/admin/urls.py +++ b/passbook/admin/urls.py @@ -247,6 +247,10 @@ urlpatterns = [ path("users/create/", users.UserCreateView.as_view(), name="user-create"), path("users//update/", users.UserUpdateView.as_view(), name="user-update"), path("users//delete/", users.UserDeleteView.as_view(), name="user-delete"), + path( + "users//disable/", users.UserDisableView.as_view(), name="user-disable" + ), + path("users//enable/", users.UserEnableView.as_view(), name="user-enable"), path( "users//reset/", users.UserPasswordResetView.as_view(), diff --git a/passbook/admin/views/users.py b/passbook/admin/views/users.py index 9644853da..6f490ebc6 100644 --- a/passbook/admin/views/users.py +++ b/passbook/admin/views/users.py @@ -6,6 +6,7 @@ from django.contrib.auth.mixins import ( ) from django.contrib.messages.views import SuccessMessageMixin from django.http import HttpRequest, HttpResponse +from django.http.response import HttpResponseRedirect from django.shortcuts import redirect from django.urls import reverse, reverse_lazy from django.utils.http import urlencode @@ -98,6 +99,53 @@ class UserDeleteView(LoginRequiredMixin, PermissionRequiredMixin, DeleteMessageV success_message = _("Successfully deleted User") +class UserDisableView( + LoginRequiredMixin, PermissionRequiredMixin, BackSuccessUrlMixin, DeleteMessageView +): + """Disable user""" + + object: User + + model = User + permission_required = "passbook_core.update_user" + + # By default the object's name is user which is used by other checks + context_object_name = "object" + template_name = "administration/user/disable.html" + success_url = reverse_lazy("passbook_admin:users") + success_message = _("Successfully disabled User") + + def delete(self, request: HttpRequest, *args, **kwargs) -> HttpResponse: + self.object: User = self.get_object() + success_url = self.get_success_url() + self.object.is_active = False + self.object.save() + return HttpResponseRedirect(success_url) + + +class UserEnableView( + LoginRequiredMixin, PermissionRequiredMixin, BackSuccessUrlMixin, DetailView +): + """Enable user""" + + object: User + + model = User + permission_required = "passbook_core.update_user" + + # By default the object's name is user which is used by other checks + context_object_name = "object" + success_url = reverse_lazy("passbook_admin:users") + success_message = _("Successfully enabled User") + + def get(self, request: HttpRequest, *args, **kwargs): + self.object: User = self.get_object() + success_url = self.get_success_url() + self.object.is_active = True + self.object.save() + return HttpResponseRedirect(success_url) + + class UserPasswordResetView(LoginRequiredMixin, PermissionRequiredMixin, DetailView): """Get Password reset link for user"""