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.
 
 
 
 

89 lines
3.2 KiB

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