debianmemberportfolio/ddportfolioservice/controllers/showformscripts.py

70 lines
2.6 KiB
Python
Raw Normal View History

# -*- python -*-
# -*- coding: utf-8 -*-
#
# DDPortfolio service ShowformscriptController.
# 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/>.
#
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):
"""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)