2009-01-23 18:03:39 +01:00
|
|
|
# -*- python -*-
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2014-02-08 23:54:51 +01:00
|
|
|
# Debian Member Portfolio Service ShowformscriptsController.
|
2009-01-23 18:03:39 +01:00
|
|
|
#
|
2014-02-08 23:54:51 +01:00
|
|
|
# Copyright © 2009-2014 Jan Dittberner <jan@dittberner.info>
|
2009-01-23 18:03:39 +01:00
|
|
|
#
|
2014-02-08 23:54:51 +01:00
|
|
|
# This file is part of the Debian Member Portfolio Service.
|
2009-01-23 18:03:39 +01:00
|
|
|
#
|
2014-02-08 23:54:51 +01:00
|
|
|
# Debian Member Portfolio 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.
|
2009-01-23 18:03:39 +01:00
|
|
|
#
|
2014-02-08 23:54:51 +01:00
|
|
|
# Debian Member Portfolio 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.
|
2009-01-23 18:03:39 +01:00
|
|
|
#
|
2014-02-08 23:54:51 +01:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2014-10-14 13:38:29 +02:00
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2014-02-08 23:54:51 +01:00
|
|
|
#
|
|
|
|
"""
|
|
|
|
This file defines the ShowformscriptsController used to generate the JavaScript
|
|
|
|
code in forms.
|
|
|
|
"""
|
|
|
|
|
2009-01-23 14:29:37 +01:00
|
|
|
import logging
|
2009-01-23 18:03:39 +01:00
|
|
|
import simplejson
|
2009-01-23 14:29:37 +01:00
|
|
|
|
2010-04-18 15:03:02 +02:00
|
|
|
from pylons import request, response
|
2010-04-18 20:53:13 +02:00
|
|
|
from pylons.controllers.util import abort
|
2014-02-08 23:54:51 +01:00
|
|
|
|
2010-04-18 15:03:02 +02:00
|
|
|
import formencode.api
|
|
|
|
import formencode.validators
|
|
|
|
|
2014-02-08 23:54:51 +01:00
|
|
|
from debianmemberportfolio.lib.base import BaseController, render
|
|
|
|
from debianmemberportfolio.model.form import DDDataRequest
|
|
|
|
from debianmemberportfolio.model import dddatabuilder
|
2009-01-23 14:29:37 +01:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2012-01-07 01:46:57 +01:00
|
|
|
|
2009-01-23 14:29:37 +01:00
|
|
|
class ShowformscriptsController(BaseController):
|
2009-01-23 18:03:39 +01:00
|
|
|
"""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."""
|
2009-01-23 14:29:37 +01:00
|
|
|
|
|
|
|
def index(self):
|
2012-01-07 01:46:57 +01:00
|
|
|
"""
|
|
|
|
This action generates the helper script for the showform page.
|
|
|
|
"""
|
2010-04-18 15:03:02 +02:00
|
|
|
response.headers['Content-Type'] = 'text/javascript; charset=utf-8'
|
2009-01-23 18:03:39 +01:00
|
|
|
return render('/showformscript.mako')
|
|
|
|
|
|
|
|
def fetchdddata(self):
|
2012-01-07 01:46:57 +01:00
|
|
|
"""
|
|
|
|
This action fetches the data for a given mail address and
|
|
|
|
returns them as JSON.
|
|
|
|
"""
|
2009-01-23 18:03:39 +01:00
|
|
|
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(
|
2014-02-08 23:54:51 +01:00
|
|
|
["%s: %s" % (key, errors[key]) for key in errors]))
|
2009-02-17 23:30:03 +01:00
|
|
|
fields = dddatabuilder.build_data(form_result['email'])
|
2009-01-23 18:03:39 +01:00
|
|
|
log.debug(fields)
|
|
|
|
response.headers['Content-Type'] = 'text/plain'
|
|
|
|
return simplejson.dumps(fields)
|