101 lines
3.6 KiB
Python
101 lines
3.6 KiB
Python
# -*- python -*-
|
|
# -*- coding: utf8 -*-
|
|
|
|
from paste.request import parse_formvars
|
|
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():
|
|
if my_config.has_option(section, 'urls'):
|
|
for url in my_config.get(section, 'urls').split(','):
|
|
if my_config.has_option(section, url + '.pattern'):
|
|
try:
|
|
result[section + '.' + url] = \
|
|
my_config.get(section, url + '.pattern',
|
|
False, qfields)
|
|
except Exception, 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):
|
|
"""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')])
|
|
return [simplejson.dumps(data)]
|
|
else:
|
|
start_response('200 OK', [('content-type', 'text/html')])
|
|
return ['''<html>
|
|
<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>']
|
|
else:
|
|
start_response('200 OK', [('content-type', 'text/html')])
|
|
return ['''<html>
|
|
<head>
|
|
<title>''', _('Debian Developer Portfolio'), '''</title>
|
|
</head>
|
|
<body>
|
|
<form method="post">
|
|
<fieldset id="ddportfolio">
|
|
<legend>''', _('Debian Developer Portfolio'), '''</legend>
|
|
<label for="name">''', _('Name:'), '''</label><br />
|
|
<input type="text" name="name" /><br />
|
|
<label for="email">''', _('E-Mail:'), '''</label><br />
|
|
<input type="text" name="email" /><br />
|
|
<label for="username">''', _('User name:'), '''</label><br />
|
|
<input type="text" name="username" /><br />
|
|
<input type="submit" value="''', _('Build DD Portfolio URLs'), '''" />
|
|
</fieldset>
|
|
</form>
|
|
</body>
|
|
</html>''']
|
|
|
|
if __name__ == '__main__':
|
|
from paste import httpserver
|
|
httpserver.serve(application, host='127.0.0.1', port='8080')
|