From b132be1f6738a6537b52a6b02e4d3f32d65f451b Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Fri, 23 Jan 2009 18:03:39 +0100 Subject: [PATCH] add jQuery magic for input - display fields depending on whether a DD email address is entered or not - make automatically determined fields readonly - don't hide fields with errors --- ddportfolioservice/config/routing.py | 2 + .../controllers/showformscripts.py | 64 ++++++++++++++- ddportfolioservice/model/form.py | 9 ++- ddportfolioservice/templates/base.mako | 2 +- ddportfolioservice/templates/showform.mako | 34 ++++++-- .../templates/showformscript.mako | 78 +++++++++++++++++++ 6 files changed, 176 insertions(+), 13 deletions(-) create mode 100644 ddportfolioservice/templates/showformscript.mako diff --git a/ddportfolioservice/config/routing.py b/ddportfolioservice/config/routing.py index 6d6dfb6..3d116d4 100644 --- a/ddportfolioservice/config/routing.py +++ b/ddportfolioservice/config/routing.py @@ -41,6 +41,8 @@ def make_map(): # CUSTOM ROUTES HERE map.connect('', controller='ddportfolio', action='index') map.connect('result', controller='ddportfolio', action='urllist') + map.connect('htmlformhelper.js', controller='showformscripts', + action='index') map.connect(':controller/:action/:id') map.connect('*url', controller='template', action='view') diff --git a/ddportfolioservice/controllers/showformscripts.py b/ddportfolioservice/controllers/showformscripts.py index 7664345..72a0aeb 100644 --- a/ddportfolioservice/controllers/showformscripts.py +++ b/ddportfolioservice/controllers/showformscripts.py @@ -1,13 +1,69 @@ +# -*- python -*- +# -*- coding: utf-8 -*- +# +# DDPortfolio service ShowformscriptController. +# Copyright (c) 2009 Jan Dittberner +# +# 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 +# . +# import logging +import simplejson from ddportfolioservice.lib.base import * +from ddportfolioservice.model.form import * +from ddportfolioservice.model import keyfinder +from webob.exc import HTTPBadRequest log = logging.getLogger(__name__) class ShowformscriptsController(BaseController): + """This controller is used to support data entry in showform. + + It provides code for generating JavaScript as well as JSON + responses for autocompletion of fields.""" def index(self): - # Return a rendered template - # return render('/some/template.mako') - # or, Return a response - return 'Hello World' + """This action generates the helper script for the showform + page.""" + response.headers['Content-Type'] = 'text/javascript' + return render('/showformscript.mako') + + def fetchdddata(self): + schema = DDDataRequest() + try: + formencode.api.set_stdtranslation( + domain="FormEncode", + languages=[lang[0:2] for lang in request.languages]) + form_result = schema.to_python(request.params) + except formencode.validators.Invalid, error: + errors = error.unpack_errors() + abort(400, "\n".join( + ["%s: %s" % (key, errors[key]) for key in errors])) + + 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) + response.headers['Content-Type'] = 'text/plain' + return simplejson.dumps(fields) diff --git a/ddportfolioservice/model/form.py b/ddportfolioservice/model/form.py index b8f39ed..b097c45 100644 --- a/ddportfolioservice/model/form.py +++ b/ddportfolioservice/model/form.py @@ -29,9 +29,14 @@ class DeveloperData(formencode.Schema): email = formencode.validators.Email(not_empty=True) name = formencode.validators.String(not_empty=True) gpgfp = formencode.All(formencode.validators.PlainText(), - formencode.validators.MinLength(32), - formencode.validators.MaxLength(32)) + formencode.validators.MinLength(40), + formencode.validators.MaxLength(40)) username = formencode.validators.PlainText() nonddemail = formencode.validators.Email() aliothusername = formencode.validators.PlainText() mode = formencode.validators.OneOf([u'json', u'html'], not_empty=True) + +class DDDataRequest(formencode.Schema): + """Validation schema for DDData request.""" + allow_extra_fields = False + email = formencode.validators.Email(not_empty=True) diff --git a/ddportfolioservice/templates/base.mako b/ddportfolioservice/templates/base.mako index d209ec6..c01b4a0 100644 --- a/ddportfolioservice/templates/base.mako +++ b/ddportfolioservice/templates/base.mako @@ -1,7 +1,7 @@ <%doc> -Template for the data input form. +Base template for XHTML templates. Copyright (c) 2009 Jan Dittberner This file is part of DDPortfolio service. diff --git a/ddportfolioservice/templates/showform.mako b/ddportfolioservice/templates/showform.mako index fa37508..a520ae1 100644 --- a/ddportfolioservice/templates/showform.mako +++ b/ddportfolioservice/templates/showform.mako @@ -22,11 +22,17 @@ License along with this program. If not, see <%def name="titleaddon()"> - ${_('Enter your personal information')} -<%def name="extrahead()">${h.javascript_link('/javascript/jquery/jquery.js')} +<%def name="extrahead()">${h.javascript_link('/javascript/jquery/jquery.js', +h.url_for(controller='showformscripts', +action='index'))} ${h.form(h.url_for(action='urllist'), method='get')}
${_('Debian Developer Portfolio')} -
+

${h.text('email', request.params.get('email', None), id='email')}
-
+
-
+
-
+
-
+