add input validation, prepare i18n

This commit is contained in:
Jan Dittberner 2009-01-20 20:50:17 +01:00
parent e9493b6fb0
commit 0c5816154b
2 changed files with 47 additions and 13 deletions

View file

@ -6,11 +6,21 @@ import pkg_resources
import simplejson
from ConfigParser import ConfigParser
from urllib import quote_plus
import logging
import sys
import formencode
from gettext import gettext as _
from formencode import validators
logging.basicConfig(level=logging.DEBUG, stream=sys.stderr)
my_config = ConfigParser()
my_config.readfp(pkg_resources.resource_stream(__name__, 'ddportfolio.ini'))
def build_urls(fields):
"""Build personalized URLs using the developer information in
fields."""
result = {}
qfields = dict([(key, quote_plus(fields[key])) for key in fields])
for section in my_config.sections():
@ -22,12 +32,33 @@ def build_urls(fields):
my_config.get(section, url + '.pattern',
False, qfields)
except Exception, e:
print "unable to parse %s: %s" % (my_config.get(section, url + '.pattern', True), e)
logging.error(_("unable to parse %s: %s") %
(my_config.get(section, url +
'.pattern', True), e))
return result
class DeveloperData(formencode.Schema):
name = validators.String(not_empty=True)
email = validators.Email(not_empty=True)
username = validators.PlainText(not_empty=True)
def validate_input(fields):
logging.debug(fields)
return DeveloperData().to_python(fields)
def gather_additional_info(fields):
logging.debug(fields)
return fields
def application(environ, start_response):
fields = parse_formvars(environ)
"""WSGI application entry point."""
if environ['REQUEST_METHOD'] == 'POST':
try:
fields = gather_additional_info(
validate_input(parse_formvars(environ)))
except formencode.Invalid, e:
start_response('400 Bad Request', [('content-type', 'text/plain')])
return ["input validation failed\n", e.unpack_errors()]
data = build_urls(fields)
if ('mode' in fields and fields['mode'] == 'json'):
start_response('200 OK', [('content-type', 'text/json')])
@ -35,29 +66,31 @@ def application(environ, start_response):
else:
start_response('200 OK', [('content-type', 'text/html')])
return ['''<html>
<head><title>Debian Developer Portfolio</title></head>
<head><title>''', _("Debian Developer Portfolio"),'''</title></head>
<body>
<ul>
<li>''', '</li><li>'.join(['%(key)s: <a href="%(url)s">%(url)s</a>' % {'key': key, 'url': data[key]} for key in data]), '</li></ul></body></html>']
<li>''',
'</li><li>'.join([
'%(key)s: <a href="%(url)s">%(url)s</a>' %
{'key': key, 'url': data[key]} for key in data]),
'</li></ul></body></html>']
else:
start_response('200 OK', [('content-type', 'text/html')])
return ['''<html>
<head>
<title>Debian Developer Portfolio</title>
<title>''', _('Debian Developer Portfolio'), '''</title>
</head>
<body>
<form method="post">
<fieldset id="ddportfolio">
<legend>Debian Developer Portfolio</legend>
<label for="name">Name:</label><br />
<legend>''', _('Debian Developer Portfolio'), '''</legend>
<label for="name">''', _('Name:'), '''</label><br />
<input type="text" name="name" /><br />
<label for="email">E-Mail:</label><br />
<label for="email">''', _('E-Mail:'), '''</label><br />
<input type="text" name="email" /><br />
<label for="gpgfp">GPG-Fingerprint:</label><br />
<input type="text" name="gpgfp" /><br />
<label for="username">User name:</label><br />
<label for="username">''', _('User name:'), '''</label><br />
<input type="text" name="username" /><br />
<input type="submit" value="Build DD Portfolio URLs" />
<input type="submit" value="''', _('Build DD Portfolio URLs'), '''" />
</fieldset>
</form>
</body>

View file

@ -33,6 +33,7 @@ array of URLs.""",
# -*- Extra requirements: -*-
'Paste',
'PasteDeploy',
'simplejson'
'simplejson',
'formencode'
],
)