Project Description

django-fobi (or just fobi) is a customisable, modular, user- and developer-
friendly form generator/builder application for Django. With fobi you can
build Django forms using an intuitive GUI, save or mail posted form data or
even export forms into JSON format and import them on other instances. API
allows you to build your own form elements and form handlers (mechanisms for
handling the submitted form data).

Prerequisites

Django 1.8, 1.9, 1.10, 1.11 and 2.0.

Python 2.7, 3.4, 3.5, 3.6 and PyPy.

Key concepts

Each form consists of elements. Form elements are divided into two groups:

form fields (input field, textarea, hidden field, file field, etc.).

content (presentational) elements (text, image, embed video, etc.).

Number of form elements is not limited.

Each form may contain handlers. Handler processes the form data (for example,
saves it or mails it). Number of the handlers is not limited.

Both form elements and form handlers are made with Django permission system
in mind.

As an addition to form handlers, form callbacks are implemented. Form
callbacks are fired on various stages of pre- and post-processing the form
data (on POST). Form callbacks do not make use of permission system (unless
you intentionally do so in the code of your callback) and are fired for all
forms (unlike form handlers, that are executed only if assigned).

Each plugin (form element or form handler) or a callback - is a Django
micro-app.

In addition for form element and form handler plugins, integration form
element and integration form handler plugins are implemented for integration
with diverse third-party apps and frameworks (such as Django REST framework).

Note, that django-fobi does not require django-admin and administrative
rights/permissions to access the UI, although almost seamless integration with
django-admin is implemented through the simple theme.

Main features and highlights

Form wizards. Combine your forms into wizards. Form wizards may contain
handlers. Handler processes the form wizard data (for example, saves it or
mails it). Number of the form wizard handlers is not limited.

Forms can be automatically enabled/disabled based on dates (start date, end
date).

Demo

Live demo

Run demo locally

In order to be able to quickly evaluate the django-fobi, a demo app (with a
quick installer) has been created (works on Ubuntu/Debian, may work on other
Linux systems as well, although not guaranteed). Follow the instructions below
for having the demo running within a minute.

Add fobi to INSTALLED_APPS of the your projects’ Django settings.
Furthermore, all themes and plugins to be used, shall be added to the
INSTALLED_APPS as well. Note, that if a plugin has additional
dependencies, you should be mentioning those in the INSTALLED_APPS
as well.

Note, that some plugins require additional URL includes. For instance, if you
listed the fobi.contrib.plugins.form_handlers.db_store form handler plugin
in the INSTALLED_APPS, you should mention the following in urls
module.

View URLs are put separately from edit URLs in order to make it possible
to prefix the edit URLs differently. For example, if you’re using the
“Simple” theme, you would likely want to prefix the edit URLs with “admin/”
so that it looks more like django-admin.

Creating a new form element plugin

Form element plugins represent the elements of which the forms is made:
Inputs, checkboxes, textareas, files, hidden fields, as well as pure
presentational elements (text or image). Number of form elements in a form
is not limited.

Presentational form elements are inherited from fobi.base.FormElementPlugin.

The rest (real form elements, that are supposed to have a value)
are inherited from fobi.base.FormFieldPlugin.

You should see a form element plugin as a Django micro app, which could have
its’ own models, admin interface, etc.

django-fobi comes with several bundled form element plugins. Do check the
source code as example.

Let’s say, you want to create a textarea form element plugin.

There are several properties, each textarea should have. They are:

label (string): HTML label of the textarea.

name (string): HTML name of the textarea.

initial (string): Initial value of the textarea.

required (bool): Flag, which tells us whether the field is required or
optional.

Let’s name that plugin sample_textarea. The plugin directory should then
have the following structure.

Form element plugins should be registered in “fobi_form_elements.py” file. Each
plugin module should be put into the INSTALLED_APPS of your Django
projects’ settings.

In some cases, you would need plugin specific overridable settings (see
fobi.contrib.form_elements.fields.content.content_image plugin as an
example). You are advised to write your settings in such a way, that variables
of your Django project settings module would have FOBI_PLUGIN_ prefix.

Define and register the form element plugin

Step by step review of a how to create and register a plugin and plugin
widgets. Note, that django-fobi auto-discovers your plugins if you place
them into a file named fobi_form_elements.py of any Django app listed in
INSTALLED_APPS of your Django projects’ settings module.

classSampleTextareaPlugin(FormFieldPlugin):"""Sample textarea plugin."""uid="sample_textarea"name="Sample Textarea"form=SampleTextareaFormgroup="Samples"# Group to which the plugin belongs todefget_form_field_instances(self,request=None,form_entry=None,form_element_entries=None,**kwargs):kwargs={'required':self.data.required,'label':self.data.label,'initial':self.data.initial,'widget':forms.widgets.Textarea(attrs={})}return[(self.data.name,forms.CharField,kwargs),]

Registering the SampleTextareaPlugin plugin.

form_element_plugin_registry.register(SampleTextareaPlugin)

Note, that in case you want to define a pure presentational element, make use
of fobi.base.FormElementPlugin for subclassing, instead of
fobi.base.FormFieldPlugin.
See the source of the content plugins
(fobi.contrib.plugins.form_elements.content) as a an example.

For instance, the captcha and honeypot fields are implemented
as form elements (subclasses the fobi.base.FormElementPlugin). The
db_store form handler plugin does not save the form data of
those elements. If you want the form element data to be saved, do inherit
from fobi.base.FormFieldPlugin.

Hidden form element plugins, should be also having set the is_hidden
property to True. By default it’s set to False. That makes the hidden
form elements to be rendered using as django.forms.widgets.TextInput
widget in edit mode. In the view mode, the original widget that you
assigned in your form element plugin would be used.

There might be cases, when you need to do additional handling of the data upon
the successful form submission. In such cases, you will need to define a
submit_plugin_form_data method in the plugin, which accepts the
following arguments:

form_entry (fobi.models.FormEntry): Form entry, which is being submitted.

request (django.http.HttpRequest): The Django HTTP request.

form (django.forms.Form): Form object (a valid one, which contains
the cleaned_data attribute).

form_element_entries (fobi.models.FormElementEntry): Form element entries
for the form_entry given.

(**)kwargs : Additional arguments.

Example (taken from fobi.contrib.plugins.form_elements.fields.file):

defsubmit_plugin_form_data(self,form_entry,request,form,form_element_entries=None,**kwargs):"""Submit plugin form data."""# Get the file pathfile_path=form.cleaned_data.get(self.data.name,None)iffile_path:# Handle the uploadsaved_file=handle_uploaded_file(FILES_UPLOAD_DIR,file_path)# Overwrite ``cleaned_data`` of the ``form`` with path to moved# file.form.cleaned_data[self.data.name]="{0}{1}".format(settings.MEDIA_URL,saved_file)# It's critically important to return the ``form`` with updated# ``cleaned_data``returnform

In the example below, the original form is being modified. If you don’t want
the original form to be modified, do not return anything.

Check the file form element plugin
(fobi.contrib.plugins.form_elements.fields.file) for complete example.

path/to/sample_textarea/forms.py

Why to have another file for defining forms? Just to keep the code clean and
less messy, although you could perfectly define all your plugin forms in the
module fobi_form_elements.py, it’s recommended to keep it separate.

Take into consideration, that forms.py is not an auto-discovered file
pattern. All your form element plugins should be registered in modules named
fobi_form_elements.py.

defsave_plugin_data(self,request=None):"""Saving the plugin data and moving the file."""file_path=self.cleaned_data.get('file',None)iffile_path:saved_image=handle_uploaded_file(IMAGES_UPLOAD_DIR,file_path)self.cleaned_data['file']=saved_image

path/to/sample_textarea/widgets.py

Required imports.

fromfobi.baseimportFormElementPluginWidget

Defining the base plugin widget.

classBaseSampleTextareaPluginWidget(FormElementPluginWidget):"""Base sample textarea plugin widget."""# Same as ``uid`` value of the ``SampleTextareaPlugin``.plugin_uid="sample_textarea"

path/to/sample_layout/fobi_form_elements.py

Register in the registry (in some module which is for sure to be loaded; it’s
handy to do it in the theme module).

classSampleTextareaPluginWidget(BaseSampleTextareaPluginWidget):"""Sample textarea plugin widget."""theme_uid='bootstrap3'# Theme for which the widget is loadedmedia_js=['sample_layout/js/fobi.plugins.form_elements.sample_textarea.js',]media_css=['sample_layout/css/fobi.plugins.form_elements.sample_textarea.css',]

Note, that you have to be logged in, in order to use the dashboard. If your
new plugin doesn’t appear, set the FOBI_DEBUG to True in your Django’s
local settings module, re-run your code and check console for error
notifications.

Creating a new form handler plugin

Form handler plugins handle the form data. django-fobi comes with several
bundled form handler plugins, among which is the db_store and mail
plugins, which are responsible for saving the submitted form data into the
database and mailing the data to recipients specified. Number of form handlers
in a form is not limited. Certain form handlers are not configurable (for
example the db_store form handler isn’t), while others are (mail,
http_repost).

You should see a form handler as a Django micro app, which could have its’ own
models, admin interface, etc.

By default, it’s possible to use a form handler plugin multiple times per form.
If you wish to allow form handler plugin to be used only once in a form,
set the allow_multiple property of the plugin to False.

As said above, django-fobi comes with several bundled form handler plugins.
Do check the source code as example.

Define and register the form handler plugin

Let’s name that plugin sample_mail. The plugin directory should then have
the following structure.

Some form handlers are configurable, some others not. In order to
have a user friendly way of showing the form handler settings, what’s
sometimes needed, a plugin_data_repr method has been introduced.
Simplest implementation of it would look as follows:

path/to/sample_mail/forms.py

If plugin is configurable, it has configuration data. A single form may have
unlimited number of same plugins. Imagine, you want to have different subjects
and additional body texts for different user groups. You could then assign two
form handler mail plugins to the form. Of course, saving the posted form
data many times does not make sense, but it’s up to the user. So, in case if
plugin is configurable, it should have a form.

Why to have another file for defining forms? Just to keep the code clean and
less messy, although you could perfectly define all your plugin forms in the
module fobi_form_handlers.py, it’s recommended to keep it separate.

Take into consideration, that forms.py is not an auto-discovered file
pattern. All your form handler plugins should be registered in modules named
fobi_form_handlers.py.

After the plugin has been processed, all its’ data is available in a
plugin_instance.data container (for example,
plugin_instance.data.subject or plugin_instance.data.from_name).

Prioritise the execution order

Some form handlers shall be executed prior others. A good example of such, is
a combination of “mail” and “db_save” form handlers for the form. In case if
large files are posted, submission of form data would fail if “mail” plugin
would be executed after “db_save” has been executed. That’s why it’s possible
to prioritise that ordering in a FOBI_FORM_HANDLER_PLUGINS_EXECUTION_ORDER
setting variable.

If not specified or left empty, form handler plugins would be ran in the order
of discovery. All form handler plugins that are not listed in the
FORM_HANDLER_PLUGINS_EXECUTION_ORDER, would be ran after the plugins that
are mentioned there.

FORM_HANDLER_PLUGINS_EXECUTION_ORDER=('http_repost','mail',# The 'db_store' is left out intentionally, since it should# be the last plugin to be executed.)

Form handler plugin custom actions

By default, a single form handler plugin has at least a “delete” action.
If plugin is configurable, it gets an “edit” action as well.

For some of your plugins, you may want to register a custom action. For
example, the “db_store” plugin does have one, for showing a link to
a listing page with saved form data for the form given.

For such cases, define a custom_actions method in your form handler
plugin. That method shall return a list of triples. In each triple,
first value is the URL, second value is the title and the third value
is the icon of the URL.

Form handler plugin final steps

Do not forget to add the form handler plugin module to INSTALLED_APPS.

INSTALLED_APPS=(# ...'path.to.sample_mail',# ...)

Afterwards, go to terminal and type the following command.

./manage.py fobi_sync_plugins

If your HTTP server is running, you would then be able to see the new plugin
in the edit form interface.

Creating a new form importer plugin

Form importer plugins import the forms from some external data source into
django-fobi form format. Number of form importers is not limited. Form
importers are implemented in forms of wizards (since they may contain several
steps).

You should see a form importer as a Django micro app, which could have its’ own
models, admin interface, etc.

At the moment django-fobi comes with only one bundled form handler plugin,
which is the mailchimp_importer, which is responsible for importing
existing MailChimp forms into django-fobi.

Define and register the form importer plugin

Let’s name that plugin sample_importer. The plugin directory should then
have the following structure.

path/to/sample_importer/forms.py

As mentioned above, form importers are implemented in form of wizards. The
forms are the wizard steps.

Required imports.

fromdjangoimportformsfromdjango.utils.translationimportugettext_lazyas_fromsample_service_apiimportsample_api# Just an imaginary API client

Defining the form for Sample importer plugin.

classSampleImporterStep1Form(forms.Form):"""First form the the wizard."""api_key=forms.CharField(required=True)classSampleImporterStep2Form(forms.Form):"""Second form of the wizard."""list_id=forms.ChoiceField(required=True,choices=[])def__init__(self,*args,**kwargs):self._api_key=Noneif'api_key'inkwargs:self._api_key=kwargs.pop('api_key',None)super(SampleImporterStep2Form,self).__init__(*args,**kwargs)ifself._api_key:client=sample_api.Api(self._api_key)lists=client.lists.list()choices=[(l['id'],l['name'])forlinlists['data']]self.fields['list_id'].choices=choices

path/to/sample_importer/views.py

The wizard views.

Required imports.

fromsample_service_apiimportsample_api# Just an imaginary API clientfromdjango.shortcutsimportredirectfromdjango.core.urlresolversimportreversefromdjango.contribimportmessagesfromdjango.utils.translationimportugettext_lazyas_# For django LTE 1.8 import from `django.contrib.formtools.wizard.views`fromformtools.wizard.viewsimportSessionWizardViewfrompath.to.sample_importer.formsimport(SampleImporterStep1Form,SampleImporterStep2Form,)

Defining the wizard view for Sample importer plugin.

classSampleImporterWizardView(SessionWizardView):"""Sample importer wizard view."""form_list=[SampleImporterStep1Form,SampleImporterStep2Form]defget_form_kwargs(self,step):"""Get form kwargs (to be used internally)."""if'1'==step:data=self.get_cleaned_data_for_step('0')or{}api_key=data.get('api_key',None)return{'api_key':api_key}return{}defdone(self,form_list,**kwargs):"""After all forms are submitted."""# Merging cleaned data into one dictcleaned_data={}forforminform_list:cleaned_data.update(form.cleaned_data)# Connecting to sample client APIclient=sample_client.Api(cleaned_data['api_key'])# Fetching the form dataform_data=client.lists.merge_vars(id={'list_id':cleaned_data['list_id']})# We need the first form onlytry:form_data=form_data['data'][0]exceptExceptionaserr:messages.warning(self.request,_('Selected form could not be imported due errors.'))returnredirect(reverse('fobi.dashboard'))# Actually, import the formform_entry=self._form_importer.import_data({'name':form_data['name'],'user':self.request.user},form_data['merge_vars'])redirect_url=reverse('fobi.edit_form_entry',kwargs={'form_entry_id':form_entry.pk})messages.info(self.request,_('Form {0} imported successfully.').format(form_data['name']))returnredirect("{0}".format(redirect_url))

Form importer plugin final steps

Do not forget to add the form importer plugin module to INSTALLED_APPS.

INSTALLED_APPS=(# ...'path.to.sample_importer',# ...)

Afterwards, go to terminal and type the following command.

./manage.py fobi_sync_plugins

If your HTTP server is running, you would then be able to see the new plugin
in the dashboard form interface (implemented in all bundled themes).

Creating a form callback

Form callbacks are additional hooks, that are executed on various stages of
the form submission.

Let’s place the callback in the foo module. The plugin directory should
then have the following structure.

Suggestions

Custom action for the form

Sometimes, you would want to specify a different action for the form.
Although it’s possible to define a custom form action (action field
in the “Form properties” tab), you’re advised to use the http_repost
plugin instead, since then the form would be still validated locally
and only then the valid data, as is, would be sent to the desired
endpoint.

Take in mind, that if both cases, if CSRF protection is enabled on
the endpoint, your post request would result an error.

When you want to customise too many things

django-fobi, with its’ flexible form elements, form handlers and form
callbacks is very customisable. However, there might be cases when you need to
override entire view to fit your needs. Take a look at the
FeinCMS integration
or DjangoCMS integration
as a good example of such. You may also want to compare the code from original
view fobi.views.view_form_entry with the code from the widget to get a
better idea of what could be changed in your case. If need a good advice,
just ask me.

Theming

django-fobi comes with theming API. While there are several ready-to-use
themes:

“Bootstrap 3” theme

“Foundation 5” theme

“Simple” theme in (with editing interface in style of the Django admin)

“DjangoCMS admin style” theme (which is another simple theme with editing
interface in style of djangocms-admin-style)

Obviously, there are two sorts of views when it comes to editing and viewing
the form.

The “view-view”, when the form as it has been made is exposed to the
site end- users/visitors.

The “edit-view” (builder view), where the authorised users build their forms.

Both “Bootstrap 3” and “Foundation 5” themes are making use of the same style
for both “view-view” and “edit-view” views.

Both “Simple” and “DjangoCMS admin style” themes are styling for the
“edit-view” only. The “view-view” is pretty much blank, as shown on the one
of the screenshots [2.6].

Have in mind, that creating a brand new theme could be time consuming.
Instead, you are advised to extend existing themes or in the worst case,
if too much customisation required, create your own themes based on
existing ones (just copy the desired theme to your project directory and
work it out further).

It’s possible to use different templates for all “view” and “edit”
actions (see the source code of the “simple” theme). Both “Bootstrap 3” and
“Foundation 5” themes look great. Although if you can’t use any of those,
the “Simple” theme is the best start, since it looks just like django-admin.

Create a new theme

Let’s place the theme in the sample_theme module. The theme directory
should then have the following structure.

Sometimes you would want to attach additional properties to the theme
in order to use them later in templates (remember, current theme object
is always available in templates under name fobi_theme).

For such cases you would need to define a variable in your project’s settings
module, called FOBI_CUSTOM_THEME_DATA. See the following code as example:

# `django-fobi` custom theme data for to be displayed in third party apps# like `django-registraton`.FOBI_CUSTOM_THEME_DATA={'bootstrap3':{'page_header_html_class':'','form_html_class':'form-horizontal','form_button_outer_wrapper_html_class':'control-group','form_button_wrapper_html_class':'controls','form_button_html_class':'btn','form_primary_button_html_class':'btn-primary pull-right',},'foundation5':{'page_header_html_class':'','form_html_class':'form-horizontal','form_button_outer_wrapper_html_class':'control-group','form_button_wrapper_html_class':'controls','form_button_html_class':'radius button','form_primary_button_html_class':'btn-primary',},'simple':{'page_header_html_class':'','form_html_class':'form-horizontal','form_button_outer_wrapper_html_class':'control-group','form_button_wrapper_html_class':'submit-row','form_button_html_class':'btn','form_primary_button_html_class':'btn-primary',}}

You would now be able to access the defined extra properties in templates
as shown below.

From all of the templates listed above, the _base.html template is
the most influenced by the Bootstrap 3 theme.

Make changes to an existing theme

As said above, making your own theme from scratch could be costly. Instead,
you can override/reuse an existing one and change it to your needs with
minimal efforts. See the override simple theme
example. In order to see it in action, run the project with
settings_override_simple_theme
option:

templates/override_simple_theme/snippets/form_ajax.html

Form wizards

Basics

With form wizards you can split forms across multiple pages. State is
maintained in one of the backends (at the moment the Session backend). Data
processing is delayed until the submission of the final form.

In django-fobi wizards work in the following way:

Number of forms in a form wizard is not limited.

Form callbacks, handlers are totally ignored in form wizards. Instead,
the form-wizard specific handlers (form wizard handlers) take over handling
of the form data on the final step.

Bundled form wizard handler plugins

Below a short overview of the form wizard handler plugins. See the
README.rst file in directory of each plugin for details.

Integration with third-party apps and frameworks

django-fobi has been successfully integrated into a number of diverse
third-party apps and frameworks, such as: Django REST framework, Django CMS,
FeinCMS, Mezzanine and Wagtail.

Certainly, integration into CMS is one case, integration into REST framework -
totally another. In REST frameworks we no longer have forms as such. Context
is very different. Handling of form data should obviously happen in a
different way. Assembling of the form class isn’t enough (in case of Django
REST framework we assemble the serializer class).

In order to handle such level of integration, two additional sort of plugins
have been introduced:

IntegrationFormElementPlugin

IntegrationFormHandlerPlugin

These plugins are in charge of representation of the form elements in a
proper way for the package to be integrated and handling the submitted form
data.

Sample IntegrationFormElementPlugin

base.py

Define the form element plugin.

fromdjango.utils.translationimportugettext_lazyas_fromrest_framework.fieldsimportEmailFieldfromfobi.baseimportIntegrationFormFieldPluginfromfobi.contrib.apps.drf_integrationimportUIDasINTEGRATE_WITH_UIDfromfobi.contrib.apps.drf_integration.baseimport(DRFIntegrationFormElementPluginProcessor,DRFSubmitPluginFormDataMixin,)fromfobi.contrib.apps.drf_integration.form_elements.fields.emailimportUIDclassEmailInputPlugin(IntegrationFormFieldPlugin,DRFSubmitPluginFormDataMixin):"""EmailField plugin."""uid=UIDintegrate_with=INTEGRATE_WITH_UIDname=_("Decimal")group=_("Fields")defget_custom_field_instances(self,form_element_plugin,request=None,form_entry=None,form_element_entries=None,**kwargs):"""Get form field instances."""field_kwargs={'required':form_element_plugin.data.required,'initial':form_element_plugin.data.initial,'label':form_element_plugin.data.label,'help_text':form_element_plugin.data.help_text,'max_length':form_element_plugin.data.max_length,}return[DRFIntegrationFormElementPluginProcessor(field_class=EmailField,field_kwargs=field_kwargs)]

fobi_integration_form_elements.py

Register the plugin. Note the name pattern fobi_integration_form_elements.

Sample IntegrationFormHandlerPlugin

base.py

Define the form handler plugin.

importloggingfrommimetypesimportguess_typeimportosfromdjango.confimportsettingsfromdjango.utils.translationimportugettext_lazyas_fromfobi.baseimportIntegrationFormHandlerPluginfromfobi.helpersimportextract_file_pathfromfobi.contrib.apps.drf_integrationimportUIDasINTEGRATE_WITH_UIDfromfobi.contrib.apps.drf_integration.baseimportget_processed_serializer_datafrom.importUIDclassMailHandlerPlugin(IntegrationFormHandlerPlugin):"""Mail handler form handler plugin.
Can be used only once per form.
"""uid=UIDname=_("Mail")integrate_with=INTEGRATE_WITH_UIDdefrun(self,form_handler_plugin,form_entry,request,form_element_entries=None,**kwargs):"""Run."""base_url=form_handler_plugin.get_base_url(request)serializer=kwargs['serializer']# Clean up the values, leave our content fields and empty values.field_name_to_label_map,cleaned_data=get_processed_serializer_data(serializer,form_element_entries)rendered_data=form_handler_plugin.get_rendered_data(serializer.validated_data,field_name_to_label_map,base_url)files=self._prepare_files(request,serializer)form_handler_plugin.send_email(rendered_data,files)def_prepare_files(self,request,serializer):"""Prepares the files for being attached to the mail message."""files={}defprocess_path(file_path,imf):"""Processes the file path and the file."""iffile_path:file_path=file_path.replace(settings.MEDIA_URL,os.path.join(settings.MEDIA_ROOT,''))mime_type=guess_type(imf.name)files[field_name]=(imf.name,''.join([cforcinimf.chunks()]),mime_type[0]ifmime_typeelse'')forfield_name,imfinrequest.FILES.items():try:file_path=serializer.validated_data.get(field_name,'')process_path(file_path,imf)exceptExceptionaserr:file_path=extract_file_path(imf.name)process_path(file_path,imf)returnfiles

fobi_integration_form_handlers.py

Register the plugin. Note the name pattern fobi_integration_form_handlers.

Permissions

Plugin system allows administrators to specify the access rights to every
plugin. django-fobi permissions are based on Django Users and User Groups.
Access rights are manageable via Django admin (“/admin/fobi/formelement/”,
“/admin/fobi/formhandler/”). If user doesn’t have the rights to access plugin,
it doesn’t appear on his form even if has been added to it (imagine, you have
once granted the right to use the news plugin to all users, but later on
decided to limit it to Staff members group only). Note, that superusers have
access to all plugins.

Management commands

fobi_find_broken_entries. Find broken form element/handler entries that
occur when some plugin which did exist in the system, no longer exists.

fobi_sync_plugins. Should be ran each time a new plugin is being added to
the django-fobi.

fobi_update_plugin_data. A mechanism to update existing plugin data in
case if it had become invalid after a change in a plugin. In order for it
to work, each plugin should implement and update method, in which the
data update happens.

Tuning

There are number of django-fobi settings you can override in the settings
module of your Django project:

FOBI_RESTRICT_PLUGIN_ACCESS (bool): If set to True, (Django) permission
system for dash plugins is enabled. Defaults to True. Setting this to False
makes all plugins available for all users.

Third-party plugins and themes

fobi-phonenumber - A Fobi
PhoneNumber form field plugin. Makes use of the
phonenumber_field.formfields.PhoneNumberField and
phonenumber_field.widgets.PhoneNumberPrefixWidget.

HTML5 fields

The following HTML5 fields are supported in corresponding bundled plugins:

date

datetime

email

max

min

number

url

placeholder

type

With the fobi.contrib.plugins.form_elements.fields.input support for
HTML5 fields is extended to the following fields:

autocomplete

autofocus

list

multiple

pattern

step

Loading initial data using GET arguments

It’s possible to provide initial data for the form using the GET arguments.

In that case, along with the field values, you should be providing
an additional argument named “fobi_initial_data”, which doesn’t have to
hold a value. For example, if your form contains of fields named “email” and
“age” and you want to provide initial values for those using GET arguments, you
should be constructing your URL to the form as follows:

Dynamic initial values

It’s possible to provide a dynamic initial value for any of the text elements.
In order to do that, you should use the build-in context processor or make
your own one. The only requirement is that you should store all values that
should be exposed in the form as a dict for fobi_dynamic_values dictionary
key. Beware, that passing the original request object might be unsafe in
many ways. Currently, a stripped down version of the request object is being
passed as a context variable.

In your GUI, you should be referring to the initial values in the following
way:

{{ request.path }} {{ now }} {{ today }}

Note, that you should not provide the fobi_dynamic_values. as a prefix.
Currently, the following variables are available in the
fobi.context_processors.dynamic_values context processor:

- request: Stripped HttpRequest object.
- request.path: A string representing the full path to the requested
page, not including the scheme or domain.
- request.get_full_path(): Returns the path, plus an appended query
string, if applicable.
- request.is_secure(): Returns True if the request is secure; that
is, if it was made with HTTPS.
- request.is_ajax(): Returns True if the request was made via an
XMLHttpRequest, by checking the HTTP_X_REQUESTED_WITH header for the
string 'XMLHttpRequest'.
- request.META: A stripped down standard Python dictionary containing
the available HTTP headers.
- HTTP_ACCEPT_ENCODING: Acceptable encodings for the response.
- HTTP_ACCEPT_LANGUAGE: Acceptable languages for the response.
- HTTP_HOST: The HTTP Host header sent by the client.
- HTTP_REFERER: The referring page, if any.
- HTTP_USER_AGENT: The client’s user-agent string.
- QUERY_STRING: The query string, as a single (un-parsed) string.
- REMOTE_ADDR: The IP address of the client.
- request.user: Authenticated user.
- request.user.email:
- request.user.get_username(): Returns the username for the user.
Since the User model can be swapped out, you should use this
method instead of referencing the username attribute directly.
- request.user.get_full_name(): Returns the first_name plus the
last_name, with a space in between.
- request.user.get_short_name(): Returns the first_name.
- request.user.is_anonymous():
- now: datetime.datetime.now()
- today: datetime.date.today()

Submitted form element plugins values

While some values of form element plugins are submitted as is, some others
need additional processing. There are 3 types of behaviour taken into
consideration:

“val”: value is being sent as is.

“repr”: (human readable) representation of the value is used.

“mix”: mix of value as is and human readable representation.

The following plugins have been made configurable in such a way, that
developers can choose the desired behaviour in projects’ settings:

FOBI_FORM_ELEMENT_CHECKBOX_SELECT_MULTIPLE_SUBMIT_VALUE_AS

FOBI_FORM_ELEMENT_RADIO_SUBMIT_VALUE_AS

FOBI_FORM_ELEMENT_SELECT_SUBMIT_VALUE_AS

FOBI_FORM_ELEMENT_SELECT_MULTIPLE_SUBMIT_VALUE_AS

FOBI_FORM_ELEMENT_SELECT_MODEL_OBJECT_SUBMIT_VALUE_AS

FOBI_FORM_ELEMENT_SELECT_MULTIPLE_MODEL_OBJECTS_SUBMIT_VALUE_AS

See the README.rst in each of the following plugins for more information.

Rendering forms using third-party libraries

For that purpose you should override the “snippets/form_snippet.html” used
by the theme you have chosen. Your template would then look similar to the
one below (make sure to setup/configure your third-party form rendering library
prior doing this).

Import/export forms

There might be cases when you have django-fobi running on multiple instances
and have already spend some time on making forms on one of the instances,
and want to reuse those forms on another. You could of course re-create entire
form in the GUI, but we can do better than that. It’s possible to export forms
into JSON format and import the exported forms again. It’s preferable that
you run both instances on the same versions of django-fobi, otherwise imports
might break (although it might just work). There many ways to deal with
missing plugin errors, but the chosen strategy (which you don’t yet have full
control of) is safest (import everything possible, but warn user about errors).
If both instances have the same set of form element and form handler plugins
imports should go smoothly. It is though possible to make an import ignoring
missing form element and form handler plugins. You would get an appropriate
notice about that, but import will continue leaving the broken plugin data out.

Translations

Available translations

English is the primary language. The following translations are
available (core and plugins)

In the example given, “Boolean” and “Checkbox select multiple” plugin names
are renamed to “Checkbox” and “Multiple checkboxes” respectively.

All built-in plugin name values are almost equivalent to the plugin uid
values. By default plugins are sorted by uid value. When you override the
name of the plugin, sorting breaks. Therefore, it’s recommended to
set the FOBI_SORT_PLUGINS_BY_VALUE value to True in your settings module.
Default value is False, which means that plugins are sorted by their uid
value.

FOBI_SORT_PLUGINS_BY_VALUE=True

Debugging

By default debugging is turned off. It means that broken form entries, which
are entries with broken data, that are not possible to be shown, are just
skipped. That’s safe in production. Although, you for sure would want to
see the broken entries in development. Set the FOBI_DEBUG to True
in the settings.py of your project in order to do so.

Most of the errors are logged (DEBUG). If you have written a plugin and it
somehow doesn’t appear in the list of available plugins, do run the following
management command since it not only syncs your plugins into the database,
but also is a great way of checking for possible errors.

./manage.py fobi_sync_plugins

Run the following command in order to identify the broken plugins.

./manage.py fobi_find_broken_entries

If you have forms referring to form element- of form handler- plugins
that are currently missing (not registered, removed, failed to load - thus
there would be a risk that your form would’t be rendered properly/fully and
the necessary data handling wouldn’t happen either) you will get an
appropriate exception. Although it’s fine to get an instant error message about
such failures in development, in production is wouldn’t look appropriate.
Thus, there are two settings related to the non-existing (not-found) form
element- and form handler- plugins.

FOBI_DEBUG: Set this to True in your development environment anyway. Watch
error logs closely.

FOBI_FAIL_ON_MISSING_FORM_ELEMENT_PLUGINS: If you want no error to be
shown in case of missing form element plugins, set this to False in
your settings module. Default value is True.

FOBI_FAIL_ON_MISSING_FORM_HANDLER_PLUGINS: If you want no error to be
shown in case of missing form element handlers, set this to False in
your settings module. Default value is True.

Testing

Project is covered by test (functional- and browser-tests).

To test with all supported Python/Django versions type:

tox

To test against specific environment, type:

tox -e pypy-django18

To test just your working environment type:

./runtests.py

It’s assumed that you have all the requirements installed. If not, first
install the test requirements:

pip install -r examples/requirements/test.txt

Browser tests

For browser tests you may choose between Firefox, headless Firefox and
PhantomJS. PhantomJS is faster, headless Firefox is fast as well, but
normal Firefox tests tell you more (as you see what exactly happens on the
screen). Both cases require some effort and both have disadvantages regarding
the installation (although once you have them installed they work perfect).

Latest versions of Firefox are often not supported by Selenium. Current
version of the Selenium for Python (2.53.6) works fine with Firefox 47.
Thus, instead of using system Firefox you could better use a custom one.

For PhantomJS you need to have NodeJS installed.

Set up Firefox 47

Download Firefox 47 from
this
location and unzip it into /usr/lib/firefox47/

Specify the full path to your Firefox in FIREFOX_BIN_PATH
setting. Example:

FIREFOX_BIN_PATH='/usr/lib/firefox47/firefox'

If you set to use system Firefox, remove or comment-out the
FIREFOX_BIN_PATH setting.

After that your Selenium tests would work.

Set up headless Firefox

Install xvfb package which is used to start Firefox in headless mode.

sudo apt-get install xvfb

Run the tests using headless Firefox.

./scripts/runtests.sh

Or run tox tests using headless Firefox.

./scripts/tox.sh

Or run specific tox tests using headless Firefox.

./scripts/tox.sh -e py36-django111

Setup PhantomJS

You could also run tests in headless mode (faster). For that you will need
PhantomJS.