pre-fill fields if enough necessary information is given

- the email address is enough information for DDs
- default to JSON mode
- use text/javascript content type for JSON response in ddportfolio
  controller
This commit is contained in:
Jan Dittberner 2009-02-17 23:30:03 +01:00
parent 2b0b098d1d
commit 6d9b5b85c8
5 changed files with 70 additions and 19 deletions

View file

@ -26,6 +26,7 @@ import simplejson
from ddportfolioservice.lib.base import * from ddportfolioservice.lib.base import *
from ddportfolioservice.model.form import * from ddportfolioservice.model.form import *
from ddportfolioservice.model.urlbuilder import * from ddportfolioservice.model.urlbuilder import *
from ddportfolioservice.model import dddatabuilder
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -115,7 +116,7 @@ developer name on all bug logs)'),
def urllist(self): def urllist(self):
"""Handle the actual data.""" """Handle the actual data."""
schema = DeveloperData() schema = DDDataRequest()
try: try:
formencode.api.set_stdtranslation( formencode.api.set_stdtranslation(
domain="FormEncode", domain="FormEncode",
@ -124,9 +125,31 @@ developer name on all bug logs)'),
except formencode.validators.Invalid, error: except formencode.validators.Invalid, error:
c.messages = { 'errors' : error.unpack_errors() } c.messages = { 'errors' : error.unpack_errors() }
return render('/showform.mako') return render('/showform.mako')
data = build_urls(form_result) fields = dddatabuilder.build_data(form_result['email'])
if 'mode' in request.params and request.params['mode'] == 'json': rp = request.params.copy()
response.headers['Content-Type'] = 'text/json' if fields['isdd']:
for tuple in (('name', 'name'),
('gpgfp', 'gpgfp'),
('username', 'username'),
('aliothusername', 'username'),
('nonddemail', 'email')):
if not tuple[0] in rp or not rp[tuple[0]]:
rp[tuple[0]] = fields[tuple[1]]
if not 'mode' in rp or not rp['mode']:
rp['mode'] = 'json'
schema = DeveloperData()
try:
formencode.api.set_stdtranslation(
domain="FormEncode",
languages=[lang[0:2] for lang in request.languages])
form_result = schema.to_python(rp)
except formencode.validators.Invalid, error:
c.messages = { 'errors' : error.unpack_errors() }
return render('/showform.mako')
data = build_urls(rp)
if 'mode' in rp and rp['mode'] == 'json':
response.headers['Content-Type'] = 'text/javascript'
return simplejson.dumps( return simplejson.dumps(
dict([("%s.%s" % \ dict([("%s.%s" % \
(entry[1], entry[2]), entry[3]) \ (entry[1], entry[2]), entry[3]) \

View file

@ -25,8 +25,7 @@ import simplejson
from ddportfolioservice.lib.base import * from ddportfolioservice.lib.base import *
from ddportfolioservice.model.form import * from ddportfolioservice.model.form import *
from ddportfolioservice.model import keyfinder from ddportfolioservice.model import dddatabuilder
from webob.exc import HTTPBadRequest
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -53,17 +52,7 @@ class ShowformscriptsController(BaseController):
errors = error.unpack_errors() errors = error.unpack_errors()
abort(400, "\n".join( abort(400, "\n".join(
["%s: %s" % (key, errors[key]) for key in errors])) ["%s: %s" % (key, errors[key]) for key in errors]))
fields = dddatabuilder.build_data(form_result['email'])
fields = dict([(field, func(str(form_result['email']))) \
for (field, func) in \
[('gpgfp', keyfinder.getFingerprintByEmail),
('name', keyfinder.getRealnameByEmail),
('username', keyfinder.getLoginByEmail)]])
fields['email'] = form_result['email']
if fields['username'] and fields['gpgfp'] and fields['name']:
fields['isdd'] = 1
else:
fields['isdd'] = 0
log.debug(fields) log.debug(fields)
response.headers['Content-Type'] = 'text/plain' response.headers['Content-Type'] = 'text/plain'
return simplejson.dumps(fields) return simplejson.dumps(fields)

View file

@ -0,0 +1,38 @@
# -*- python -*-
# -*- coding: utf-8 -*-
#
# DDPortfolio service DD data builder
# Copyright (c) 2009 Jan Dittberner <jan@dittberner.info>
#
# This file is part of DDPortfolio service.
#
# DDPortfolio service is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# DDPortfolio service is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public
# License along with this program. If not, see
# <http://www.gnu.org/licenses/>.
#
from ddportfolioservice.model import keyfinder
def build_data(email_address):
"""Build a DD data structure from a given email address."""
fields = dict([(field, func(str(email_address))) \
for (field, func) in \
[('gpgfp', keyfinder.getFingerprintByEmail),
('name', keyfinder.getRealnameByEmail),
('username', keyfinder.getLoginByEmail)]])
fields['email'] = email_address
if fields['username'] and fields['gpgfp'] and fields['name']:
fields['isdd'] = 1
else:
fields['isdd'] = 0
return fields

View file

@ -38,5 +38,6 @@ class DeveloperData(formencode.Schema):
class DDDataRequest(formencode.Schema): class DDDataRequest(formencode.Schema):
"""Validation schema for DDData request.""" """Validation schema for DDData request."""
allow_extra_fields = False allow_extra_fields = True
filter_extra_fields = False
email = formencode.validators.Email(not_empty=True) email = formencode.validators.Email(not_empty=True)

View file

@ -42,7 +42,7 @@ def _get_keyring_cache():
def _get_cached(cachekey): def _get_cached(cachekey):
cache = _get_keyring_cache() cache = _get_keyring_cache()
if cachekey in cache: if cachekey in cache:
return cache[cachekey].split(':') return cache[cachekey]
return None return None
def getFingerprintByEmail(email): def getFingerprintByEmail(email):