Skip to content

Commit

Permalink
Merge branch 'fayazkhan-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
kvesteri committed Jul 25, 2017
2 parents 9cdc59d + bfc25f8 commit f7d7045
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 7 deletions.
4 changes: 2 additions & 2 deletions docs/advanced.rst
Expand Up @@ -48,13 +48,13 @@ In order to make WTForms-Alchemy work with `Flask-WTF`_ you need the following s
::


from flask.ext.wtf import Form
from flask_wtf import FlaskForm
from wtforms_alchemy import model_form_factory
# The variable db here is a SQLAlchemy object instance from
# Flask-SQLAlchemy package
from myproject.extensions import db

BaseModelForm = model_form_factory(Form)
BaseModelForm = model_form_factory(FlaskForm)

class ModelForm(BaseModelForm):
@classmethod
Expand Down
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -25,6 +25,7 @@ def get_version():

extras_require = {
'test': [
'enum34',
'pytest>=2.3',
'Pygments>=1.2',
'Jinja2>=2.3',
Expand Down
41 changes: 39 additions & 2 deletions tests/test_types.py
Expand Up @@ -44,11 +44,14 @@
)
from wtforms_alchemy.utils import ClassMap

passlib = None
try:
import passlib # noqa
except ImportError:
pass
passlib = None
try:
from enum import Enum
except ImportError:
Enum = None


class UnknownType(sa.types.UserDefinedType):
Expand Down Expand Up @@ -252,6 +255,40 @@ def test_choice_type_uses_custom_coerce_func(self):
form = self.form_class(obj=model)
assert '<option selected value="2">' in str(form.test_column)

@mark.xfail('Enum is None')
def test_choice_type_with_enum(self):
class Choice(Enum):
choice1 = 1
choice2 = 2

def __str__(self):
return self.name
self.init(type_=ChoiceType(Choice))
self.assert_type('test_column', SelectField)
assert self.form_class().test_column.choices == [
(1, u'choice1'), (2, u'choice2')]

@mark.xfail('Enum is None')
@mark.parametrize(
['type_', 'impl'],
[
(int, sa.Integer()),
(str, sa.String())
]
)
def test_choice_type_with_enum_uses_custom_coerce_func(self, type_, impl):
class Choice(Enum):
choice1 = type_(1)
choice2 = type_(2)

def __str__(self):
return self.name
self.init(type_=ChoiceType(Choice, impl=impl))
self.assert_type('test_column', SelectField)
model = self.ModelTest(test_column=type_(2))
form = self.form_class(obj=model)
assert '<option selected value="2">' in str(form.test_column)


class TestWeekDaysTypeConversion(ModelFormTestCase):

Expand Down
16 changes: 15 additions & 1 deletion wtforms_alchemy/generator.py
Expand Up @@ -2,6 +2,10 @@
from collections import OrderedDict
except ImportError:
from ordereddict import OrderedDict
try:
from enum import Enum
except ImportError:
Enum = None
import inspect
from decimal import Decimal

Expand Down Expand Up @@ -438,7 +442,17 @@ def select_field_kwargs(self, column):
kwargs = {}
kwargs['coerce'] = self.coerce(column)
if isinstance(column.type, types.ChoiceType):
kwargs['choices'] = column.type.choices
choices = column.type.choices
if (
Enum is not None and
isinstance(choices, type)
and issubclass(choices, Enum)
):
kwargs['choices'] = [
(choice.value, str(choice)) for choice in choices
]
else:
kwargs['choices'] = choices
elif 'choices' in column.info and column.info['choices']:
kwargs['choices'] = column.info['choices']
else:
Expand Down
18 changes: 16 additions & 2 deletions wtforms_alchemy/utils.py
Expand Up @@ -10,6 +10,10 @@
from collections import OrderedDict
except ImportError:
from ordereddict import OrderedDict
try:
from enum import Enum
except ImportError:
Enum = None


def choice_type_coerce_factory(type_):
Expand All @@ -19,11 +23,21 @@ def choice_type_coerce_factory(type_):
:param type_: ChoiceType object
"""
choices = type_.choices
if (
Enum is not None and
isinstance(choices, type)
and issubclass(choices, Enum)
):
key, choice_cls = 'value', choices
else:
key, choice_cls = 'code', Choice

def choice_coerce(value):
if value is None:
return None
if isinstance(value, Choice):
return value.code
if isinstance(value, choice_cls):
return getattr(value, key)
return type_.python_type(value)
return choice_coerce

Expand Down

0 comments on commit f7d7045

Please sign in to comment.