Adding a helper method to pecan's default template namespace, static. Now in templates, you can wrap form field values with static(name, value), e.g.,

<input type="password" name="password" value="${static('password', '')}" />
<input type="hidden" name="token" value="${static('token', 'RANDOMTOKEN')}" />

...and htmlfill will skip over them when pre-filling.  This is useful if you'd like to always enforce a default value for an HTML field.
This commit is contained in:
Ryan Petrello
2011-01-26 14:35:14 -05:00
parent 8922dc4d7d
commit 23794ab887
5 changed files with 80 additions and 1 deletions

View File

@@ -63,6 +63,13 @@ def error_for(field):
return ''
return request.validation_errors.get(field, '')
def static(name, value):
if 'pecan.params' not in request.environ:
request.environ['pecan.params'] = dict(request.str_params)
request.environ['pecan.params'][name] = value
return value
class ValidationException(ForwardRequestException):
def __init__(self, location=None, errors={}):
@@ -75,7 +82,8 @@ class ValidationException(ForwardRequestException):
if callable(location):
location = location()
merge_dicts(request.validation_errors, errors)
request.environ['pecan.params'] = dict(request.str_params)
if 'pecan.params' not in request.environ:
request.environ['pecan.params'] = dict(request.str_params)
request.environ['pecan.validation_errors'] = request.validation_errors
if cfg.get('htmlfill') is not None:
request.environ['pecan.htmlfill'] = cfg['htmlfill']
@@ -292,6 +300,7 @@ class Pecan(object):
state.content_type = self.get_content_type('json')
else:
result['error_for'] = error_for
result['static'] = static
if ':' in template:
renderer = self.renderers.get(template.split(':')[0], self.template_path)

View File

@@ -0,0 +1,2 @@
<input type="text" id="username" name="username" />
<input type="password" id="password" name="password" value="${static('password', '')}" />

View File

@@ -0,0 +1,2 @@
<input type="text" id="username" name="username" value="ryan" />
<input type="password" id="password" name="password" value="" class="error" />

View File

@@ -0,0 +1,2 @@
<input type="text" id="username" name="username" />
<input type="password" id="password" name="password" value="" />

View File

@@ -624,6 +624,70 @@ class TestValidation(object):
})
assert r.status_int == 200
assert r.body == dumps(dict(error_with=['name']))
def test_htmlfill_static(self):
if 'mako' not in builtin_renderers:
return
class LoginSchema(Schema):
username = validators.String(not_empty=True)
password = validators.String(not_empty=True)
class RootController(object):
@expose(template='mako:form_login.html',
schema=LoginSchema())
def index(self, **kwargs):
if request.validation_errors:
return dict()
else:
return dict(data=kwargs)
@expose(schema=LoginSchema(),
error_handler='/errors_with_handler')
def with_handler(self, **kwargs):
return '%s:%s' % (kwargs['username'], kwargs['password'])
@expose('mako:form_login.html')
def errors_with_handler(self):
return dict()
def _get_contents(filename):
return open(os.path.join(self.template_path, filename), 'r').read()
# test without error handler
app = TestApp(make_app(RootController(), template_path=self.template_path))
r = app.post('/', {
'username' : 'ryan',
'password' : 'password'
})
assert r.status_int == 200
assert r.body == _get_contents('form_login_valid.html')
# test failure without error handler
r = app.post('/', {
'username' : 'ryan',
'password' : ''
})
assert r.status_int == 200
assert r.body == _get_contents('form_login_invalid.html')
# test with error handler
r = app.post('/with_handler', {
'username' : 'ryan',
'password' : 'password'
})
assert r.status_int == 200
assert r.body == 'ryan:password'
# test failure with error handler
r = app.post('/with_handler', {
'username' : 'ryan',
'password' : ''
})
assert r.status_int == 200
assert r.body == _get_contents('form_login_invalid.html')
def test_error_for(self):