Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Commit

Permalink
Adds custom field parser support
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krienbühl committed May 19, 2015
1 parent 616fbb5 commit 0bbc86a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
14 changes: 12 additions & 2 deletions onegov/form/parser/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Form,
with_options
)
from onegov.form.utils import label_to_field_id
from onegov.form.parser.grammar import document
from wtforms import PasswordField, StringField, TextAreaField
from wtforms.widgets import TextArea
Expand All @@ -12,7 +13,7 @@
doc = document()


def parse_form(text):
def parse_form(text, custom_fields={}):
""" Takes the given form text, parses it and returns a WTForms form
class (not an instance of it).
Expand Down Expand Up @@ -51,6 +52,15 @@ class (not an instance of it).
label=block.label,
required=block.required
)
elif block.type == 'custom':
if block.custom_id in custom_fields:
builder.add_field(
field_class=custom_fields[block.custom_id],
label=block.label,
required=block.required
)
else:
raise NotImplementedError
else:
raise NotImplementedError

Expand Down Expand Up @@ -85,7 +95,7 @@ def add_field(self, field_class, label, required, **kwargs):
if required:
validators.insert(0, InputRequired())

field_id = label.lower().replace(' ', '_')
field_id = label_to_field_id(label)

setattr(self.form_class, field_id, field_class(
label=label,
Expand Down
16 changes: 16 additions & 0 deletions onegov/form/tests/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import pytest

from onegov.form.parser import parse_form
from textwrap import dedent
from wtforms.fields.html5 import EmailField


def test_parse_text():
Expand Down Expand Up @@ -67,3 +70,16 @@ def test_parse_fieldsets():
assert fieldsets[2].label is None
assert not fieldsets[2].is_visible
assert fieldsets[2]['comment'].label.text == 'Comment'


def test_parse_custom_fields():

with pytest.raises(NotImplementedError):
form = parse_form("E-Mail = /E-Mail")

form = parse_form("E-Mail = /E-Mail", custom_fields={
'e-mail': EmailField
})()

assert form.e_mail.label.text == 'E-Mail'
assert isinstance(form.e_mail, EmailField)
12 changes: 12 additions & 0 deletions onegov/form/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import re

from unidecode import unidecode

_unwanted_characters = re.compile(r'[^a-zA-Z0-9]+')


def label_to_field_id(label):
clean = unidecode(label).strip(' ').lower()
clean = _unwanted_characters.sub('_', clean)

return clean
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ def get_long_description():
install_requires=[
'pyparsing',
'wtforms',
'wtforms-components[color]'
'wtforms-components[color]',
'unidecode'
],
extras_require=dict(
test=[
Expand Down

0 comments on commit 0bbc86a

Please sign in to comment.