Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/barseghyanartur/django-fobi
Form generator/builder application for Django done right: customisable, modular, user- and developer- friendly.
https://github.com/barseghyanartur/django-fobi
django django-cms-plugin django-rest-framework-addon dynamic-forms feincms-widget form-builder form-designer form-generator form-wizards forms mezzanine-page-processor python wagtail-page
Last synced: 6 days ago
JSON representation
Form generator/builder application for Django done right: customisable, modular, user- and developer- friendly.
- Host: GitHub
- URL: https://github.com/barseghyanartur/django-fobi
- Owner: barseghyanartur
- Created: 2014-10-11T03:22:25.000Z (about 10 years ago)
- Default Branch: main
- Last Pushed: 2023-11-13T22:59:20.000Z (about 1 year ago)
- Last Synced: 2024-11-30T10:02:38.573Z (13 days ago)
- Topics: django, django-cms-plugin, django-rest-framework-addon, dynamic-forms, feincms-widget, form-builder, form-designer, form-generator, form-wizards, forms, mezzanine-page-processor, python, wagtail-page
- Language: Python
- Homepage: https://pypi.python.org/pypi/django-fobi
- Size: 15.7 MB
- Stars: 486
- Watchers: 25
- Forks: 112
- Open Issues: 39
-
Metadata Files:
- Readme: README.rst
- Changelog: CHANGELOG.rst
- License: LICENSES.rst
- Code of conduct: CODE_OF_CONDUCT.md
- Roadmap: ROADMAP.rst
Awesome Lists containing this project
- starred-awesome - django-fobi - Form generator/builder application for Django done right: customisable, modular, user- and developer- friendly. (Python)
- awesome-starred - barseghyanartur/django-fobi - Form generator/builder application for Django done right: customisable, modular, user- and developer- friendly. (python)
README
===========
django-fobi
===========
`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)... image:: https://img.shields.io/pypi/v/django-fobi.svg
:target: https://pypi.python.org/pypi/django-fobi
:alt: PyPI Version.. image:: https://img.shields.io/pypi/pyversions/django-fobi.svg
:target: https://pypi.python.org/pypi/django-fobi/
:alt: Supported Python versions.. image:: https://github.com/barseghyanartur/django-fobi/workflows/test/badge.svg?branch=main
:target: https://github.com/barseghyanartur/django-fobi/actions?query=workflow%3Atest
:alt: Build Status.. image:: https://readthedocs.org/projects/django-fobi/badge/?version=latest
:target: http://django-fobi.readthedocs.io/en/latest/?badge=latest
:alt: Documentation Status.. image:: https://img.shields.io/badge/license-GPL--2.0--only%20OR%20LGPL--2.1--or--later-blue.svg
:target: https://github.com/barseghyanartur/django-fobi/#License
:alt: GPL-2.0-only OR LGPL-2.1-or-later.. image:: https://coveralls.io/repos/github/barseghyanartur/django-fobi/badge.svg?branch=main
:target: https://coveralls.io/github/barseghyanartur/django-fobi?branch=main
:alt: CoveragePrerequisites
=============
- Django 2.2, 3.0, 3.1, 3.2, 4.0 and 4.1.
- Python 3.6, 3.7, 3.8, 3.9, 3.10 and 3.11.Key concepts
============
- Each form consists of elements. Form elements are divided into groups:(a) form fields (input field, textarea, hidden field, file field, etc.).
(b) content (presentational) elements (text, image, embed video, etc.).
(c) security elements (captcha, 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
============================
- User-friendly GUI to quickly build forms.
- Large variety of `Bundled form element plugins`_. Most of the Django fields
are supported. `HTML5 fields`_ are supported as well.
- `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).
- Anti-spam solutions like `CAPTCHA
`_,
`ReCAPTCHA
`_,
`Honeypot
`_
or `Invisible reCAPTCHA
`__
come out of the box (CAPTCHA and ReCAPTCHA do require additional third-party
apps to be installed; Invisible reCAPTCHA doesn't).
- In addition to standard form elements, there are cosmetic (presentational)
form elements (for adding a piece of text, image or a embed video)
alongside standard form elements.
- Data handling in plugins (form handlers). Save the data, mail it to some
address or re-post it to some other endpoint. See the
`Bundled form handler plugins`_ for more information.
- Developer-friendly API, which allows to edit existing or build new form
fields and handlers without touching the core.
- Support for custom user model.
- Class based views (and class-based permissions). Forms have an
owner (``auth.User``). Default permissions are set for the owner, but
class-based views provide a lot of freedom and can be easily customized.
- `Theming`_. There are 4 ready to use `Bundled themes`_: "Bootstrap 3",
"Foundation 5", "Simple" (with editing interface in style of Django admin)
and "DjangoCMS admin style" theme (which is another simple theme with editing
interface in style of `djangocms-admin-style
`_).
- Implemented `integration with Django REST framework
`_.
- Implemented `integration with Wagtail
`_
(in a form of a Wagtail page).
- Implemented `integration with FeinCMS
`_
(in a form of a FeinCMS page widget).
- Implemented `integration with DjangoCMS
`_
(in a form of a DjangoCMS page plugin).
- Implemented `integration with Mezzanine
`_
(in a form of a Mezzanine page).
- Reordering of form elements using drag-n-drop.
- Data export (`DB store
`_
form handler plugin) into XLS/CSV format.
- `Dynamic initial values`_ for form elements.
- Import/export forms to/from JSON format.
- Import forms from MailChimp using `mailchimp importer
`_.Roadmap
=======
Some of the upcoming/in-development features/improvements are:- Implement disabling forms based on dates.
- Cloning of forms.
- JSON schema support.
- Webpack integration.
- Improved Django REST framework OPTIONS.
- Bootstrap 5 support.
- Foundation 6 support.See the `TODOS
`_
for the full list of planned-, pending- in-development- or to-be-implemented
features.Some screenshots
================
See the documentation for some screen shots:- `ReadTheDocs `_
Demo
====
Live demo
---------
See the `live demo app `_ on Heroku.
Additionally, see the `Django REST framework integration demo
`_.Credentials:
- username: test_user
- password: test_userRun 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.Grab the latest ``django_fobi_example_app_installer.sh``:
.. code-block:: sh
wget https://raw.github.com/barseghyanartur/django-fobi/stable/examples/django_fobi_example_app_installer.sh
Assign execute rights to the installer and run the
`django_fobi_example_app_installer.sh`:.. code-block:: sh
chmod +x django_fobi_example_app_installer.sh
./django_fobi_example_app_installer.shOpen your browser and test the app.
Dashboard:
- URL: http://127.0.0.1:8001/fobi/
- Admin username: test_admin
- Admin password: testDjango admin interface:
- URL: http://127.0.0.1:8001/admin/
- Admin username: test_admin
- Admin password: testIf quick installer doesn't work for you, see the manual steps on running the
`example project
`_.Quick start
===========
See the `quick start `_.Installation
============(1) Install latest stable version from PyPI:
.. code-block:: sh
pip install django-fobi
Or latest stable version from GitHub:
.. code-block:: sh
pip install https://github.com/barseghyanartur/django-fobi/archive/stable.tar.gz
(2) 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... code-block:: python
INSTALLED_APPS = (
# Used by fobi
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',# ...
# `django-fobi` core
'fobi',# `django-fobi` themes
'fobi.contrib.themes.bootstrap3', # Bootstrap 3 theme
'fobi.contrib.themes.foundation5', # Foundation 5 theme
'fobi.contrib.themes.simple', # Simple theme# `django-fobi` form elements - fields
'fobi.contrib.plugins.form_elements.fields.boolean',
'fobi.contrib.plugins.form_elements.fields.checkbox_select_multiple',
'fobi.contrib.plugins.form_elements.fields.date',
'fobi.contrib.plugins.form_elements.fields.date_drop_down',
'fobi.contrib.plugins.form_elements.fields.datetime',
'fobi.contrib.plugins.form_elements.fields.decimal',
'fobi.contrib.plugins.form_elements.fields.email',
'fobi.contrib.plugins.form_elements.fields.file',
'fobi.contrib.plugins.form_elements.fields.float',
'fobi.contrib.plugins.form_elements.fields.hidden',
'fobi.contrib.plugins.form_elements.fields.input',
'fobi.contrib.plugins.form_elements.fields.integer',
'fobi.contrib.plugins.form_elements.fields.ip_address',
'fobi.contrib.plugins.form_elements.fields.null_boolean',
'fobi.contrib.plugins.form_elements.fields.password',
'fobi.contrib.plugins.form_elements.fields.radio',
'fobi.contrib.plugins.form_elements.fields.regex',
'fobi.contrib.plugins.form_elements.fields.select',
'fobi.contrib.plugins.form_elements.fields.select_model_object',
'fobi.contrib.plugins.form_elements.fields.select_multiple',
'fobi.contrib.plugins.form_elements.fields.select_multiple_model_objects',
'fobi.contrib.plugins.form_elements.fields.slug',
'fobi.contrib.plugins.form_elements.fields.text',
'fobi.contrib.plugins.form_elements.fields.textarea',
'fobi.contrib.plugins.form_elements.fields.time',
'fobi.contrib.plugins.form_elements.fields.url',# `django-fobi` form elements - content elements
'fobi.contrib.plugins.form_elements.test.dummy',
'easy_thumbnails', # Required by `content_image` plugin
'fobi.contrib.plugins.form_elements.content.content_image',
'fobi.contrib.plugins.form_elements.content.content_image_url',
'fobi.contrib.plugins.form_elements.content.content_text',
'fobi.contrib.plugins.form_elements.content.content_video',# `django-fobi` form handlers
'fobi.contrib.plugins.form_handlers.db_store',
'fobi.contrib.plugins.form_handlers.http_repost',
'fobi.contrib.plugins.form_handlers.mail',
'fobi.contrib.plugins.form_handlers.mail_sender',# Other project specific apps
'foo', # Test app
# ...
)(3) Make appropriate changes to the ``TEMPLATES`` of the your projects'
Django settings.And ``fobi.context_processors.theme`` and
``fobi.context_processors.dynamic_values``. See the following example... code-block:: python
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [(os.path.join('path', 'to', 'your', 'templates'))],
'OPTIONS': {
'context_processors': [
"django.template.context_processors.debug",
'django.template.context_processors.request',
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
"fobi.context_processors.theme", # Important!
"fobi.context_processors.dynamic_values", # Optional
],
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
'admin_tools.template_loaders.Loader',
],
'debug': DEBUG_TEMPLATE,
}
},
]Make sure that ``django.core.context_processors.request`` is in
``context_processors`` too.(4) Configure URLs
Add the following line to urlpatterns of your `urls` module.
.. code-block:: python
# View URLs
re_path(r'^fobi/', include('fobi.urls.class_based.view')),# Edit URLs
re_path(r'^fobi/', include('fobi.urls.class_based.edit')),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... code-block:: python
# DB Store plugin URLs
re_path(r'^fobi/plugins/form-handlers/db-store/',
include('fobi.contrib.plugins.form_handlers.db_store.urls')),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... code-block:: sh
path/to/sample_textarea/
├── __init__.py
├── fobi_form_elements.py # Where plugins are defined and registered
├── forms.py # Plugin configuration form
└── widgets.py # Where plugins widgets are definedForm 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.path/to/sample_textarea/fobi_form_elements.py
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A single form element plugin is registered by its' UID.Required imports.
.. code-block:: python
from django import forms
from fobi.base import FormFieldPlugin, form_element_plugin_registry
from path.to.sample_textarea.forms import SampleTextareaFormDefining the Sample textarea plugin.
.. code-block:: python
class SampleTextareaPlugin(FormFieldPlugin):
"""Sample textarea plugin."""uid = "sample_textarea"
name = "Sample Textarea"
form = SampleTextareaForm
group = "Samples" # Group to which the plugin belongs todef get_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.
.. code-block:: python
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):
.. code-block:: python
def submit_plugin_form_data(self,
form_entry,
request,
form,
form_element_entries=None,
**kwargs):
"""Submit plugin form data."""
# Get the file path
file_path = form.cleaned_data.get(self.data.name, None)
if file_path:
# Handle the upload
saved_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``
return formIn 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``.Required imports.
.. code-block:: python
from django import forms
from fobi.base import BasePluginFormForm for for ``SampleTextareaPlugin`` form element plugin.
.. code-block:: python
class SampleTextareaForm(forms.Form, BasePluginForm):
"""Sample textarea form."""plugin_data_fields = [
("name", ""),
("label", ""),
("initial", ""),
("required", False)
]name = forms.CharField(label="Name", required=True)
label = forms.CharField(label="Label", required=True)
initial = forms.CharField(label="Initial", required=False)
required = forms.BooleanField(label="Required", required=False)Note that although it's not being checked in the code, but for form
field plugins the following fields should be present in the plugin
form (``BasePluginForm``) and the form plugin (``FormFieldPlugin``):- name
In some cases, you might want to do something with the data
before it gets saved. For that purpose, ``save_plugin_data`` method
has been introduced.See the following `example
`_... code-block:: python
def save_plugin_data(self, request=None):
"""Saving the plugin data and moving the file."""
file_path = self.cleaned_data.get('file', None)
if file_path:
saved_image = handle_uploaded_file(IMAGES_UPLOAD_DIR, file_path)
self.cleaned_data['file'] = saved_imagepath/to/sample_textarea/widgets.py
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Required imports... code-block:: python
from fobi.base import FormElementPluginWidget
Defining the base plugin widget.
.. code-block:: python
class BaseSampleTextareaPluginWidget(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).Required imports.
.. code-block:: python
from fobi.base import form_element_plugin_widget_registry
from path.to.sample_textarea.widgets import BaseSampleTextareaPluginWidgetDefine the theme specific plugin.
.. code-block:: python
class SampleTextareaPluginWidget(BaseSampleTextareaPluginWidget):
"""Sample textarea plugin widget."""theme_uid = 'bootstrap3' # Theme for which the widget is loaded
media_js = [
'sample_layout/js/fobi.plugins.form_elements.sample_textarea.js',
]
media_css = [
'sample_layout/css/fobi.plugins.form_elements.sample_textarea.css',
]Register the widget.
.. code-block:: python
form_element_plugin_widget_registry.register(SampleTextareaPluginWidget)
Form element plugin final steps
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Now, that everything is ready, make sure your plugin module is added to
``INSTALLED_APPS``... code-block:: python
INSTALLED_APPS = (
# ...
'path.to.sample_textarea',
# ...
)Afterwards, go to terminal and type the following command.
.. code-block:: sh
./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.Dashboard URL: http://127.0.0.1:8000/fobi/
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... code-block:: text
path/to/sample_mail/
├── __init__.py
├── fobi_form_handlers.py # Where plugins are defined and registered
└── forms.py # Plugin configuration formForm handler plugins should be registered in "fobi_form_handlers.py" file.
Each plugin module should be put into the ``INSTALLED_APPS`` of your Django
projects' settings.path/to/sample_mail/fobi_form_handlers.py
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A single form handler plugin is registered by its' UID.Required imports.
.. code-block:: python
import json
from django.core.mail import send_mail
from fobi.base import FormHandlerPlugin, form_handler_plugin_registry
from path.to.sample_mail.forms import SampleMailFormDefining the Sample mail handler plugin.
.. code-block:: python
class SampleMailHandlerPlugin(FormHandlerPlugin):
"""Sample mail handler plugin."""uid = "sample_mail"
name = _("Sample mail")
form = SampleMailFormdef run(self, form_entry, request, form, form_element_entries=None):
"""To be executed by handler."""
send_mail(
self.data.subject,
json.dumps(form.cleaned_data),
self.data.from_email,
[self.data.to_email],
fail_silently=True
)Register the plugin
.. code-block:: python
form_handler_plugin_registry.register(SampleMailHandlerPlugin)
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:.. code-block:: python
def plugin_data_repr(self):
"""Human readable representation of plugin data.:return string:
"""
return self.data.__dict__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``.Required imports.
.. code-block:: python
from django import forms
from django.utils.translation import gettext_lazy as _
from fobi.base import BasePluginFormDefining the form for Sample mail handler plugin.
.. code-block:: python
class MailForm(forms.Form, BasePluginForm):
"""Mail form."""plugin_data_fields = [
("from_name", ""),
("from_email", ""),
("to_name", ""),
("to_email", ""),
("subject", ""),
("body", ""),
]from_name = forms.CharField(label=_("From name"), required=True)
from_email = forms.EmailField(label=_("From email"), required=True)
to_name = forms.CharField(label=_("To name"), required=True)
to_email = forms.EmailField(label=_("To email"), required=True)
subject = forms.CharField(label=_("Subject"), required=True)
body = forms.CharField(
label=_("Body"),
required=False,
widget=forms.widgets.Textarea
)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... code-block:: python
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.The following example is taken from the "db_store" plugin.
.. code-block:: python
def custom_actions(self):
"""Adding a link to view the saved form entries.:return iterable:
"""
return (
(
reverse('fobi.contrib.plugins.form_handlers.db_store.view_saved_form_data_entries'),
_("View entries"),
'glyphicon glyphicon-list'
),
)Form handler plugin final steps
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Do not forget to add the form handler plugin module to ``INSTALLED_APPS``... code-block:: python
INSTALLED_APPS = (
# ...
'path.to.sample_mail',
# ...
)Afterwards, go to terminal and type the following command.
.. code-block:: sh
./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... code-block:: text
path/to/sample_importer/
├── templates
│ └── sample_importer
│ ├── 0.html
│ └── 1.html
├── __init__.py
├── fobi_form_importers.py # Where plugins are defined and registered
├── forms.py # Wizard forms
└── views.py # Wizard viewsForm importer plugins should be registered in "fobi_form_importers.py" file.
Each plugin module should be put into the ``INSTALLED_APPS`` of your Django
projects' settings.path/to/sample_importer/fobi_form_importers.py
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A single form importer plugin is registered by its' UID.Required imports.
.. code-block:: python
from django.utils.translation import gettext_lazy as _
from fobi.form_importers import BaseFormImporter, form_importer_plugin_registry
from fobi.contrib.plugins.form_elements import fields
from path.to.sample_importer.views import SampleImporterWizardViewDefining the Sample importer plugin.
.. code-block:: python
class SampleImporterPlugin(FormHandlerPlugin):
"""Sample importer plugin."""uid = 'sample_importer'
name = _("Sample importer")
wizard = SampleImporterWizardView
templates = [
'sample_importer/0.html',
'sample_importer/1.html',
]# field_type (at importer): uid (django-fobi)
fields_mapping = {
# Implemented
'email': fields.email.UID,
'text': fields.text.UID,
'number': fields.integer.UID,
'dropdown': fields.select.UID,
'date': fields.date.UID,
'url': fields.url.UID,
'radio': fields.radio.UID,# Transformed into something else
'address': fields.text.UID,
'zip': fields.text.UID,
'phone': fields.text.UID,
}# Django standard: remote
field_properties_mapping = {
'label': 'name',
'name': 'tag',
'help_text': 'helptext',
'initial': 'default',
'required': 'req',
'choices': 'choices',
}field_type_prop_name = 'field_type'
position_prop_name = 'order'def extract_field_properties(self, field_data):
field_properties = {}
for prop, val in self.field_properties_mapping.items():
if val in field_data:
if 'choices' == val:
field_properties[prop] = "\n".join(field_data[val])
else:
field_properties[prop] = field_data[val]
return field_propertiesform_importer_plugin_registry.register(SampleImporter)
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.
.. code-block:: python
from django import forms
from django.utils.translation import gettext_lazy as _
from sample_service_api import sample_api # Just an imaginary API clientDefining the form for Sample importer plugin.
.. code-block:: python
class SampleImporterStep1Form(forms.Form):
"""First form the the wizard."""api_key = forms.CharField(required=True)
class SampleImporterStep2Form(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' in kwargs:
self._api_key = kwargs.pop('api_key', None)super(SampleImporterStep2Form, self).__init__(*args, **kwargs)
if self._api_key:
client = sample_api.Api(self._api_key)
lists = client.lists.list()
choices = [(l['id'], l['name']) for l in lists['data']]
self.fields['list_id'].choices = choicespath/to/sample_importer/views.py
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The wizard views.Required imports.
.. code-block:: python
from sample_service_api import sample_api # Just an imaginary API client
from django.shortcuts import redirect
from django.urls import reverse
from django.contrib import messages
from django.utils.translation import gettext_lazy as _# For django LTE 1.8 import from `django.contrib.formtools.wizard.views`
from formtools.wizard.views import SessionWizardViewfrom path.to.sample_importer.forms import (
SampleImporterStep1Form,
SampleImporterStep2Form,
)Defining the wizard view for Sample importer plugin.
.. code-block:: python
class SampleImporterWizardView(SessionWizardView):
"""Sample importer wizard view."""form_list = [SampleImporterStep1Form, SampleImporterStep2Form]
def get_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 {}def done(self, form_list, **kwargs):
"""After all forms are submitted."""
# Merging cleaned data into one dict
cleaned_data = {}
for form in form_list:
cleaned_data.update(form.cleaned_data)# Connecting to sample client API
client = sample_client.Api(cleaned_data['api_key'])# Fetching the form data
form_data = client.lists.merge_vars(
id={'list_id': cleaned_data['list_id']}
)# We need the first form only
try:
form_data = form_data['data'][0]
except Exception as err:
messages.warning(
self.request,
_('Selected form could not be imported due errors.')
)
return redirect(reverse('fobi.dashboard'))# Actually, import the form
form_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'])
)return redirect("{0}".format(redirect_url))
Form importer plugin final steps
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Do not forget to add the form importer plugin module to ``INSTALLED_APPS``... code-block:: python
INSTALLED_APPS = (
# ...
'path.to.sample_importer',
# ...
)Afterwards, go to terminal and type the following command.
.. code-block:: sh
./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... code-block:: text
path/to/foo/
├── __init__.py
└── fobi_form_callbacks.py # Where callbacks are defined and registeredSee the callback example below.
Required imports.
.. code-block:: python
from fobi.constants import (
CALLBACK_BEFORE_FORM_VALIDATION,
CALLBACK_FORM_VALID_BEFORE_SUBMIT_PLUGIN_FORM_DATA,
CALLBACK_FORM_VALID,
CALLBACK_FORM_VALID_AFTER_FORM_HANDLERS,
CALLBACK_FORM_INVALID,
)
from fobi.base import FormCallback, form_callback_registryDefine and register the callback
.. code-block:: python
class SampleFooCallback(FormCallback):
"""Sample foo callback."""stage = CALLBACK_FORM_VALID
def callback(self, form_entry, request, form):
"""Define your callback code here."""
print("Great! Your form is valid!")form_callback_registry.register(SampleFooCallback)
Add the callback module to ``INSTALLED_APPS``.
.. code-block:: python
INSTALLED_APPS = (
# ...
'path.to.foo',
# ...
)Class-based views
=================
Views
-----
Migration to class based views is simple. Only your project's ``urls.py``
would change:.. code-block:: python
urlpatterns = [
# ...
re_path(r'^fobi/', include('fobi.urls.class_based.view')),
re_path(r'^fobi/', include('fobi.urls.class_based.edit')),
# ...
]To use function based views, simply replace the previous line with:
.. code-block:: python
urlpatterns = [
# ...
re_path(r'^fobi/', include('fobi.urls.view')),
re_path(r'^fobi/', include('fobi.urls.edit')),
# ...
]Permissions
-----------Class-based permissions work only in combination with class-based views.
Example:
.. code-block:: python
from fobi.permissions.definitions import edit_form_entry_permissions
from fobi.permissions.generic import BasePermission
from fobi.permissions.helpers import (
any_permission_required_func, login_required,
)class EditFormEntryPermission(BasePermission):
"""Permission to edit form entries."""def has_permission(self, request, view) -> bool:
return login_required(request) and any_permission_required_func(
edit_form_entry_permissions
)(request.user)def has_object_permission(self, request, view, obj) -> bool:
return login_required(request) and any_permission_required_func(
edit_form_entry_permissions
)(request.user) and obj.user == request.userAnd then in your view:
.. code-block:: python
from fobi.views.class_based import EditFormEntryView
class MyEditFormEntryView(EditFormEntryView):
"""EditFormEntryView."""permission_classes = (EditFormEntryPermission,)
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... code-block:: text
path/to/sample_theme/
├── static
│ ├── css
│ │ └── sample_theme.css
│ └── js
│ └── sample_theme.js
├── templates
│ └── sample_theme
│ ├── _base.html
│ ├── add_form_element_entry.html
│ ├── ...
│ └── view_form_entry_ajax.html
├── __init__.py
├── fobi_form_elements.py
└── fobi_themes.py # Where themes are defined and registeredSee the theme example below.
.. code-block:: python
from django.utils.translation import gettext_lazy as _
from fobi.base import BaseTheme, theme_registry
class SampleTheme(BaseTheme):
"""Sample theme."""uid = 'sample'
name = _("Sample")media_css = (
'sample_theme/css/sample_theme.css',
'css/fobi.core.css',
)media_js = (
'js/jquery-1.10.2.min.js',
'jquery-ui/js/jquery-ui-1.10.3.custom.min.js',
'js/jquery.slugify.js',
'js/fobi.core.js',
'sample_theme/js/sample_theme.js',
)# Form element specific
form_element_html_class = 'form-control'
form_radio_element_html_class = 'radio'
form_element_checkbox_html_class = 'checkbox'form_edit_form_entry_option_class = 'glyphicon glyphicon-edit'
form_delete_form_entry_option_class = 'glyphicon glyphicon-remove'
form_list_container_class = 'list-inline'# Templates
master_base_template = 'sample_theme/_base.html'
base_template = 'sample_theme/base.html'form_ajax = 'sample_theme/snippets/form_ajax.html'
form_snippet_template_name = 'sample_theme/snippets/form_snippet.html'
form_properties_snippet_template_name = 'sample_theme/snippets/form_properties_snippet.html'
messages_snippet_template_name = 'sample_theme/snippets/messages_snippet.html'add_form_element_entry_template = 'sample_theme/add_form_element_entry.html'
add_form_element_entry_ajax_template = 'sample_theme/add_form_element_entry_ajax.html'add_form_handler_entry_template = 'sample_theme/add_form_handler_entry.html'
add_form_handler_entry_ajax_template = 'sample_theme/add_form_handler_entry_ajax.html'create_form_entry_template = 'sample_theme/create_form_entry.html'
create_form_entry_ajax_template = 'bootstrap3/create_form_entry_ajax.html'dashboard_template = 'sample_theme/dashboard.html'
edit_form_element_entry_template = 'sample_theme/edit_form_element_entry.html'
edit_form_element_entry_ajax_template = 'sample_theme/edit_form_element_entry_ajax.html'edit_form_entry_template = 'sample_theme/edit_form_entry.html'
edit_form_entry_ajax_template = 'sample_theme/edit_form_entry_ajax.html'edit_form_handler_entry_template = 'sample_theme/edit_form_handler_entry.html'
edit_form_handler_entry_ajax_template = 'sample_theme/edit_form_handler_entry_ajax.html'form_entry_submitted_template = 'sample_theme/form_entry_submitted.html'
form_entry_submitted_ajax_template = 'sample_theme/form_entry_submitted_ajax.html'view_form_entry_template = 'sample_theme/view_form_entry.html'
view_form_entry_ajax_template = 'sample_theme/view_form_entry_ajax.html'Registering the ``SampleTheme`` plugin.
.. code-block:: python
theme_registry.register(SampleTheme)
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:.. code-block:: python
# `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... code-block:: html