2009-04-07 04:23:33 +08:00
|
|
|
"""
|
|
|
|
Built-in, globally-available admin actions.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.core.exceptions import PermissionDenied
|
|
|
|
from django.contrib.admin import helpers
|
|
|
|
from django.contrib.admin.util import get_deleted_objects, model_ngettext
|
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
|
2012-07-21 16:00:10 +08:00
|
|
|
from django.utils.encoding import force_text
|
2009-04-07 04:23:33 +08:00
|
|
|
from django.utils.translation import ugettext_lazy, ugettext as _
|
|
|
|
|
|
|
|
def delete_selected(modeladmin, request, queryset):
|
|
|
|
"""
|
|
|
|
Default action which deletes the selected objects.
|
|
|
|
|
|
|
|
This action first displays a confirmation page whichs shows all the
|
|
|
|
deleteable objects, or, if the user has no permission one of the related
|
|
|
|
childs (foreignkeys), a "permission denied" message.
|
|
|
|
|
|
|
|
Next, it delets all selected objects and redirects back to the change list.
|
|
|
|
"""
|
|
|
|
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.
|
2011-01-20 08:33:32 +08:00
|
|
|
deletable_objects, 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.
|
|
|
|
# Do the deletion and return a None to display the change list view again.
|
|
|
|
if request.POST.get('post'):
|
|
|
|
if perms_needed:
|
|
|
|
raise PermissionDenied
|
|
|
|
n = queryset.count()
|
|
|
|
if n:
|
|
|
|
for obj in queryset:
|
2012-07-21 16:00:10 +08:00
|
|
|
obj_display = force_text(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)
|
|
|
|
})
|
|
|
|
# Return None to display the change list page again.
|
|
|
|
return None
|
|
|
|
|
2011-01-20 09:00:47 +08:00
|
|
|
if len(queryset) == 1:
|
2012-07-21 16:00:10 +08:00
|
|
|
objects_name = force_text(opts.verbose_name)
|
2011-01-20 09:00:47 +08:00
|
|
|
else:
|
2012-07-21 16:00:10 +08:00
|
|
|
objects_name = force_text(opts.verbose_name_plural)
|
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?")
|
|
|
|
|
2009-04-07 04:23:33 +08:00
|
|
|
context = {
|
2011-01-20 09:00:47 +08:00
|
|
|
"title": title,
|
|
|
|
"objects_name": objects_name,
|
2010-02-26 21:17:43 +08:00
|
|
|
"deletable_objects": [deletable_objects],
|
2009-04-07 04:23:33 +08:00
|
|
|
'queryset': queryset,
|
|
|
|
"perms_lacking": perms_needed,
|
2011-01-20 08:33:32 +08:00
|
|
|
"protected": protected,
|
2009-04-07 04:23:33 +08:00
|
|
|
"opts": opts,
|
|
|
|
"app_label": app_label,
|
|
|
|
'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME,
|
|
|
|
}
|
|
|
|
|
|
|
|
# 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"
|
2011-04-23 02:17:16 +08:00
|
|
|
], context, current_app=modeladmin.admin_site.name)
|
2009-04-07 04:23:33 +08:00
|
|
|
|
|
|
|
delete_selected.short_description = ugettext_lazy("Delete selected %(verbose_name_plural)s")
|