Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 16 additions & 20 deletions flask_appbuilder/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,23 @@
import operator

from wtforms import widgets
from wtforms.compat import string_types, text_type
from wtforms.fields import Field, SelectField, SelectFieldBase
from wtforms.validators import ValidationError


class AJAXSelectField(Field):
"""
Simple class to convert primary key to ORM objects
for SQLAlchemy and fab normal processing on add and update
This WTF field class is prepared to be used in related views or directly on forms.

:param label: The label to render on form
:param validators: A list of form validators
:param: datamodel: An initialized SQLAInterface with a model
:param: col_name: The column that maps to the model
:param: is_related:
If the model column is a relationship or direct on
this case use col_name with the pk
Simple class to convert primary key to ORM objects
for SQLAlchemy and fab normal processing on add and update
This WTF field class is prepared to be used in related views or directly on forms.

:param label: The label to render on form
:param validators: A list of form validators
:param: datamodel: An initialized SQLAInterface with a model
:param: col_name: The column that maps to the model
:param: is_related:
If the model column is a relationship or direct on
this case use col_name with the pk
"""

def __init__(
Expand Down Expand Up @@ -78,7 +77,7 @@ def process_formdata(self, valuelist):

class QuerySelectField(SelectFieldBase):
"""
Based on WTForms QuerySelectField
Based on WTForms QuerySelectField
"""

widget = widgets.Select()
Expand All @@ -100,7 +99,7 @@ def __init__(

if get_label is None:
self.get_label = lambda x: x
elif isinstance(get_label, string_types):
elif isinstance(get_label, str):
self.get_label = operator.attrgetter(get_label)
else:
self.get_label = get_label
Expand All @@ -126,9 +125,7 @@ def _set_data(self, data):
def _get_object_list(self):
if self._object_list is None:
objs = self.query_func()
self._object_list = list(
(text_type(self.get_pk_func(obj)), obj) for obj in objs
)
self._object_list = list((str(self.get_pk_func(obj)), obj) for obj in objs)
return self._object_list

def iter_choices(self):
Expand Down Expand Up @@ -243,8 +240,7 @@ def __init__(
# Column(Enum(enum.Enum)) case
if enum_class is not None:
labels = [
text_type(enum_class.__members__[enum_member].value)
for enum_member in enums
str(enum_class.__members__[enum_member].value) for enum_member in enums
]

def coerce(value):
Expand All @@ -262,7 +258,7 @@ def coerce(value):
def coerce(value):
if value is None:
return None
return text_type(value)
return str(value)

choices = list(zip(enums, labels))

Expand Down