View Classes#
ViewSets#
- class ModelViewSet(model: Optional[Type[ViewSetMixin]] = None, url_prefix: Optional[str] = None, url_namespace: Optional[str] = None, **kwargs)[source]#
Bases:
object
A viewset to allow listing, creating, editing and deleting model instances.
- create_view_class#
The view class to use for the index view; must be a subclass of
wildewidgets.views.generic.CreateView
.alias of
CreateView
- delete_view_class#
The view class to use for the index view; must be a subclass of
wildewidgets.views.generic.DeleteView
.alias of
DeleteView
- index_table_class#
The
wildewidgets.widgets.tables.base.BaseDataTable
subclass to use for the listing tablealias of
LookupModelTable
- index_view_class#
The view class to use for the index view; must be a subclass of
wildewidgets.views.generic.IndexView
.alias of
IndexView
- table_ajax_view_class#
The view class to use as the AJAX JSON endpoint for the dataTable; must be a subclass of
wildewidgets.views.generic.TableAJAXView
.alias of
TableAJAXView
- table_bulk_action_view_class#
The view class to use for the index view; must be a subclass of
wildewidgets.views.generic.CreateView
.alias of
TableBulkActionView
- update_view_class#
The view class to use for the index view; must be a subclass of
wildewidgets.views.generic.UpdateView
.alias of
UpdateView
- get_breadcrumbs(view_name: str) Optional[BreadcrumbBlock] [source]#
- get_url_name(view_name: str, namespace: bool = True) str [source]#
Returns the (possibly) namespaced URL name for the given view.
- Parameters:
view_name –
- breadcrumbs_class: Optional[Type[BreadcrumbBlock]] = None#
Set this to your
wildewidgets.widgets.navigation.BreadcrumbBlock
class to manage breadcrumbs automatically.
- property create_view#
- property delete_view#
- index_table_kwargs: Dict[str, Any] = {'buttons': True, 'page_length': 50, 'striped': True}#
A dictionary that we will use as the
**kwargs
for the constructor oftable_class
- property index_view#
- model: Optional[Type[ViewSetMixin]] = None#
The model we’re describing in this view. It must have
wildewidgets.models.ViewSetMixin
in its class heirarchy
The navbar to use. We’ll highlight our verbose name in any of the menus within the navbar
- property table_ajax_view#
- property table_bulk_action_view#
- property update_view#
View Mixins#
- class GenericViewMixin(*args, **kwargs)[source]#
Bases:
object
This class based view mixin adds some methods and attributes that our other generic views use.
- get_breadcrumbs() Optional[BreadcrumbBlock] [source]#
Add our title to the breadcrumbs and return the updated block.
- Returns:
The breadcrumb block to use
- get_client_ip() str [source]#
Return our user’s IP address. This will either be the leftmost IP address in our
X-Forwarded-For
header, or theREMOTE_ADDR
header.- Returns:
Our user’s IP address
Return the name of the main menu item to set active, which is the the appropriately capitalized
verbose_name_plural
for our model.- Returns:
The name of the main menu item to set active.
- get_permissions_required() List[str] [source]#
Overrides
wildewidgets.views.permission.PermissionRequiredMixin.get_permissions_required
.Expand the permissions listed in
required_model_permissions
into full Django permission strings (e.g.appname.add_modelname
and add them to the list of permissions already defined by our superclass.- Returns:
The list of Django user permission strings
- breadcrumbs: Optional[BreadcrumbBlock] = None#
Set this to your
wildewidgets.widgets.navigation.BreadcrumbBlock
to manage breadcrumbs automatically.
- property logging_extra: str#
Build some extra stuff to append to our logging statements:
add the user’s remote_ip
add the user’s username
add the pk of the object, if any
- Returns:
A string to append to our log message
- property model_logger_name: str#
Return a string we can use to identify this model in a log message.
- class GenericDatatableMixin[source]#
Bases:
object
- table_class#
The
wildewidgets.widgets.tables.base.BaseDataTable
subclass to use for the listing tablealias of
LookupModelTable
- get_table(*args, **kwargs) BaseDataTable [source]#
- get_table_kwargs() Dict[str, Any] [source]#
Return a dict that will be used as the
**kwargs
for constructing ourtable_class
instance.If there are no bulk_action form actions defined, define one for bulk deleting rows.
- Returns:
Constructor kwargs for
table_class
- table_kwargs: Dict[str, Any] = {'buttons': True, 'page_length': 25, 'striped': True}#
A dictionary that we will use as the
**kwargs
for the constructor oftable_class
- class JSONResponseMixin[source]#
Bases:
object
- get_json_response(content: Any, **httpresponse_kwargs) HttpResponse [source]#
Construct an HttpResponse object.
- class StandardWidgetMixin[source]#
Bases:
object
A class based view mixin for views that use a standard widget template. This is used with a template-less design.
The template used by your derived class should include at least the following:
{% extends "<your_base_template>.html" %} {% load wildewidgets %} {% block title %}{{page_title}}{% endblock %} {% block breadcrumb-items %} {% if breadcrumbs %} {% wildewidgets breadcrumbs %} {% endif %} {% endblock %} {% block content %} {% wildewidgets content %} {% endblock %}
The
content
block is where the content of the page is rendered. Thebreadcrumbs
block is where the breadcrumbs are rendered.An example derived class, which will be used by most of the views in the project:
class DemoStandardMixin(StandardWidgetMixin, NavbarMixin): template_name='core/standard.html' menu_class = DemoMenu
The
DemoStandardMixin
class is used in the following way:class HomeView(DemoStandardMixin, TemplateView): menu_item = 'Home' def get_content(self): return HomeBlock() def get_breadcrumbs(self): breadcrumbs = DemoBaseBreadcrumbs() breadcrumbs.add_breadcrumb('Home') return breadcrumbs
- get_breadcrumbs() Optional[BreadcrumbBlock] [source]#
- class PermissionRequiredMixin[source]#
Bases:
AccessMixin
This class-based view mixin grants or denies access to a view based on a user’s group membership or specific permisisons.
Django superusers are always authorized
Anonymous users are never authorized
If
request.user
is in one of the groups returned byget_groups_required
, they are authorized.Failing that, if
request.user
has one of the permissions returned byget_permissions_required
, they are authorized.Additionally, if
model
andrequired_model_permissions
are both set, extend what we got fromget_permissions_required
with what we get fromget_model_permissions
- check_permissions(groups_required: Optional[List[Group]] = None, permissions_required: Optional[List[str]] = None) bool [source]#
Check if the current user is authorized for our view.
Django superusers are always authorized
Anonymous users are never authorized
If
request.user
is in one of the groups returned byget_groups_required
, they are authorized.Failing that, if
request.user
has one of the permissions returned byget_permissions_required
, they are authorized.Additionally, if
model
andrequired_model_permissions
are both set, extend what we got fromget_permissions_required
with what we get fromget_model_permissions
- Parameters:
request – the current request
- Keyword Arguments:
groups_required – test versus these groups instead of what
get_groups_required
returnspermissions_required – test versus these permissions instead of what
get_permissions_required
returns
- Returns:
Return
True
if the user is authorized,False
otherwise.
- get_model_permissions(model: Model, perms: List[str]) List[str] [source]#
Given a list of simple strings like
view
,add
,change
, etc. and a Djangodjango.db.models.Model
, return the list of actual permissions names that would apply to that model, e.g. for a modelFoo
in the django appcore
:>>> PermissionMixin.get_model_permissions(Foo, ['add', 'change']) ['core.add_foo', 'core.change_foo']
- Parameters:
model – the model for which to build permission strings
perms – the list of short permissions
- Returns:
The fully formed permission strings
- groups_required: Optional[List[str]] = None#
Users who are members of the named Django auth groups will be permitted access to the view, and those who are not will be denied access.
- model: Optional[Type[Model]]#
Used in conjunction with
required_model_permissions
, this is the model we want to act on. This should be set by our our subclass, which is why it is just a type declaration here. This is used byget_model_permissions
AJAX Views#
- class JSONResponseView(**kwargs)[source]#
Bases:
JSONResponseMixin
,TemplateView
Generic Views#
- class IndexView(*args, **kwargs)[source]#
Bases:
LoginRequiredMixin
,PermissionRequiredMixin
,GenericViewMixin
,GenericDatatableMixin
,NavbarMixin
,StandardWidgetMixin
,TemplateView
- class TableAJAXView(*args, **kwargs)[source]#
Bases:
LoginRequiredMixin
,PermissionRequiredMixin
,WidgetInitKwargsMixin
,GenericDatatableMixin
,View
- class TableBulkActionView(*args, **kwargs)[source]#
Bases:
LoginRequiredMixin
,PermissionRequiredMixin
,GenericViewMixin
,GenericDatatableMixin
,MessageMixin
,TableActionFormView
- class CreateView(*args, **kwargs)[source]#
Bases:
LoginRequiredMixin
,PermissionRequiredMixin
,FormInvalidMessageMixin
,FormValidMessageMixin
,GenericViewMixin
,NavbarMixin
,StandardWidgetMixin
,CreateView
- layout_widget#
Use this
AbstractFormPageLayout
subclass to render our pagealias of
FormPageLayout
- get_form_invalid_message()[source]#
Validate that form_invalid_message is set and is either a unicode or str object.
- class UpdateView(*args, **kwargs)[source]#
Bases:
LoginRequiredMixin
,PermissionRequiredMixin
,FormInvalidMessageMixin
,FormValidMessageMixin
,GenericViewMixin
,NavbarMixin
,StandardWidgetMixin
,UpdateView
- layout_widget#
Use this
AbstractFormPageLayout
subclass to render our pagealias of
FormPageLayout
- get_form_invalid_message()[source]#
Validate that form_invalid_message is set and is either a unicode or str object.
- class DeleteView(*args, **kwargs)[source]#
Bases:
LoginRequiredMixin
,PermissionRequiredMixin
,GenericViewMixin
,FormInvalidMessageMixin
,FormValidMessageMixin
,BaseDeleteView
- form_invalid(form: BaseModelForm) HttpResponse [source]#
If the form is invalid, render the invalid form.
- get_form_invalid_message()[source]#
Validate that form_invalid_message is set and is either a unicode or str object.
- get_form_valid_message()[source]#
Validate that form_valid_message is set and is either a unicode or str object.
- model: Optional[Type[ViewSetMixin]] = None#
Used in conjunction with
required_model_permissions
, this is the model we want to act on. This should be set by our our subclass, which is why it is just a type declaration here. This is used byget_model_permissions