Hello site
[auf_framonde.git] / eggs / Django-1.4.5-py2.7.egg / django / contrib / admin / actions.py
1 """
2 Built-in, globally-available admin actions.
3 """
4
5 from django.core.exceptions import PermissionDenied
6 from django.contrib.admin import helpers
7 from django.contrib.admin.util import get_deleted_objects, model_ngettext
8 from django.db import router
9 from django.template.response import TemplateResponse
10 from django.utils.encoding import force_unicode
11 from django.utils.translation import ugettext_lazy, ugettext as _
12
13 def delete_selected(modeladmin, request, queryset):
14 """
15 Default action which deletes the selected objects.
16
17 This action first displays a confirmation page whichs shows all the
18 deleteable objects, or, if the user has no permission one of the related
19 childs (foreignkeys), a "permission denied" message.
20
21 Next, it delets all selected objects and redirects back to the change list.
22 """
23 opts = modeladmin.model._meta
24 app_label = opts.app_label
25
26 # Check that the user has delete permission for the actual model
27 if not modeladmin.has_delete_permission(request):
28 raise PermissionDenied
29
30 using = router.db_for_write(modeladmin.model)
31
32 # Populate deletable_objects, a data structure of all related objects that
33 # will also be deleted.
34 deletable_objects, perms_needed, protected = get_deleted_objects(
35 queryset, opts, request.user, modeladmin.admin_site, using)
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 if len(queryset) == 1:
55 objects_name = force_unicode(opts.verbose_name)
56 else:
57 objects_name = force_unicode(opts.verbose_name_plural)
58
59 if perms_needed or protected:
60 title = _("Cannot delete %(name)s") % {"name": objects_name}
61 else:
62 title = _("Are you sure?")
63
64 context = {
65 "title": title,
66 "objects_name": objects_name,
67 "deletable_objects": [deletable_objects],
68 'queryset': queryset,
69 "perms_lacking": perms_needed,
70 "protected": protected,
71 "opts": opts,
72 "app_label": app_label,
73 'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME,
74 }
75
76 # Display the confirmation page
77 return TemplateResponse(request, modeladmin.delete_selected_confirmation_template or [
78 "admin/%s/%s/delete_selected_confirmation.html" % (app_label, opts.object_name.lower()),
79 "admin/%s/delete_selected_confirmation.html" % app_label,
80 "admin/delete_selected_confirmation.html"
81 ], context, current_app=modeladmin.admin_site.name)
82
83 delete_selected.short_description = ugettext_lazy("Delete selected %(verbose_name_plural)s")