debianmemberportfolio/ddportfolioservice/ddportfolio.py

102 lines
3.6 KiB
Python
Raw Normal View History

2009-01-20 15:33:18 +01:00
# -*- python -*-
# -*- coding: utf8 -*-
from paste.request import parse_formvars
import pkg_resources
import simplejson
2009-01-20 15:33:18 +01:00
from ConfigParser import ConfigParser
from urllib import quote_plus
2009-01-20 20:50:17 +01:00
import logging
import sys
import formencode
from gettext import gettext as _
from formencode import validators
logging.basicConfig(level=logging.DEBUG, stream=sys.stderr)
2009-01-20 15:33:18 +01:00
my_config = ConfigParser()
my_config.readfp(pkg_resources.resource_stream(__name__, 'ddportfolio.ini'))
2009-01-20 20:50:17 +01:00
2009-01-20 15:33:18 +01:00
def build_urls(fields):
2009-01-20 20:50:17 +01:00
"""Build personalized URLs using the developer information in
fields."""
2009-01-20 15:33:18 +01:00
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:
2009-01-20 20:50:17 +01:00
logging.error(_("unable to parse %s: %s") %
(my_config.get(section, url +
'.pattern', True), e))
2009-01-20 15:33:18 +01:00
return result
2009-01-20 20:50:17 +01:00
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):
2009-01-20 20:50:17 +01:00
"""WSGI application entry point."""
2009-01-20 15:33:18 +01:00
if environ['REQUEST_METHOD'] == 'POST':
2009-01-20 20:50:17 +01:00
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()]
2009-01-20 15:33:18 +01:00
data = build_urls(fields)
if ('mode' in fields and fields['mode'] == 'json'):
start_response('200 OK', [('content-type', 'text/json')])
return [simplejson.dumps(data)]
2009-01-20 15:33:18 +01:00
else:
start_response('200 OK', [('content-type', 'text/html')])
return ['''<html>
2009-01-20 20:50:17 +01:00
<head><title>''', _("Debian Developer Portfolio"),'''</title></head>
2009-01-20 15:33:18 +01:00
<body>
<ul>
2009-01-20 20:50:17 +01:00
<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>']
2009-01-20 15:33:18 +01:00
else:
start_response('200 OK', [('content-type', 'text/html')])
return ['''<html>
<head>
2009-01-20 20:50:17 +01:00
<title>''', _('Debian Developer Portfolio'), '''</title>
2009-01-20 15:33:18 +01:00
</head>
<body>
<form method="post">
<fieldset id="ddportfolio">
2009-01-20 20:50:17 +01:00
<legend>''', _('Debian Developer Portfolio'), '''</legend>
<label for="name">''', _('Name:'), '''</label><br />
2009-01-20 15:33:18 +01:00
<input type="text" name="name" /><br />
2009-01-20 20:50:17 +01:00
<label for="email">''', _('E-Mail:'), '''</label><br />
2009-01-20 15:33:18 +01:00
<input type="text" name="email" /><br />
2009-01-20 20:50:17 +01:00
<label for="username">''', _('User name:'), '''</label><br />
2009-01-20 15:33:18 +01:00
<input type="text" name="username" /><br />
2009-01-20 20:50:17 +01:00
<input type="submit" value="''', _('Build DD Portfolio URLs'), '''" />
2009-01-20 15:33:18 +01:00
</fieldset>
</form>
</body>
</html>''']
if __name__ == '__main__':
from paste import httpserver
httpserver.serve(application, host='127.0.0.1', port='8080')