viewhelpers.formview.deleteview — Implements the preview+confirm+delete workflow¶
When to use¶
Use this when you need a view to delete a single item. Your users will get a preview of the item, and the option to confirm the delete or cancel the delete.
Usage¶
The django_cradmin.viewhelpers.formview.WithinRoleDeleteView
is just a subclass of
django.views.generic.DeleteView
, so you use it just like the Django DeleteView.
Very basic example (no security)¶
Lets say you have the following Page
-model in models.py
:
from django.conf import settings
class Page(models.Model):
title = models.CharField(max_length=100)
body = models.TextField()
def __str__(self):
return self.title
Then you would create a PageDeleteView and register it in an app with the following code:
from django_cradmin import viewhelpers
from django_cradmin import crapp
class PageDeleteView(viewhelpers.formview.WithinRoleDeleteView):
"""
View used to delete existing pages.
"""
model = Page
class App(crapp.App):
appurls = [
# .. other views
crapp.Url(r'^delete/(?P<pk>\d+)$',
PageDeleteView.as_view(),
name="delete"),
]
Securing the basic example¶
The basic example lets anyone with access to the cradmin delete any page. You normally have multiple roles,
and each role will have access to a subset of objects. Lets add a role class named Site
, and extend our
Page
-model with a foreign-key to that site. Our new models.py
looks like this:
from django.conf import settings
from django.db import models
class Site(models.Model):
name = models.CharField(max_length=100)
admins = models.ManyToManyField(settings.AUTH_USER_MODEL)
class Page(models.Model):
site = models.ForeignKey(Site)
title = models.CharField(max_length=100)
body = models.TextField()
def __str__(self):
return self.title
We make the Site the roleclass on our MyCrAdminInstance
:
from django_cradmin import crinstance
class MyCrAdminInstance(crinstance.BaseCrAdminInstance):
roleclass = Site
# Other stuff documented elsewhere
We only want to allow deleting within the current Site (current role), so we replace model
on the WithinRoleDeleteView with a get_queryset_for_role
-method that limits the pages to
pages within the current site:
class PageDeleteView(viewhelpers.formview.WithinRoleDeleteView):
"""
View used to delete existing pages.
"""
def get_queryset_for_role(self):
return Page.objects.filter(site=self.request.cradmin_role)
API docs¶
-
class
DeleteViewMixin
¶ Bases:
object
Delete view mixin.
Note
You should import this class with
from django_cradmin import viewhelpers
, and refer to it usingviewhelpers.formview.DeleteViewMixin
.-
get_pagetitle
()¶ Get the page title (the title tag).
Defaults to
Delete <verbose_name model>
.
-
get_action_label
()¶ The action we are performing.
Used as the prefix of the page title (see
get_pagetitle()
), and as the default forget_delete_button_label()
.
The label of the delete button.
Defaults to
get_action_label()
.
The label of the cancel button.
Defaults to
get_action_label()
.
-
get_object_preview
()¶ The preview of the object. Used when asking the user if he/she wants to delete the current object.
-
get_confirm_message
()¶ Get the confirm message shown in the focus area of the view.
-
get_success_url
()¶ Get the URL to go to if object was deleted.
Defaults to the INDEX view of the current app.
-
get_success_message
(object_preview)¶ Override this to provide a success message.
Used by
add_success_messages()
.
-
add_success_messages
(object_preview)¶ Called after the object has been deleted.
Defaults to add
get_success_message()
as a django messages success message ifget_success_message()
returns anything.You can override this to add multiple messages or to show messages in some other way.
-
-
class
WithinRoleDeleteView
(**kwargs)¶ Bases:
django_cradmin.viewhelpers.mixins.QuerysetForRoleMixin
,django_cradmin.viewhelpers.formview.deleteview.DeleteViewMixin
,django.views.generic.edit.DeleteView
,django_cradmin.viewhelpers.mixins.CommonCradminViewMixin
,django_cradmin.javascriptregistry.viewmixin.WithinRoleViewMixin
Delete view with the correct context data and sane base template for views where we have a cradmin role.
Note
You should import this class with
from django_cradmin import viewhelpers
, and refer to it usingviewhelpers.formview.WithinRoleDeleteView
.Constructor. Called in the URLconf; can contain helpful extra keyword arguments, and other things.
-
get_context_data
(**kwargs)¶ Insert the single object into the context dict.
-