Package

daf.actions

class daf.actions.ActionMeta[source]

A metaclass for validating and registering actions

class daf.actions.Action[source]

The core Action class.

Given an app_label and callable, the Action class automatically generates attributes that can be overridden by a user. These attributes influence every interface built directly from the Action. Change attributes on the Action object to affect every interface.

name

The identifying name of the action

app_label = ''

The app to which the action belongs.

uri

The URI is the unique identifier for the action.

url_name

The default URL name for URL-based interfaces

url_path

The default URL name for URL-based interfaces

permission_codename

Returns the name of the permission associate with the action

permission_uri

The full permission URI, which includes the “daf” app label under which all DAF permissions are saved

display_name

The display name is used to render UI headings and other elements

success_message

The success message displayed after successful action runs

classmethod get_success_message(args, results)[source]

Obtains a success message based on callable args and results

success_url = '.'

The URL one goes to after a successful action

classmethod get_success_url(args, results)[source]

Obtain a success url based on callable args and results

callable = None

The main action callable

wrapper()

The wrapper around the callable. Attach exception metadata by default for interoperability with other tools

classmethod check_class_definition()[source]

Verifies all properties have been filled out properly for the action class. Called by the metaclass only on concrete actions

class daf.actions.ModelAction[source]

An action associated with a model.

Requires that the model attribute point to the Django Model class associated with the action.

Includes all of the core properties of Action, but also defines other properties and creates automatic default values for others:

model = None

The model the action is associated with

app_label

The app label to which this action belongs

model_meta

The model._meta instance

queryset

The main queryset, if any, the action is associated with

classmethod check_class_definition()[source]

Verifies all properties have been filled out properly for the action class. Called by the metaclass only on concrete actions

class daf.actions.ObjectAction[source]

An action associated with a single model object.

Similar to ModelAction, an ObjectAction updates a single model object. It requires an object_arg attribute which specifies which argument of callable is the model object.

ObjectAction exposes an object variable that is automatically included as a default argument when running the wrapped callable. Allowing your function to take an object parameter will make it work seamlessly with object actions.

By default, the wrapper for ObjectAction automatically:

  1. Parametrizes the run of the individual callable over multiple objects if the objects parameter is passed to the callable.

  2. Traps errors on each parametrized run of the callable and raises all trapped errors as one django.core.exceptions.ValidationError if more than one error is trapped in a parameterized run.

  3. Automatically maps the object argument to the argument identified by the object_arg attribute.

  4. Wraps everything in a transaction and applies a select_for_update to the queryset if select_for_update is supplied.

object_arg = None

The name of the object arg for the action callable

select_for_update = ['self']

Select_for_update parameters if the action is atomic

classmethod check_class_definition()[source]

Verifies all properties have been filled out properly for the action class. Called by the metaclass only on concrete actions

class daf.actions.ObjectsAction[source]

An action associated with multiple model objects.

The action is similar to ObjectAction except one must define an objects_arg attribute that tells daf which parameter to callable takes the list of objects. The callable must work with a list of objects at once.

By default, the wrapper attribute ensures passing an object argument will be automatically expanded into a single-element list (ensuring interoperability with object views). In contrast to ObjectAction, ObjectsAction cannot trap and re-raise multiple errors since it is up to the author of the bulk callable to handle raising multiple failures at once. ObjectsAction is intended to provide engineers the flexibility to optimize bulk routines if the automatic parametrization of ObjectAction is insufficient for their needs.

objects_arg = None

The name of the objects arg for the action callable

select_for_update = ['self']

Select_for_update parameters if the action is atomic

classmethod check_class_definition()[source]

Verifies all properties have been filled out properly for the action class. Called by the metaclass only on concrete actions

daf.admin

class daf.admin.ActionMixin[source]

All admins that use actions from daf must inherit this mixin.

daf_actions

The list of action interfaces to be rendered on the admin.

Type

List[Interface]

class daf.admin.AdminViewInterfaceMixin[source]

The base view used for any interface that is rendered in the Django admin.

Similar to the Django admin, users can also define fieldsets for rendering fields from forms and readonly_fields declarations. Interfaces also have access to the admin attribute, which holds the instance of the Django admin class.

permission_required

True if the permission associated with the Action class should be required to view and perform the action. Defaults to True or settings.DAF_ADMIN_ACTION_PERMISSIONS_REQUIRED.

Type

bool

classmethod is_visible(request)[source]

True if the interface is visible from the admin

By default, returns False if permissions are required and the user does not have permission.

get_change_form_context(context)[source]

This methods attempts to gather the same context the admin site gathers when it renders a changeform. The Django admin doesn’t have a pretty way of getting this data since it is so tightly coupled with template rendering.

class daf.admin.ModelViewInterface[source]

The base interface used by any admin views that require a model.

Currently all admin views require a model, so this needs to be the parent class for every admin view.

If this is the base class, the action is rendered in the toolbar on the model change list page.

class daf.admin.ObjectViewInterface[source]

The base interface for object views in the admin. These are rendered in the toolbar of the object detail page.

class daf.admin.ObjectsViewInterface[source]

The base interface for objects views in the admin. These are rendered in the dropdown menu of the change list page.

class daf.admin.FormView(**kwargs)[source]

For constructing form views in the admin. Using this as the base class for an admin view will render it in the toolbar on the change list admin page for the model.

class daf.admin.ObjectFormView(**kwargs)[source]

For constructing object form views in the admin. Using this as the base class for an admin view will render it in the toolbar on the detail admin page for the model object.

class daf.admin.ObjectsFormView(**kwargs)[source]

For constructing objects form views in the admin. Using this as the base class for an admin view will render it in the dropdown menu of bulk actions on the change list admin page for the model.

class daf.admin.WizardAdminViewInterfaceMixin[source]

Overrides fieldset getters for wizards so that fieldsets can be defined for each step in Django admin views.

class daf.admin.WizardViewInterface[source]

The base interface required by any admin wizard view.

Renders buttons in the same location as ModelViewInterface.

class daf.admin.ObjectWizardViewInterface[source]

The base interface required by any admin object wizard view.

Renders buttons in the same location as ObjectViewInterface.

class daf.admin.ObjectsWizardViewInterface[source]

The base interface required by any admin objects wizard view.

Renders buttons in the same location as ObjectsViewInterface.

class daf.admin.WizardView(**kwargs)[source]

The base view class for making wizard views in the Django admin. Using this view as the base class renders a wizard view in the toolbar for the main change list page on the model admin.

class daf.admin.ObjectWizardView(**kwargs)[source]

The base view class for making object wizard views in the Django admin. Using this view as the base class renders a wizard view in the toolbar for the main detail page on the model object admin.

class daf.admin.ObjectsWizardView(**kwargs)[source]

The base view class for making wizard objects views in the Django admin. Using this view as the base class renders a wizard view in the dropdown menu alongside other bulk actions on the change list page in the model admin.

class daf.admin.SessionWizardView(**kwargs)[source]

Same as daf.admin.WizardView, but uses a session backend by default.

class daf.admin.SessionObjectWizardView(**kwargs)[source]

Same as daf.admin.ObjectWizardView, but uses a session backend by default.

class daf.admin.SessionObjectsWizardView(**kwargs)[source]

Same as daf.admin.ObjectsWizardView, but uses a session backend by default.

daf.contrib

class daf.contrib.single_list(arg, default=<object object>)[source]

A lazy arg loader that creates a list from a single element

exception daf.contrib.ContextualizedValidationError(message, code=None, params=None)[source]
__init__(message, code=None, params=None)[source]

Initializes a contextual validation error.

Parameters
  • message (str) – The message for the error. Can be a string, or any of the arguments used by Django’s ValidationError

  • code (str) – An optional code for the error.

  • params (dict) – Parameters used when rendering the message

daf.contrib.raise_contextualized_error(error_class=<class 'daf.contrib.ContextualizedValidationError'>)[source]

Raises a single ContextualizedValidationError where all errors contained by it have messages that are contextualized for a parametrized run.

daf.contrib.raise_trapped_errors()[source]

Raises trapped errors as one ValidationError (if multiple errors are trapped). Re-raises the single error if one error is trapped.

daf.interfaces

class daf.interfaces.InterfaceMeta[source]

A metaclass for registering interfaces to actions

class daf.interfaces.Interface[source]

Provides base properties for any action interface.

daf.models

class daf.models.ActionPermission(*args, **kwargs)[source]

This model only serves to provide a content type for action permissions.

When this table is created, Django makes a content type for the model. We associate all auto-generated action permissions with this content type in order to ensure that action permissions dont accidentally override other Django permissions.

exception DoesNotExist
exception MultipleObjectsReturned

daf.permissions

daf.permissions.install()[source]

Installs all permissions associated with actions

This function is exected automatically at the end of every django migration. The signal handler is linked in actions/apps.py

daf.registry

daf.registry.autodiscover_actions()[source]

Imports the “actions” module of every installed app to discover actions

daf.registry.get(uri)[source]

Get the action class by uri

If URI is not a string, assume it is an action class that has a URI

class daf.registry.FilterableObjects(objects)[source]
__init__(objects)[source]

Initialize self. See help(type(self)) for accurate signature.

daf.registry.actions(actions=None)[source]

Get all action classes as a filterable list.

daf.registry.interfaces(interfaces=None)[source]

Get all action interfaces as a filterable list.

daf.rest_framework

class daf.rest_framework.InstallDAFActions[source]

A metaclass that installs DAF actions on DRF viewsets

class daf.rest_framework.ActionMixin[source]

The mixin that must be inherited by a rest framework viewset in order to expose actions as endpoints.

daf_actions = None

The DetailAction interfaces to add to the viewset.

exception daf.rest_framework.APIException(detail=None, code=None)[source]

The base error class raised by raise_drf_error.

property status_code

int([x]) -> integer int(x, base=10) -> integer

Convert a number or string to an integer, or return 0 if no arguments are given. If x is a number, return x.__int__(). For floating point numbers, this truncates towards zero.

If x is not a number or if base is given, then x must be a string, bytes, or bytearray instance representing an integer literal in the given base. The literal can be preceded by ‘+’ or ‘-‘ and be surrounded by whitespace. The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to interpret the base from the string as an integer literal. >>> int(‘0b100’, base=0) 4

daf.rest_framework.raise_drf_error(exception_class=<class 'daf.rest_framework.APIException'>)[source]

Re-raise non-DRF errors as APIException classes

class daf.rest_framework.DetailAction(viewset, request, pk)[source]

The interface for constructing detail actions in rest framework viewsets.

exception_class

alias of APIException

form_class

alias of django.forms.forms.Form

refetch_for_serialization = True

True if objects should be re-fetched before they are serialized and returned as a response

methods = None

Methods for the action. Defaults to [“post”] if None

__init__(viewset, request, pk)[source]

Initialize self. See help(type(self)) for accurate signature.

classmethod as_interface(url_name=None, url_path=None, methods=None, **kwargs)[source]

Creates a DRF action from a the interface.

Parameters
  • url_name (str, default=cls.url_name) – The url_name argument that is passed to the DRF @action decorator.

  • url_path (str, default=cls.url_path) – The url_path argument that is passed to the DRF @action decorator.

  • methods (list, default=[POST]) – The list of methods over which the action will be available.

  • **kwargs – Any additional argument accepted by the drf.action decorator.

daf.urls

daf.urls.get_url_patterns(interfaces, *, path_prefix='', name_prefix='', interface_kwargs=None)[source]

Generate URL patterns for interfaces.

daf.utils

class daf.utils.classproperty(getter)[source]

A descriptor for a class property

Adapted from https://stackoverflow.com/questions/3203286/how-to-create-a-read-only-class-property-in-python Note that this does not handle cases of users overwriting the values

__init__(getter)[source]

Initialize self. See help(type(self)) for accurate signature.

daf.views

class daf.views.ViewInterface[source]

Base interface all action views must inherit.

Automatically wraps the action with daf.contrib.raise_contextualized_error, which will raise errors contextualized based on various factors.

class daf.views.ModelViewInterface[source]

The base view used by any views that are directly associated with a model. Verifies that any action used by the view are subclasses of ModelAction.

class daf.views.ObjectViewInterface[source]

The base interface for any object-based views.

class daf.views.ObjectsViewInterface[source]

The base interface for any bulk object views.

class daf.views.FormView(**kwargs)[source]

For constructing a django-args FormView on an Action

class daf.views.ObjectFormView(**kwargs)[source]

For constructing a django-args ObjectView on an ObjectAction or ObjectsAction

class daf.views.ObjectsFormView(**kwargs)[source]

For constructing a django-args ObjectsView on an ObjectAction or ObjectsAction.

class daf.views.WizardView(**kwargs)[source]

For constructing a django-args WizardView on an Action.

class daf.views.ObjectWizardView(**kwargs)[source]

For constructing a django-args ObjectWizardView on an ObjectAction or ObjectsAction.

class daf.views.ObjectsWizardView(**kwargs)[source]

For constructing a django-args ObjectsWizardView on an ObjectAction or ObjectsAction.

class daf.views.SessionWizardView(**kwargs)[source]

For constructing a django-args SessionWizardView on an Action.

class daf.views.SessionObjectWizardView(**kwargs)[source]

For constructing a django-args SessionObjectWizardView on an ObjectAction or ObjectsAction.

class daf.views.SessionObjectsWizardView(**kwargs)[source]

For constructing a django-args SessionObjectsWizardView on an ObjectAction or ObjectsAction.