From 2a0239b6ba50d136643ff6d1cd1342a2511d5ee3 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Sun, 19 Jun 2022 22:01:22 -0700 Subject: [PATCH] users: Fix deleting user LDAP entry with Django 4.0 Helps: #2228. In Django 4.0, form_valid() method should be overridden instead of delete() method. This is because DeleteView inherits from FormMixin. To make the code work for Django 2.2 and up, implement both methods but make delete() method available only after base __init__() so that Django does not show a warning with Django 4.0. Tests: - Run unit tests on stable, testing and unstable containers. - Create a temporary user and delete the user. User deletion success message must be show. Create another user with the same username as the deleted user (to ensure that deletion actually happened). Perform the test on stable, testing and unstable containers. Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/modules/users/views.py | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/plinth/modules/users/views.py b/plinth/modules/users/views.py index c0ea7986e..3df2f31a7 100644 --- a/plinth/modules/users/views.py +++ b/plinth/modules/users/views.py @@ -133,14 +133,16 @@ class UserDelete(ContextMixin, DeleteView): success_url = reverse_lazy('users:index') title = gettext_lazy('Delete User') - def delete(self, *args, **kwargs): - """Set the success message of deleting the user. + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) - The SuccessMessageMixin doesn't work with the DeleteView on Django1.7, - so set the success message manually here. - """ - output = super(UserDelete, self).delete(*args, **kwargs) + # Avoid a warning with Django 4.0 that delete member should not be + # overridden. Remove this line and _delete() after Django 4.0 reaches + # Debian Stable. + self.delete = self._delete + def _delete_from_ldap(self): + """Remove user from LDAP and show a success/error message.""" message = _('User {user} deleted.').format(user=self.kwargs['slug']) messages.success(self.request, message) @@ -150,8 +152,26 @@ class UserDelete(ContextMixin, DeleteView): except ActionError: messages.error(self.request, _('Deleting LDAP user failed.')) + def _delete(self, *args, **kwargs): + """Set the success message of deleting the user. + + The SuccessMessageMixin doesn't work with the DeleteView on Django1.7, + so set the success message manually here. + """ + output = super().delete(*args, **kwargs) + self._delete_from_ldap() return output + def form_valid(self, form): + """Perform additional operations after delete. + + Since Django 4.0, DeleteView inherits form_view and a call to delete() + is not made. + """ + response = super().form_valid(form) # NOQA, pylint: disable=no-member + self._delete_from_ldap() + return response + class UserChangePassword(ContextMixin, SuccessMessageMixin, FormView): """View to change user password."""