我们从Python开源项目中,提取了以下37个代码示例,用于说明如何使用django.contrib.admin.utils.get_deleted_objects()。
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_text(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, model_count, self.perms_needed, self.protected) = get_deleted_objects( [self.obj], self.opts, self.request.user, self.admin_site, using)
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, model_count, self.perms_needed, self.protected) = get_deleted_objects( [self.obj], self.opts, self.request.user, self.admin_site, using)
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': 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, model_count, self.perms_needed, self.protected) = get_deleted_objects( [self.obj], self.opts, self.request.user, self.admin_site, using)
def _folder_form_delete(self, request, obj): if not self.has_delete_permission(request, obj): raise PermissionDenied using = router.db_for_write(obj.__class__) # Populate deleted_objects, a data structure of all related objects # that will also be deleted. (deleted_objects, model_count, perms_needed, protected) = get_deleted_objects( # noqa [obj], obj._meta, request.user, self.admin_site, using) if protected or perms_needed: self.message_user( request, _('Cannot delete %(name)s') % {'name': obj._meta.verbose_name}, messages.ERROR, ) elif len(deleted_objects) > 1: self.message_user( request, _('Cannot delete %(name)s because of related objects (%(related)s)') % { # noqa 'name': obj._meta.verbose_name, 'related': ', '.join( '%s %s' % (count, name) for name, count in model_count.items() ), }, messages.ERROR, ) else: obj.delete() self.message_user( request, _('The folder "%s" was deleted successfully.') % obj, messages.SUCCESS) return self.redirect_to_folder(request, obj.parent_id)
def clear_placeholder(self, request, placeholder_id): placeholder = get_object_or_404(Placeholder, pk=placeholder_id) if not self.has_clear_placeholder_permission(request, placeholder): return HttpResponseForbidden(force_text(_("You do not have permission to clear this placeholder"))) language = request.GET.get('language', None) plugins = placeholder.get_plugins(language) opts = Placeholder._meta using = router.db_for_write(Placeholder) app_label = opts.app_label deleted_objects, __, perms_needed, protected = get_deleted_objects( plugins, opts, request.user, self.admin_site, using) obj_display = force_text(placeholder) if request.POST: # The user has already confirmed the deletion. if perms_needed: return HttpResponseForbidden(force_text(_("You do not have permission to clear this placeholder"))) self.log_deletion(request, placeholder, obj_display) placeholder.clear(language) self.message_user(request, _('The placeholder "%(obj)s" was cleared successfully.') % { 'obj': force_text(obj_display)}) self.post_clear_placeholder(request, placeholder) return HttpResponseRedirect(admin_reverse('index', current_app=self.admin_site.name)) if perms_needed or protected: title = _("Cannot delete %(name)s") % {"name": obj_display} else: title = _("Are you sure?") context = { "title": title, "object_name": _("placeholder"), "object": placeholder, "deleted_objects": deleted_objects, "perms_lacking": perms_needed, "protected": protected, "opts": opts, "app_label": app_label, } return TemplateResponse(request, "admin/cms/page/plugin/delete_confirmation.html", context, current_app=self.admin_site.name)
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, model_count, 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_text(self.opts.verbose_name) else: objects_name = force_text(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 self.get_template_list('views/model_delete_selected_confirm.html'), context, current_app=self.admin_site.name)
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, model_count, 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_text(self.opts.verbose_name) else: objects_name = force_text(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 self.get_template_list('views/model_delete_selected_confirm.html'), context)
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, model_count, 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 self.get_template_list('views/model_delete_selected_confirm.html'), context)
def delete_plugin(self, request, plugin_id): plugin = get_object_or_404( CMSPlugin.objects.select_related('placeholder'), pk=plugin_id) if not self.has_delete_plugin_permission(request, plugin): return HttpResponseForbidden(force_text( _("You do not have permission to delete this plugin"))) plugin_cms_class = plugin.get_plugin_class() plugin_class = plugin_cms_class.model opts = plugin_class._meta using = router.db_for_write(plugin_class) app_label = opts.app_label deleted_objects, __, perms_needed, protected = get_deleted_objects( [plugin], opts, request.user, self.admin_site, using) if request.POST: # The user has already confirmed the deletion. if perms_needed: raise PermissionDenied(_("You do not have permission to delete this plugin")) obj_display = force_text(plugin) self.log_deletion(request, plugin, obj_display) plugin.delete() self.message_user(request, _('The %(name)s plugin "%(obj)s" was deleted successfully.') % { 'name': force_text(opts.verbose_name), 'obj': force_text(obj_display)}) self.post_delete_plugin(request, plugin) return HttpResponseRedirect(admin_reverse('index', current_app=self.admin_site.name)) plugin_name = force_text(plugin_pool.get_plugin(plugin.plugin_type).name) if perms_needed or protected: title = _("Cannot delete %(name)s") % {"name": plugin_name} else: title = _("Are you sure?") context = { "title": title, "object_name": plugin_name, "object": plugin, "deleted_objects": deleted_objects, "perms_lacking": perms_needed, "protected": protected, "opts": opts, "app_label": app_label, } request.current_app = self.admin_site.name return TemplateResponse( request, "admin/cms/page/plugin/delete_confirmation.html", context )
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, model_count, 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 self.get_template_list('views/model_delete_selected_confirm.html'), context, current_app=self.admin_site.name)