Example #1
0
    def do_action(self, queryset):
        # Check that the user has delete permission for the actual model
        if not self.has_delete_permission():
            raise PermissionDenied

        using = router.db_for_write(self.model)

        # Populate deletable_objects, a data structure of all related objects that
        # will also be deleted.
        deletable_objects, perms_needed, protected = get_deleted_objects(
            queryset, self.opts, self.user, self.admin_site, using
        )

        # The user has already confirmed the deletion.
        # Do the deletion and return a None to display the change list view again.
        if self.request.POST.get("post"):
            if perms_needed:
                raise PermissionDenied
            self.delete_models(queryset)
            # Return None to display the change list page again.
            return None

        if len(queryset) == 1:
            objects_name = force_unicode(self.opts.verbose_name)
        else:
            objects_name = force_unicode(self.opts.verbose_name_plural)

        if perms_needed or protected:
            title = _("Cannot delete %(name)s") % {"name": objects_name}
        else:
            title = _("Are you sure?")

        context = self.get_context()
        context.update(
            {
                "title": title,
                "objects_name": objects_name,
                "deletable_objects": [deletable_objects],
                "queryset": queryset,
                "perms_lacking": perms_needed,
                "protected": protected,
                "opts": self.opts,
                "app_label": self.app_label,
                "action_checkbox_name": ACTION_CHECKBOX_NAME,
            }
        )

        # Display the confirmation page
        return TemplateResponse(
            self.request,
            self.delete_selected_confirmation_template
            or [
                "admin/%s/%s/delete_selected_confirmation.html" % (self.app_label, self.opts.object_name.lower()),
                "admin/%s/delete_selected_confirmation.html" % self.app_label,
                "admin/delete_selected_confirmation.html",
            ],
            context,
            current_app=self.admin_site.name,
        )
Example #2
0
    def init_request(self, object_id, *args, **kwargs):
        "The 'delete' admin view for this model."
        self.obj = self.get_object(unquote(object_id))

        if not self.has_delete_permission(self.obj):
            raise PermissionDenied

        if self.obj is None:
            raise Http404(_('%(name)s object with primary key %(key)r does not exist.') % {'name': force_unicode(self.opts.verbose_name), 'key': escape(object_id)})

        using = router.db_for_write(self.model)

        # Populate deleted_objects, a data structure of all related objects that
        # will also be deleted.
        (self.deleted_objects, self.perms_needed, self.protected) = get_deleted_objects(
            [self.obj], self.opts, self.request.user, self.admin_site, using)