Actions¶
Actions define what happens once a user submits a
FormModelForm
. django-dynamic-forms provides
two basic actions dynamic_form_send_email()
and
dynamic_form_store_database()
that, as their names indicate, either
send the submitted data via e-mail to the recipients defined in the
DYNAMIC_FORMS_EMAIL_RECIPIENTS
settings variable
or stores it into the database (precisely the
FormModelData
model).
Any action that should be available for usage must be registered in the
ActionRegistry
. This can be done with the following code:
>>> def my_function(form_model, form, request):
... # do something
... pass
...
>>> from dynamic_forms.actions import action_registry
>>> action_registry.register(my_function, 'My Label')
This allows one to register an action during runtime. But many actions are
already available during compile or start-up time and can be registered then by
using a handy decorator formmodel_action()
. Given the above situation,
this would look like:
>>> from dynamic_forms.actions import formmodel_action
>>> @formmodel_action('My Label')
... def my_function(form_model, form, request):
... # do something
... pass
...
New in version 0.3: When a dynamic form is submitted through
DynamicFormView
the return values of actions
are kept for further usage. This allows the view to e.g. add a link to a
permanent URL refering to some stored values.
Providing and accessing actions¶
ActionRegistry
¶
-
class
dynamic_forms.actions.
ActionRegistry
¶ The ActionRegistry keeps track of all available actions available to the software. It is available to the outside through the
action_registry
singletonWarning
You should not import the
ActionRegistry
directly! Always use the singleton instanceaction_registry
!>>> from dynamic_forms.actions import action_registry
-
get
(key)¶ Parameters: key (str) – The key to get an action Returns: Either the action previously registered or None
if no action with the given key has been found.
-
get_as_choices
()¶ Changed in version 0.3: Returns a generator instead of a list
Returns a generator that yields all registered actions as 2-tuples in the form
(key, label)
.
-
register
(func, label)¶ Registers the function
func
with the labellabel
. The function will internally be referred by it’s full qualified name:'%s.%s' % (func.__module__, func.__name__)
Parameters:
-
-
dynamic_forms.actions.
action_registry
¶ The singleton instance of the
ActionRegistry
.
Action registry utilities¶
-
@
dynamic_forms.actions.
formmodel_action
(label)¶ Registering various actions by hand can be time consuming. This function decorator eases this heavily: given a string as the first argument, this decorator registeres the decorated function withing the
action_registry
with its fully dotted Python path.Usage:
@formmodel_action('My super awesome action') def my_action(form_model, form, request): # do something with the data ...
This is equivalent to:
def my_action(form_model, form, request): # do something with the data ... action_registry.register(my_action, 'My super awesome action')
Default Actions¶
-
dynamic_forms.actions.
dynamic_form_send_email
(form_model, form, request)¶ Sends the data submitted through the form
form
via e-mail to all recipients listed inDYNAMIC_FORMS_EMAIL_RECIPIENTS
.Parameters: - form_model (dynamic_forms.models.FormModel) – The instance of the model defining the form.
- form (dynamic_forms.forms.FormModelForm) – The instance of the submitted
form. One can get the data either using
form.cleaned_data
or, if the labels defined in theform_model
for each field are needed, in the appropriate order by callingget_mapped_data()
. - request (django.http.Request) – The current request
New in version 0.5: The
request
parameter was added.
-
dynamic_forms.actions.
dynamic_form_store_database
(form_model, form, request)¶ This action takes the mapped data from the
form
and serializes it as JSON. This value is then stored in theFormModelData
.See also
dynamic_form_store_database()
for a detailed explaination of the arguments.New in version 0.3: To allow linking to a stored data set, the action now returns the inserted object.
New in version 0.5: The
request
parameter was added.