274 lines
9.8 KiB
Python
274 lines
9.8 KiB
Python
# w.c.s. - web application for online forms
|
|
# Copyright (C) 2005-2012 Entr'ouvert
|
|
#
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program 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 General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
import collections
|
|
import urllib
|
|
import urlparse
|
|
import xml.etree.ElementTree as ET
|
|
|
|
from quixote import get_publisher
|
|
from quixote.html import TemplateIO
|
|
|
|
from qommon import _
|
|
from qommon.form import *
|
|
from qommon.misc import get_variadic_url
|
|
import qommon.misc
|
|
from qommon import get_logger
|
|
|
|
from qommon.storage import StorableObject
|
|
from qommon.template import Template
|
|
from qommon.xml_storage import XmlStorableObject
|
|
|
|
from wcs.api_utils import sign_url, get_secret_and_orig, MissingSecret
|
|
|
|
|
|
data_source_functions = {}
|
|
|
|
def register_data_source_function(function, function_name=None):
|
|
if not function_name:
|
|
function_name = function.__name__
|
|
if not function_name in data_source_functions:
|
|
data_source_functions[function_name] = function
|
|
|
|
|
|
class DataSourceSelectionWidget(CompositeWidget):
|
|
def __init__(self, name, value=None, allow_jsonp=True,
|
|
allow_named_sources=True, **kwargs):
|
|
CompositeWidget.__init__(self, name, value, **kwargs)
|
|
|
|
if not value:
|
|
value = {}
|
|
|
|
options = [('none', _('None')),
|
|
('formula', _('Python Expression')),
|
|
('json', _('JSON URL'))]
|
|
if allow_jsonp:
|
|
options.append(('jsonp', _('JSONP URL')))
|
|
if allow_named_sources:
|
|
options.extend([(x.slug, x.name) for x in
|
|
NamedDataSource.select(order_by='name')])
|
|
|
|
self.add(SingleSelectWidget, 'type', options=options, value=value.get('type'),
|
|
attrs={'data-dynamic-display-parent': 'true'})
|
|
|
|
self.parse()
|
|
if not self.value:
|
|
self.value = {}
|
|
|
|
self.add(StringWidget, 'value', value=value.get('value'), size=80,
|
|
attrs={'data-dynamic-display-child-of': 'data_source$type',
|
|
'data-dynamic-display-value-in': '|'.join(
|
|
[_('Python Expression'),
|
|
_('JSON URL'),
|
|
_('JSONP URL')])})
|
|
|
|
self._parsed = False
|
|
|
|
def _parse(self, request):
|
|
values = {}
|
|
for name in ('type', 'value'):
|
|
value = self.get(name)
|
|
if value:
|
|
values[name] = value
|
|
if values.get('type', '') == 'none':
|
|
values = None
|
|
self.value = values or None
|
|
|
|
def render_content(self):
|
|
r = TemplateIO(html=True)
|
|
for widget in self.get_widgets():
|
|
r += widget.render_content()
|
|
return r.getvalue()
|
|
|
|
|
|
def get_items(data_source, include_disabled=False):
|
|
structured_items = get_structured_items(data_source)
|
|
tupled_items = []
|
|
for item in structured_items:
|
|
if item.get('disabled') and not include_disabled:
|
|
continue
|
|
tupled_items.append((str(item['id']),
|
|
str(item['text']),
|
|
str(item.get('key', item['id'])),
|
|
item))
|
|
return tupled_items
|
|
|
|
|
|
def get_structured_items(data_source):
|
|
data_source = get_real(data_source)
|
|
if data_source.get('type') == 'formula':
|
|
# the result of a python expression, it must be a list.
|
|
# - of strings
|
|
# - of dictionaries, in which case it has to have both a "id" and a
|
|
# "text" keys
|
|
# - of lists or tuples, in which case it may have up to three elements:
|
|
# - three elements, (id, text, key)
|
|
# - two elements, (id, text)
|
|
# - a single element, (id,)
|
|
variables = get_publisher().substitutions.get_context_variables()
|
|
global_eval_dict = get_publisher().get_global_eval_dict()
|
|
global_eval_dict.update(data_source_functions)
|
|
try:
|
|
value = eval(data_source.get('value'), global_eval_dict, variables)
|
|
if not isinstance(value, collections.Iterable):
|
|
get_logger().warn('Python data source (%r) gave a non-iterable result' % \
|
|
data_source.get('value'))
|
|
return []
|
|
if len(value) == 0:
|
|
return []
|
|
value = qommon.misc.json_encode_helper(value, get_publisher().site_charset)
|
|
if isinstance(value[0], list) or isinstance(value[0], tuple):
|
|
if len(value[0]) >= 3:
|
|
return [{'id': x[0], 'text': x[1], 'key': x[2]} for x in value]
|
|
elif len(value[0]) == 2:
|
|
return [{'id': x[0], 'text': x[1]} for x in value]
|
|
elif len(value[0]) == 1:
|
|
return [{'id': x[0], 'text': x[0]} for x in value]
|
|
return value
|
|
elif isinstance(value[0], basestring):
|
|
return [{'id': x, 'text': x} for x in value]
|
|
return value
|
|
except:
|
|
get_logger().warn('Failed to eval() Python data source (%r)' % data_source.get('value'))
|
|
return []
|
|
elif data_source.get('type') == 'json':
|
|
# the content available at a json URL, it must answer with a dict with
|
|
# a 'data' key holding the list of items, each of them being a dict
|
|
# with at least both an "id" and a "text" key.
|
|
url = data_source.get('value')
|
|
if not url:
|
|
get_logger().warn('Empty URL in JSON data source')
|
|
return []
|
|
url = url.strip()
|
|
if Template.is_template_string(url):
|
|
vars = get_publisher().substitutions.get_context_variables()
|
|
url = get_variadic_url(url, vars)
|
|
try:
|
|
signature_key, orig = get_secret_and_orig(url)
|
|
except MissingSecret:
|
|
pass
|
|
else:
|
|
parsed = urlparse.urlparse(url)
|
|
querystring = urlparse.parse_qsl(parsed.query)
|
|
querystring.append(('orig', orig))
|
|
querystring = urllib.urlencode(querystring)
|
|
url = urlparse.urlunparse(parsed[:4] + (querystring,) + parsed[5:6])
|
|
url = sign_url(url, signature_key)
|
|
try:
|
|
entries = qommon.misc.json_loads(qommon.misc.urlopen(url).read())
|
|
if type(entries) is not dict:
|
|
raise ValueError('not a json dict')
|
|
if type(entries.get('data')) is not list:
|
|
raise ValueError('not a json dict with a data list attribute')
|
|
items = []
|
|
for item in entries.get('data'):
|
|
# skip malformed items
|
|
if item.get('id') is None or item.get('id') == '':
|
|
continue
|
|
if 'text' not in item:
|
|
item['text'] = item['id']
|
|
items.append(item)
|
|
return items
|
|
except qommon.misc.ConnectionError as e:
|
|
get_logger().warn('Error loading JSON data source (%s)' % str(e))
|
|
except ValueError as e:
|
|
get_logger().warn('Error reading JSON data source output (%s)' % str(e))
|
|
return []
|
|
|
|
|
|
def get_real(data_source):
|
|
if not data_source:
|
|
return None
|
|
ds_type = data_source.get('type')
|
|
if ds_type in ('json', 'jsonp', 'formula'):
|
|
return data_source
|
|
return NamedDataSource.get_by_slug(ds_type).data_source
|
|
|
|
|
|
class NamedDataSource(XmlStorableObject):
|
|
_names = 'datasources'
|
|
_xml_tagname = 'datasources'
|
|
|
|
name = None
|
|
slug = None
|
|
description = None
|
|
data_source = None
|
|
|
|
# declarations for serialization
|
|
XML_NODES = [('name', 'str'), ('slug', 'str'), ('description', 'str'),
|
|
('data_source', 'data_source')]
|
|
|
|
def __init__(self, name=None):
|
|
StorableObject.__init__(self)
|
|
self.name = name
|
|
|
|
def migrate(self):
|
|
changed = False
|
|
|
|
if not self.slug:
|
|
# .store() will take care of setting the slug
|
|
changed = True
|
|
if changed:
|
|
self.store()
|
|
|
|
def store(self):
|
|
if self.slug is None:
|
|
# set slug if it's not yet there
|
|
self.slug = self.get_new_slug()
|
|
super(NamedDataSource, self).store()
|
|
|
|
def get_new_slug(self):
|
|
new_slug = qommon.misc.simplify(self.name, space='_')
|
|
base_new_slug = new_slug
|
|
suffix_no = 0
|
|
while True:
|
|
try:
|
|
obj = self.get_on_index(new_slug, 'slug', ignore_migration=True)
|
|
except KeyError:
|
|
break
|
|
if obj.id == self.id:
|
|
break
|
|
suffix_no += 1
|
|
new_slug = '%s-%s' % (base_new_slug, suffix_no)
|
|
return new_slug
|
|
|
|
def export_data_source_to_xml(self, element, attribute_name, charset):
|
|
data_source = getattr(self, attribute_name)
|
|
ET.SubElement(element, 'type').text = data_source.get('type')
|
|
ET.SubElement(element, 'value').text = unicode(data_source.get('value') or '', charset)
|
|
|
|
def import_data_source_from_xml(self, element, charset):
|
|
return {
|
|
'type': str(element.find('type').text),
|
|
'value': (element.find('value').text or '').encode(charset)
|
|
}
|
|
|
|
@classmethod
|
|
def get_by_slug(cls, slug):
|
|
objects = [x for x in cls.select() if x.slug == slug]
|
|
if objects:
|
|
return objects[0]
|
|
raise KeyError()
|
|
|
|
@classmethod
|
|
def get_substitution_variables(cls):
|
|
return {'data_source': DataSourcesSubstitutionProxy()}
|
|
|
|
|
|
class DataSourcesSubstitutionProxy(object):
|
|
def __getattr__(self, attr):
|
|
return get_structured_items(NamedDataSource.get_by_slug(attr).data_source)
|