-
Notifications
You must be signed in to change notification settings - Fork 123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add hard delete action #151
Open
helena238
wants to merge
2
commits into
makinacorpus:master
Choose a base branch
from
helena238:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
from django.utils.translation import ugettext_lazy as _ | ||
|
||
from .utils import related_objects | ||
from .models import HARD_DELETE | ||
|
||
# Django 3.0 compatibility | ||
try: | ||
|
@@ -48,11 +49,12 @@ class SafeDeleteAdmin(admin.ModelAdmin): | |
... list_filter = ("last_name",) + SafeDeleteAdmin.list_filter | ||
""" | ||
undelete_selected_confirmation_template = "safedelete/undelete_selected_confirmation.html" | ||
hard_delete_confirmation_template = "safedelete/hard_delete_confirmation.html" | ||
|
||
list_display = ('deleted',) | ||
list_filter = ('deleted',) | ||
exclude = ('deleted',) | ||
actions = ('undelete_selected',) | ||
actions = ('undelete_selected', 'hard_delete') | ||
|
||
class Meta: | ||
abstract = True | ||
|
@@ -165,3 +167,80 @@ def undelete_selected(self, request, queryset): | |
context, | ||
) | ||
undelete_selected.short_description = _("Undelete selected %(verbose_name_plural)s.") | ||
|
||
def hard_delete(self, request, queryset): | ||
""" Admin action to hard delete objects. """ | ||
if not self.has_delete_permission(request): | ||
raise PermissionDenied | ||
|
||
original_queryset = queryset.all() | ||
undeleted_queryset = queryset.filter(deleted__isnull=True) | ||
queryset = queryset.filter(deleted__isnull=False) | ||
|
||
if request.POST.get('post'): | ||
requested = original_queryset.count() | ||
changed = queryset.count() | ||
|
||
if changed: | ||
for obj in queryset: | ||
obj.delete(force_policy=HARD_DELETE) | ||
if requested > changed: | ||
self.message_user( | ||
request, | ||
"Successfully hard deleted %(count_changed)d of the " | ||
"%(count_requested)d selected %(items)s." % { | ||
"count_requested": requested, | ||
"count_changed": changed, | ||
"items": model_ngettext(self.opts, requested) | ||
}, | ||
messages.WARNING, | ||
) | ||
else: | ||
self.message_user( | ||
request, | ||
"Successfully hard deleted %(count)d %(items)s." % { | ||
"count": changed, | ||
"items": model_ngettext(self.opts, requested) | ||
}, | ||
messages.SUCCESS, | ||
) | ||
else: | ||
self.message_user( | ||
request, | ||
"No permission for hard delete. Execute soft delete first.", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would change this to something more explicit such as: "You need to soft delete first in order to hard delete an object." |
||
messages.ERROR | ||
) | ||
return None | ||
|
||
opts = self.model._meta | ||
if len(original_queryset) == 1: | ||
objects_name = force_text(opts.verbose_name) | ||
else: | ||
objects_name = force_text(opts.verbose_name_plural) | ||
title = "Are you sure?" | ||
|
||
deletable_objects, model_count, perms_needed, protected = self.get_deleted_objects(queryset, request) | ||
|
||
context = { | ||
'title': title, | ||
'objects_name': objects_name, | ||
'queryset': original_queryset, | ||
'deletable_queryset': queryset, | ||
'undeleted_queryset': undeleted_queryset, | ||
'opts': opts, | ||
'app_label': opts.app_label, | ||
'action_checkbox_name': admin.helpers.ACTION_CHECKBOX_NAME, | ||
'model_count': dict(model_count).items(), | ||
'deletable_objects': [deletable_objects], | ||
'perms_lacking': perms_needed, | ||
'protected': protected, | ||
'media': self.media, | ||
} | ||
|
||
return TemplateResponse( | ||
request, | ||
self.hard_delete_confirmation_template, | ||
context, | ||
) | ||
|
||
hard_delete.short_description = "Hard delete selected %(verbose_name_plural)s." |
31 changes: 31 additions & 0 deletions
31
safedelete/templates/safedelete/hard_delete_confirmation.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{% extends "admin/delete_selected_confirmation.html" %} | ||
{% load i18n l10n %} | ||
|
||
{% block object-tools %}{% endblock %} | ||
|
||
{% block content %} | ||
<div id="content-main"> | ||
<p>{% blocktrans %}Are you sure you want to hard delete the selected {{ objects_name }}?{% endblocktrans %}</p> | ||
{% include "admin/includes/object_delete_summary.html" %} | ||
<form action="" method="post">{% csrf_token %} | ||
<div> | ||
{% for obj in queryset %} | ||
<input type="hidden" name="{{ action_checkbox_name }}" value="{{ obj.pk|unlocalize }}" /> | ||
{% endfor %} | ||
|
||
<h2>{% translate "Objects" %}</h2> | ||
{% for deletable_object in deletable_objects %} | ||
<ul>{{ deletable_object|unordered_list }}</ul> | ||
{% endfor %} | ||
|
||
<p>{% blocktrans %}The following {{ objects_name }} are not soft deleted yet and cannot be hard deleted. {% endblocktrans %}</p> | ||
<ul>{{ undeleted_queryset|unordered_list }}</ul> | ||
|
||
<input type="hidden" name="action" value="hard_delete" /> | ||
<input type="hidden" name="post" value="yes" /> | ||
<input type="submit" value="{% trans "Yes, I'm sure" %}" /> | ||
<a href="#" class="button cancel-link">{% translate "No, take me back" %}</a> | ||
</div> | ||
</form> | ||
</div> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better to call the base QuerySet delete to delete in bulk, otherwise it could be very long.