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 receipients 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): ... # 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): ... # 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¶
- 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 singleton
>>> from dynamic_forms.actions import action_registry
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.
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 label label. The function will internally be referred by it’s full qualified name:
'%s.%s' % (func.__module__, func.__name__)
Action registry utilities¶
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.
@formmodel_action('My super awesome action') def my_action(form_model, form): # do something with the data ...
This is equivalent to:
def my_action(form_model, form): # do something with the data ... action_registry.register(my_action, 'My super awesome action')
- dynamic_forms.actions.dynamic_form_send_email(form_model, form)¶
Sends the data submitted through the form form via e-mail to all recipients listed in DYNAMIC_FORMS_EMAIL_RECIPIENTS.
- 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 the form_model for each field are needed, in the appropriate order by calling get_mapped_data().
- dynamic_forms.actions.dynamic_form_store_database(form_model, form)¶
This action takes the mapped data from the form and serializes it as JSON. This value is then stored in the FormModelData.
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.