Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No number range field #58

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
10 changes: 7 additions & 3 deletions wtforms_alchemy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
from wtforms_components import (
DateRange,
Email,
NumberRangeField,
PhoneNumberField,
SelectField,
SelectMultipleField,
Expand All @@ -30,7 +29,7 @@
UnknownTypeException,
UnknownConfigurationOption
)
from .fields import ModelFieldList, ModelFormField
from .fields import ModelFieldList, ModelFormField, OptionalModelFormField
from .generator import FormGenerator


Expand All @@ -40,7 +39,7 @@
InvalidAttributeException,
ModelFieldList,
ModelFormField,
NumberRangeField,
OptionalModelFormField,
PhoneNumberField,
SelectField,
SelectMultipleField,
Expand Down Expand Up @@ -207,6 +206,11 @@ class Meta:
#: List of fields to only include in the generated form.
only = defaults.pop('only', [])

#: Silently ignore exclude elements which aren't mapped
#:
#: By Default silently ignores missing elements
silent_exclude = defaults.pop('silent_exclude', True)

def __init__(self, *args, **kwargs):
"""Sets object as form attribute."""

Expand Down
27 changes: 23 additions & 4 deletions wtforms_alchemy/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class SkipOperation(Exception):
pass



def get_pk_from_identity(obj):
cls, key = identity_key(instance=obj)
return ':'.join(map(six.text_type, key))
Expand All @@ -25,8 +24,8 @@ def get_pk_from_identity(obj):
def labelize(func):
if func is None:
return lambda x: x
elif isinstance(func, string_types):
return operator.attrgetter(func)
elif isinstance(func, six.string_types):
return six.operator.attrgetter(func)
else:
return func

Expand Down Expand Up @@ -65,7 +64,6 @@ def __iter__(self):
yield pk, obj



class ModelFormField(FormField):
def populate_obj(self, obj, name):
if self.data:
Expand All @@ -77,6 +75,27 @@ def populate_obj(self, obj, name):
FormField.populate_obj(self, obj, name)


class OptionalModelFormField(ModelFormField):
def __init__(self, *args, **kwargs):
super(OptionalModelFormField, self).__init__(*args, **kwargs)

# just here to set is_missing form field val to False
self.is_missing = True

def process(self, formdata, **kwargs):
super(OptionalModelFormField, self).process(formdata, **kwargs)

# check if the prefix is found anywhere
prefix = self.name + self.separator
self.is_missing = not any(
key.startswith(prefix) for key in formdata.keys())

def populate_obj(self, obj, name):
# only create a new sub object if the form field wasn't missing
if not self.is_missing:
super(OptionalModelFormField, self).populate_obj(obj, name)


class ModelFieldList(FieldList):
def __init__(
self,
Expand Down
7 changes: 4 additions & 3 deletions wtforms_alchemy/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
DecimalField,
EmailField,
IntegerField,
NumberRangeField,
PhoneNumberField,
SelectField,
StringField,
Expand Down Expand Up @@ -88,7 +87,6 @@ class FormGenerator(object):
(types.ChoiceType, SelectField),
(types.ColorType, ColorField),
(types.EmailType, EmailField),
(types.NumberRangeType, NumberRangeField),
(types.PasswordType, PasswordField),
(types.PhoneNumberType, PhoneNumberField),
(types.ScalarListType, StringField),
Expand Down Expand Up @@ -163,7 +161,10 @@ def filter_attributes(self, attrs):

if self.meta.exclude:
for key in self.meta.exclude:
del attrs[key]
if self.meta.silent_exclude and not key in attrs:
continue
else:
del attrs[key]
return attrs

def validate_attribute(self, attr_name):
Expand Down