Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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 3.2KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 = queryset.count()
  40. if n:
  41. for obj in queryset:
  42. obj_display = str(obj)
  43. modeladmin.log_deletion(request, obj, obj_display)
  44. modeladmin.delete_queryset(request, queryset)
  45. modeladmin.message_user(
  46. request,
  47. _("Successfully deleted %(count)d %(items)s.")
  48. % {"count": n, "items": model_ngettext(modeladmin.opts, n)},
  49. messages.SUCCESS,
  50. )
  51. # Return None to display the change list page again.
  52. return None
  53. objects_name = model_ngettext(queryset)
  54. if perms_needed or protected:
  55. title = _("Cannot delete %(name)s") % {"name": objects_name}
  56. else:
  57. title = _("Are you sure?")
  58. context = {
  59. **modeladmin.admin_site.each_context(request),
  60. "title": title,
  61. "subtitle": None,
  62. "objects_name": str(objects_name),
  63. "deletable_objects": [deletable_objects],
  64. "model_count": dict(model_count).items(),
  65. "queryset": queryset,
  66. "perms_lacking": perms_needed,
  67. "protected": protected,
  68. "opts": opts,
  69. "action_checkbox_name": helpers.ACTION_CHECKBOX_NAME,
  70. "media": modeladmin.media,
  71. }
  72. request.current_app = modeladmin.admin_site.name
  73. # Display the confirmation page
  74. return TemplateResponse(
  75. request,
  76. modeladmin.delete_selected_confirmation_template
  77. or [
  78. "admin/%s/%s/delete_selected_confirmation.html"
  79. % (app_label, opts.model_name),
  80. "admin/%s/delete_selected_confirmation.html" % app_label,
  81. "admin/delete_selected_confirmation.html",
  82. ],
  83. context,
  84. )