2009-04-07 04:23:33 +08:00
|
|
|
"""
|
|
|
|
Built-in, globally-available admin actions.
|
|
|
|
"""
|
|
|
|
|
2013-04-08 19:54:03 +08:00
|
|
|
from django.contrib import messages
|
2009-04-07 04:23:33 +08:00
|
|
|
from django.contrib.admin import helpers
|
2013-09-17 00:52:05 +08:00
|
|
|
from django.contrib.admin.utils import get_deleted_objects, model_ngettext
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.core.exceptions import PermissionDenied
|
2010-11-10 00:46:42 +08:00
|
|
|
from django.db import router
|
2011-04-23 02:17:16 +08:00
|
|
|
from django.template.response import TemplateResponse
|
2017-01-27 03:58:33 +08:00
|
|
|
from django.utils.translation import gettext as _, gettext_lazy
|
2009-04-07 04:23:33 +08:00
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2009-04-07 04:23:33 +08:00
|
|
|
def delete_selected(modeladmin, request, queryset):
|
|
|
|
"""
|
|
|
|
Default action which deletes the selected objects.
|
|
|
|
|
2015-12-03 07:55:50 +08:00
|
|
|
This action first displays a confirmation page which shows all the
|
2017-05-25 17:54:51 +08:00
|
|
|
deletable objects, or, if the user has no permission one of the related
|
2009-04-07 04:23:33 +08:00
|
|
|
childs (foreignkeys), a "permission denied" message.
|
|
|
|
|
2013-06-20 07:45:28 +08:00
|
|
|
Next, it deletes all selected objects and redirects back to the change list.
|
2009-04-07 04:23:33 +08:00
|
|
|
"""
|
|
|
|
opts = modeladmin.model._meta
|
|
|
|
app_label = opts.app_label
|
|
|
|
|
|
|
|
# Check that the user has delete permission for the actual model
|
|
|
|
if not modeladmin.has_delete_permission(request):
|
|
|
|
raise PermissionDenied
|
|
|
|
|
2010-11-10 00:46:42 +08:00
|
|
|
using = router.db_for_write(modeladmin.model)
|
|
|
|
|
2009-04-07 04:23:33 +08:00
|
|
|
# Populate deletable_objects, a data structure of all related objects that
|
|
|
|
# will also be deleted.
|
2014-08-14 19:17:52 +08:00
|
|
|
deletable_objects, model_count, perms_needed, protected = get_deleted_objects(
|
2010-11-10 00:46:42 +08:00
|
|
|
queryset, opts, request.user, modeladmin.admin_site, using)
|
2009-04-07 04:23:33 +08:00
|
|
|
|
|
|
|
# The user has already confirmed the deletion.
|
2017-02-22 04:22:33 +08:00
|
|
|
# Do the deletion and return None to display the change list view again.
|
2016-03-28 14:28:51 +08:00
|
|
|
if request.POST.get('post') and not protected:
|
2009-04-07 04:23:33 +08:00
|
|
|
if perms_needed:
|
|
|
|
raise PermissionDenied
|
|
|
|
n = queryset.count()
|
|
|
|
if n:
|
|
|
|
for obj in queryset:
|
2017-04-22 01:52:26 +08:00
|
|
|
obj_display = str(obj)
|
2009-04-07 04:23:33 +08:00
|
|
|
modeladmin.log_deletion(request, obj, obj_display)
|
|
|
|
queryset.delete()
|
|
|
|
modeladmin.message_user(request, _("Successfully deleted %(count)d %(items)s.") % {
|
|
|
|
"count": n, "items": model_ngettext(modeladmin.opts, n)
|
2013-04-08 19:54:03 +08:00
|
|
|
}, messages.SUCCESS)
|
2009-04-07 04:23:33 +08:00
|
|
|
# Return None to display the change list page again.
|
|
|
|
return None
|
|
|
|
|
2017-01-31 04:38:55 +08:00
|
|
|
objects_name = model_ngettext(queryset)
|
2011-01-20 09:00:47 +08:00
|
|
|
|
|
|
|
if perms_needed or protected:
|
|
|
|
title = _("Cannot delete %(name)s") % {"name": objects_name}
|
|
|
|
else:
|
|
|
|
title = _("Are you sure?")
|
|
|
|
|
2014-11-23 22:08:34 +08:00
|
|
|
context = dict(
|
2015-01-01 05:25:00 +08:00
|
|
|
modeladmin.admin_site.each_context(request),
|
2014-11-23 22:08:34 +08:00
|
|
|
title=title,
|
2017-04-22 01:52:26 +08:00
|
|
|
objects_name=str(objects_name),
|
2014-11-23 22:08:34 +08:00
|
|
|
deletable_objects=[deletable_objects],
|
2015-02-25 20:15:56 +08:00
|
|
|
model_count=dict(model_count).items(),
|
2014-11-23 22:08:34 +08:00
|
|
|
queryset=queryset,
|
|
|
|
perms_lacking=perms_needed,
|
|
|
|
protected=protected,
|
|
|
|
opts=opts,
|
|
|
|
action_checkbox_name=helpers.ACTION_CHECKBOX_NAME,
|
2015-11-23 18:46:19 +08:00
|
|
|
media=modeladmin.media,
|
2014-11-23 22:08:34 +08:00
|
|
|
)
|
2009-04-07 04:23:33 +08:00
|
|
|
|
2014-12-15 00:48:51 +08:00
|
|
|
request.current_app = modeladmin.admin_site.name
|
|
|
|
|
2009-04-07 04:23:33 +08:00
|
|
|
# Display the confirmation page
|
2011-04-23 02:17:16 +08:00
|
|
|
return TemplateResponse(request, modeladmin.delete_selected_confirmation_template or [
|
2013-02-05 17:16:07 +08:00
|
|
|
"admin/%s/%s/delete_selected_confirmation.html" % (app_label, opts.model_name),
|
2009-04-07 04:23:33 +08:00
|
|
|
"admin/%s/delete_selected_confirmation.html" % app_label,
|
|
|
|
"admin/delete_selected_confirmation.html"
|
2014-12-15 00:48:51 +08:00
|
|
|
], context)
|
2009-04-07 04:23:33 +08:00
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2017-01-27 03:58:33 +08:00
|
|
|
delete_selected.short_description = gettext_lazy("Delete selected %(verbose_name_plural)s")
|