|
| 1 | +=============================== |
| 2 | +External Field Plugin Mechanism |
| 3 | +=============================== |
| 4 | + |
| 5 | +We've included a mechanism to add your own fields to the collection of available fields in ``django-formidable``. |
| 6 | + |
| 7 | +It'll be possible to: |
| 8 | + |
| 9 | +* define a new form using this new type of field, |
| 10 | +* store their definition and parameters in a Formidable object instance (and thus, in the database), |
| 11 | +* using this form definition, validate the end-user data when filling this form against your field business logic mechanism. |
| 12 | + |
| 13 | +For the sake of the example, let's say you want to add a "Color Picker" field in django-formidable. You'll have to create a django library project that we'll call ``django-formidable-color-picker``. Let's say that this module has its own ``setup.py`` with the appropriate scripts to be installed in dev mode using ``pip install -e ./``. |
| 14 | + |
| 15 | +Let's also say that you have added it in your ``INSTALLED_APPS``. |
| 16 | + |
| 17 | +Tree structure |
| 18 | +============== |
| 19 | + |
| 20 | +:: |
| 21 | + |
| 22 | + . |
| 23 | + ├── formidable_color_picker |
| 24 | + │ ├── apps.py |
| 25 | + │ ├── __init__.py |
| 26 | + │ ├── serializers.py |
| 27 | + ├── setup.cfg |
| 28 | + └── setup.py |
| 29 | + |
| 30 | +Loading the field for building time |
| 31 | +=================================== |
| 32 | + |
| 33 | +The first file we're going to browse is :file:`serializers.py`. Here's a minimal version of it: |
| 34 | + |
| 35 | + |
| 36 | +.. code-block:: python |
| 37 | +
|
| 38 | + from formidable.register import load_serializer, FieldSerializerRegister |
| 39 | + from formidable.serializers.fields import FieldSerializer, BASE_FIELDS |
| 40 | +
|
| 41 | + field_register = FieldSerializerRegister.get_instance() |
| 42 | +
|
| 43 | +
|
| 44 | + @load_serializer(field_register) |
| 45 | + class ColorPickerFieldSerializer(FieldSerializer): |
| 46 | +
|
| 47 | + type_id = 'color_picker' |
| 48 | +
|
| 49 | + class Meta(FieldSerializer.Meta): |
| 50 | + fields = BASE_FIELDS |
| 51 | +
|
| 52 | +Then you're going to need to make sure that Django would catch this file at startup, and thus load the Serializer. It's done via the :file:`apps.py` file. |
| 53 | + |
| 54 | +.. code-block:: python |
| 55 | +
|
| 56 | + from __future__ import absolute_import |
| 57 | + from django.apps import AppConfig |
| 58 | +
|
| 59 | +
|
| 60 | + class FormidableColorPickerConfig(AppConfig): |
| 61 | + """ |
| 62 | + Formidable Color Picker configuration class. |
| 63 | + """ |
| 64 | + name = 'formidable_color_picker' |
| 65 | +
|
| 66 | + def ready(self): |
| 67 | + """ |
| 68 | + Load external serializer when ready |
| 69 | + """ |
| 70 | + from . import serializers # noqa |
| 71 | +
|
| 72 | +As you'd do for any other Django application, you can now add this line to your :file:`__init__.py` file at the root of the python module: |
| 73 | + |
| 74 | +.. code-block:: python |
| 75 | +
|
| 76 | + default_app_config = 'formidable_color_picker.apps.FormidableColorPickerConfig' |
| 77 | +
|
| 78 | +Check that it's working |
| 79 | +----------------------- |
| 80 | + |
| 81 | +Loading the Django shell: |
| 82 | + |
| 83 | +.. code-block:: pycon |
| 84 | +
|
| 85 | + >>> from formidable.serializers import FormidableSerializer |
| 86 | + >>> data = { |
| 87 | + "label": "Color picker test", |
| 88 | + "description": "May I help you pick your favorite color?", |
| 89 | + "fields": [{ |
| 90 | + "slug": "color", |
| 91 | + "label": "What is your favorite color?", |
| 92 | + "type_id": "color_picker", |
| 93 | + "accesses": [], |
| 94 | + }] |
| 95 | + } |
| 96 | + >>> instance = FormidableSerializer(data=data) |
| 97 | + >>> instance.is_valid() |
| 98 | + True |
| 99 | + >>> formidable_instance = instance.save() |
| 100 | +
|
| 101 | +This means that you can create a form with a field whose type is not in ``django-formidable`` code, but in your module's. |
| 102 | + |
| 103 | +Then you can also retrieve this instance JSON defintion |
| 104 | + |
| 105 | +.. code-block:: pycon |
| 106 | +
|
| 107 | + >>> import json |
| 108 | + >>> print(json.dumps(formidable_instance.to_json(), indent=2)) |
| 109 | + { |
| 110 | + "label": "Color picker test", |
| 111 | + "description": "May I help you pick your favorite color?", |
| 112 | + "fields": [ |
| 113 | + { |
| 114 | + "slug": "color", |
| 115 | + "label": "What is your favorite color?", |
| 116 | + "type_id": "color_picker", |
| 117 | + "placeholder": null, |
| 118 | + "description": null, |
| 119 | + "accesses": [], |
| 120 | + "validations": [], |
| 121 | + "defaults": [], |
| 122 | + } |
| 123 | + ], |
| 124 | + "id": 42, |
| 125 | + "conditions": [], |
| 126 | + "version": 5 |
| 127 | + } |
| 128 | +
|
| 129 | +Making your field a bit more clever |
| 130 | +=================================== |
| 131 | + |
| 132 | +Let's say that colors can be expressed in two ways: RGB tuple (``rgb``) or Hexadecimal expression (``hex``). This means your field has to be parametrized in order to store this information at the builder step. Let's imagine your JSON payload would look like: |
| 133 | + |
| 134 | +.. code-block:: json |
| 135 | +
|
| 136 | + { |
| 137 | + "label": "Color picker test", |
| 138 | + "description": "May I help you pick your favorite color?", |
| 139 | + "fields": [{ |
| 140 | + "slug": "color", |
| 141 | + "label": "What is your favorite color?", |
| 142 | + "type_id": "color_picker", |
| 143 | + "accesses": [], |
| 144 | + "color_format": "hex" |
| 145 | + }] |
| 146 | + } |
| 147 | +
|
| 148 | +You want then to make sure that your user would not send a wrong parameter, as in these BAD examples: |
| 149 | + |
| 150 | +.. code-block:: json |
| 151 | +
|
| 152 | + "color_format": "" |
| 153 | + "color_format": "foo" |
| 154 | + "color_format": "wrong" |
| 155 | +
|
| 156 | +For this specific field, you only want one parameter and its key is ``format`` and its values are only ``hex`` or ``rgb`` |
| 157 | + |
| 158 | +Let's add some validation in your Serializer, then. |
| 159 | + |
| 160 | +.. code-block:: python |
| 161 | +
|
| 162 | + from rest_framework import serializers |
| 163 | + from formidable.register import load_serializer, FieldSerializerRegister |
| 164 | + from formidable.serializers.fields import FieldSerializer, BASE_FIELDS |
| 165 | +
|
| 166 | + field_register = FieldSerializerRegister.get_instance() |
| 167 | +
|
| 168 | +
|
| 169 | + @load_serializer(field_register) |
| 170 | + class ColorPickerFieldSerializer(FieldSerializer): |
| 171 | +
|
| 172 | + type_id = 'color_picker' |
| 173 | +
|
| 174 | + allowed_formats = ('rgb', 'hex') |
| 175 | + default_error_messages = { |
| 176 | + "missing_parameter": "You need a `format` parameter for this field", |
| 177 | + "invalid_format": "Invalid format: `{format}` is not one of {formats}." |
| 178 | + } |
| 179 | +
|
| 180 | + class Meta(FieldSerializer.Meta): |
| 181 | + config_fields = ('color_format', ) |
| 182 | + fields = BASE_FIELDS + ('parameters',) |
| 183 | +
|
| 184 | + def to_internal_value(self, data): |
| 185 | + data = super(ColorPickerFieldSerializer, self).to_internal_value(data) |
| 186 | + # Check if the parameters are compliant |
| 187 | + format = data.get('color_format') |
| 188 | + if format is None: |
| 189 | + self.fail('missing_parameter') |
| 190 | +
|
| 191 | + if format not in self.allowed_formats: |
| 192 | + self.fail("invalid_format", |
| 193 | + format=format, formats=self.allowed_formats) |
| 194 | +
|
| 195 | + return super(ColorPickerFieldSerializer, self).to_internal_value(data) |
| 196 | +
|
| 197 | +.. note:: Full example |
| 198 | + |
| 199 | + You may browse this as a complete directly usable example in `the following repository: "django-formidable-color-picker" <https://github.com/peopledoc/django-formidable-color-picker>`_ |
0 commit comments