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