mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-01-21 07:55:00 +00:00
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 <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
parent
5d0a7c6d16
commit
2a0239b6ba
@ -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."""
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user