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

Changed the simple_form class to properly convert variables to their htm... #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 14 additions & 7 deletions pyramid_simpleform/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,13 @@ def __init__(self, request, schema=None, validators=None, defaults=None,
self.data.update(defaults)

if obj:
fields = self.schema.fields.keys() + self.validators.keys()
for f in fields:
if hasattr(obj, f):
self.data[f] = getattr(obj, f)
fields = itertools.chain(self.schema.fields.iteritems(), self.validators.iteritems())
for name, validator in fields:
if hasattr(obj, name):
try:
self.data[name] = validator.from_python(getattr(obj, name) )
except Invalid as e:
self.data[name] = getattr(obj, name)

def is_error(self, field):
"""
Expand Down Expand Up @@ -192,7 +195,8 @@ def validate(self, force_validate=False, params=None):

if self.schema:
try:
self.data = self.schema.to_python(decoded, self.state)
self.data = self.schema.from_python(self.schema.to_python(decoded, self.state), self.state)

except Invalid, e:
self.errors = e.unpack_errors(self.variable_decode,
self.dict_char,
Expand All @@ -201,8 +205,8 @@ def validate(self, force_validate=False, params=None):
if self.validators:
for field, validator in self.validators.iteritems():
try:
self.data[field] = validator.to_python(decoded.get(field),
self.state)
self.data[field] = validator.from_python(validator.to_python(decoded.get(field),
self.state), self.state)

except Invalid, e:
self.errors[field] = unicode(e)
Expand Down Expand Up @@ -239,13 +243,16 @@ def bind(self, obj, include=None, exclude=None):
raise RuntimeError, "Cannot bind to object if form has errors"

items = [(k, v) for k, v in self.data.items() if not k.startswith("_")]
fields = dict(itertools.chain(self.schema.fields.iteritems(), self.validators.iteritems()))
for k, v in items:

if include and k not in include:
continue

if exclude and k in exclude:
continue

v = fields[k].to_python(v)

setattr(obj, k, v)

Expand Down