You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

actions.py 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """
  2. Built-in, globally-available admin actions.
  3. """
  4. from django.contrib import messages
  5. from django.contrib.admin import helpers
  6. from django.contrib.admin.utils import model_ngettext
  7. from django.core.exceptions import PermissionDenied
  8. from django.template.response import TemplateResponse
  9. from django.utils.translation import gettext as _, gettext_lazy
  10. def delete_selected(modeladmin, request, queryset):
  11. """
  12. Default action which deletes the selected objects.
  13. This action first displays a confirmation page which shows all the
  14. deletable objects, or, if the user has no permission one of the related
  15. childs (foreignkeys), a "permission denied" message.
  16. Next, it deletes all selected objects and redirects back to the change list.
  17. """
  18. opts = modeladmin.model._meta
  19. app_label = opts.app_label
  20. # Populate deletable_objects, a data structure of all related objects that
  21. # will also be deleted.
  22. deletable_objects, model_count, perms_needed, protected = modeladmin.get_deleted_objects(queryset, request)
  23. # The user has already confirmed the deletion.
  24. # Do the deletion and return None to display the change list view again.
  25. if request.POST.get('post') and not protected:
  26. if perms_needed:
  27. raise PermissionDenied
  28. n = queryset.count()
  29. if n:
  30. for obj in queryset:
  31. obj_display = str(obj)
  32. modeladmin.log_deletion(request, obj, obj_display)
  33. modeladmin.delete_queryset(request, queryset)
  34. modeladmin.message_user(request, _("Successfully deleted %(count)d %(items)s.") % {
  35. "count": n, "items": model_ngettext(modeladmin.opts, n)
  36. }, messages.SUCCESS)
  37. # Return None to display the change list page again.
  38. return None
  39. objects_name = model_ngettext(queryset)
  40. if perms_needed or protected:
  41. title = _("Cannot delete %(name)s") % {"name": objects_name}
  42. else:
  43. title = _("Are you sure?")
  44. context = {
  45. **modeladmin.admin_site.each_context(request),
  46. 'title': title,
  47. 'objects_name': str(objects_name),
  48. 'deletable_objects': [deletable_objects],
  49. 'model_count': dict(model_count).items(),
  50. 'queryset': queryset,
  51. 'perms_lacking': perms_needed,
  52. 'protected': protected,
  53. 'opts': opts,
  54. 'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME,
  55. 'media': modeladmin.media,
  56. }
  57. request.current_app = modeladmin.admin_site.name
  58. # Display the confirmation page
  59. return TemplateResponse(request, modeladmin.delete_selected_confirmation_template or [
  60. "admin/%s/%s/delete_selected_confirmation.html" % (app_label, opts.model_name),
  61. "admin/%s/delete_selected_confirmation.html" % app_label,
  62. "admin/delete_selected_confirmation.html"
  63. ], context)
  64. delete_selected.allowed_permissions = ('delete',)
  65. delete_selected.short_description = gettext_lazy("Delete selected %(verbose_name_plural)s")