Code used to override the delete_queryset in the modeladmin:
def get_actions(self, request):
actions = super().get_actions(request)
del actions['delete_selected']
return actions
def really_delete_selected(self, request, queryset):
'''
# 1. Update the group-mailbox relation: `goto` values if mailbox(es) are deleted
# 2. Sending a mail to the current user with CSV of mailboxes deleted
# 3. Used domain space will reduce by the maxquota of mailbox
'''
response = None
print('delete from mailbox queryset called')
try:
mbox_list = []
failed_deletions = []
print(queryset)
for obj in queryset:
mdomain = None
# COMPLETE MAILBOX DELETION OF FOLDERS, DOVECOT ENTRY, ADMIN PANEL PERMANENTLY
api_delete_status = call_purge_mailbox_api(request,obj)
response = api_delete_status
print('response---------',response, response['status_code'])
if response['status_code'] == 200:
# Set the quota value after deletion of mailbox(es)
mdomain = Domain.objects.get(id=obj.domain.pk)
mdomain.quota -= obj.maxquota
mdomain.save()
mbox_list.append(obj.email)
# Remove the user from the group mailing lists
# master_grp_list = GroupMailIds.objects.exclude(goto=None)
removed_from_groups :bool = remove_from_group_lists(obj.email,mbox_list)
print('mailbox deletion completed.....')
# TODO: List for sending a mail to the currently logged in user with CSV of
# mailboxes deleted
else:
print('Failed to delete mailbox:', obj.email)
failed_deletions.append(obj.email)
print(failed_deletions, len(failed_deletions))
if mbox_list: # Check if any mailboxes were successfully deleted
self.message_user(request, f"Successfully deleted {len(mbox_list)} mailbox(es).",level='success')
for email in failed_deletions: # Display error message for each failed deletion
self.message_user(request, f"Failed to delete {email}. Mailbox absent. Contact helpdesk.",level='error')
if not mbox_list and not failed_deletions: # Check if no deletions were attempted
self.message_user(request, "No mailboxes were selected for deletion.",level='error')
#return messages.error(request, "No mailboxes were selected for deletion.")
except Exception as e:
print(e)
self.message_user(request, f"Failed to delete items: {str(e)}", level='error')
really_delete_selected.short_description = "Delete selected entries"
def delete_model(self, request, obj):
try:
# Perform the deletion
api_delete_status = call_purge_mailbox_api(request,obj)
print('8888888')
response = api_delete_status
print('response---------',response, response['status_code'])
if response['status_code'] == 200:
obj.delete()
self.message_user(request, f"Successfully deleted {obj}.", level='success')
return HttpResponseRedirect(request.path)
else:
self.message_user(request, f"Failed to delete mailbox {obj}. Contact Helpdesk", level='error')
except Exception as e:
# Handle deletion failure
self.message_user(request, f"Failed to delete {obj}. Error: {str(e)}", level='error')
return HttpResponseRedirect(reverse('admin:%s_%s_changelist' % (obj._meta.app_label, obj._meta.model_name)))
This code is displaying correctly for the delete_queryset on failure of deletion. But when we delete the mailbox object in the model admin page after confirm_delete(confirm_delete.html), the message displayed after returning to the listing page are:
Failed to delete mailbox [email protected]. Contact Helpdesk
as well as
The mailbox “[email protected]” was deleted successfully.
Failure is the expected result. But why does django also display successful deletion? How to hide this message in the modeladmin listing page for failed cases?
Deletion from queryset works as expected. Displays the failed message only