actions.py 3.1 KB

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