debianmemberportfolio/ddportfolioservice/ddportfolio.py

69 lines
2.5 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
my_config = ConfigParser()
my_config.readfp(pkg_resources.resource_stream(__name__, 'ddportfolio.ini'))
def build_urls(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:
print "unable to parse %s: %s" % (my_config.get(section, url + '.pattern', True), e)
return result
def application(environ, start_response):
fields = parse_formvars(environ)
if environ['REQUEST_METHOD'] == 'POST':
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="gpgfp">GPG-Fingerprint:</label><br />
<input type="text" name="gpgfp" /><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')