diff --git a/ddportfolioservice/ddportfolio.py b/ddportfolioservice/ddportfolio.py index a58251a..361cdc0 100644 --- a/ddportfolioservice/ddportfolio.py +++ b/ddportfolioservice/ddportfolio.py @@ -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 [''' -Debian Developer Portfolio +''', _("Debian Developer Portfolio"),''' '] +
  • ''', + '
  • '.join([ + '%(key)s: %(url)s' % + {'key': key, 'url': data[key]} for key in data]), + '
  • '] else: start_response('200 OK', [('content-type', 'text/html')]) return [''' - Debian Developer Portfolio + ''', _('Debian Developer Portfolio'), '''
    - Debian Developer Portfolio -
    + ''', _('Debian Developer Portfolio'), ''' +

    -
    +

    -
    -
    -
    +

    - +
    diff --git a/setup.py b/setup.py index a8f5f46..d52526d 100644 --- a/setup.py +++ b/setup.py @@ -33,6 +33,7 @@ array of URLs.""", # -*- Extra requirements: -*- 'Paste', 'PasteDeploy', - 'simplejson' + 'simplejson', + 'formencode' ], )