forms.py¶
This module contains the baseclass for all questionnaire forms and the functions for getting all forms based on a questionnaire model class.
To add a new form, a new apps needs to be added including a models.py with the new questionnaire model and a forms.py file with all the new forms.
Note
Make sure to update the PACKAGE_LOCATION dict in models.py
Forms can be defined as the following the example:
class IDBQuestionnaireForm3A(BaseQuestionnaireForm):
#Forms need to subclass BaseQuestionnaireForm.
#The form_template which is used rendering the template
form_template = 'questionnaire/DefaultQuestionnaireForm.html'
#The number of the form. This number is used to sort the
#forms when they are retrieved for a model
form_nr = 3
#Condition method for conditionally showing the form (True=show)
#Forms that are not shown are also not validated.
#The provided wizard argument can be used to get the cleaned
#data of a form like shown in the example below.
def condition(self, wizard):
cleaned_data = wizard.get_cleaned_data_for_form_class(
IDBQuestionnaireForm)
if cleaned_data:
if 'has_stoma' in cleaned_data:
if cleaned_data['has_stoma'] == 'yes':
return False
return True
class Meta:
model = IBDQuestionnaire
fieldsets = (
(None, {'fields': (
'stool_urgency', 'stool_planning', 'stool_continence',)}),
)
#By using the create_exclude_list method you only have to specify
#the fieldsets. The excluded list is computed automatically based
#on the fieldsets en model definition. For multiple forms for one
#model this saves a lot of time & possibilities for errors.
exclude = create_exclude_list(model, fieldsets)
The get_forms_for method automatically will pick up the new forms when the package is added to the PACKAGE_LOCATION dict in models.py.
Class definitions:
-
apps.questionnaire.forms.form_sorting_function(form)[source]¶ Simple sorting helper function
- Args:
- form: a form instance
- Returns:
- The form_nr of the form.
-
apps.questionnaire.forms.get_forms_for(questionnaire_model_class)[source]¶ Helper function for getting all forms for a questionnaire model class.
- Args:
- questionnaire_model_class: the class of the questionnaire model.
- Returns:
- A list of forms for a questionnaire model class, sorted on form_nr.
-
apps.questionnaire.forms.create_exclude_list(model, fieldsets)[source]¶ Helper method for automatic generating the exclude list of fields based on a model and a fieldset definition. Allows to only set the fieldsets attribute and use this function for the exclude list.
- Args:
- model: the model to inspect for modelfields
- fieldsets: the fieldsets that should be included
- Returns:
- A tuple with all model fields excluding the fields in fieldsets
-
class
apps.questionnaire.forms.BaseQuestionnaireForm(*args, **kwargs)[source]¶ Bases:
core.forms.BaseModelFormBase form for all questionnaires uses a form_nr attribute for sorting and a condition method for conditionally showing/using the form.
-
condition(wizard)[source]¶ Override this function to allow conditional showing/using forms. If the form is not shown it is not validated.
- Args:
- wizard: the wizard instance that is used to show the forms which can be used to get clean and unclean data of other steps/forms to determine if the form should be shown.
- Returns:
- True if the form should be used else False (default=True)
-