0001-encode-data-source-value-using-qommon.misc.json_enco.patch
tests/test_datasource.py | ||
---|---|---|
11 | 11 |
from wcs import fields, data_sources |
12 | 12 |
from wcs.data_sources import NamedDataSource, register_data_source_function |
13 | 13 | |
14 |
import mock |
|
14 | 15 |
from test_widgets import MockHtmlForm, mock_form_submission |
15 | 16 |
from utilities import create_temporary_pub |
16 | 17 | |
... | ... | |
215 | 216 |
assert widget is not None |
216 | 217 |
assert widget.options == [('1', 'un', '1'), ('2', 'deux', '2')] |
217 | 218 | |
219 | ||
218 | 220 |
def test_data_source_unicode(): |
219 | 221 |
NamedDataSource.wipe() |
220 | 222 |
data_source = NamedDataSource(name='foobar') |
... | ... | |
223 | 225 | |
224 | 226 |
data_source2 = NamedDataSource.select()[0] |
225 | 227 |
assert data_source2.data_source == data_source.data_source |
228 |
assert data_sources.get_items({'type': 'foobar'}) == [ |
|
229 |
('uné',) * 3, |
|
230 |
('deux',) * 3, |
|
231 |
] |
|
232 | ||
233 |
NamedDataSource.wipe() |
|
234 |
data_source = NamedDataSource(name='foobar') |
|
235 |
data_source.data_source = {'type': 'json', 'value': "https://whatever.com/json"} |
|
236 |
data_source.store() |
|
237 | ||
238 |
data_source2 = NamedDataSource.select()[0] |
|
239 |
assert data_source2.data_source == data_source.data_source |
|
240 |
with mock.patch('wcs.data_sources.urllib2') as urllib2: |
|
241 |
urllib2.urlopen.return_value.read.return_value = \ |
|
242 |
'{"data": [{"id": 1, "text": "uné"}, {"id": 2, "text": "deux"}]}' |
|
243 |
assert data_sources.get_items({'type': 'foobar'}) == [ |
|
244 |
('1', 'uné', '1'), |
|
245 |
('2', 'deux', '2'), |
|
246 |
] |
wcs/data_sources.py | ||
---|---|---|
15 | 15 |
# along with this program; if not, see <http://www.gnu.org/licenses/>. |
16 | 16 | |
17 | 17 |
import collections |
18 |
import json |
|
19 | 18 |
import urllib2 |
20 | 19 |
import xml.etree.ElementTree as ET |
21 | 20 | |
... | ... | |
84 | 83 |
r += widget.render_content() |
85 | 84 |
return r.getvalue() |
86 | 85 | |
86 | ||
87 | 87 |
def get_items(data_source): |
88 | 88 |
structured_items = get_structured_items(data_source) |
89 |
tupled_items = [(site_encode(x.get('id')), |
|
90 |
site_encode(x.get('text')), |
|
91 |
site_encode(x.get('key'))) for x in structured_items] |
|
92 |
if tupled_items and tupled_items[0][2] is None: # no key |
|
93 |
tupled_items = [tuple(x[:2]) + (x[0],) for x in tupled_items] |
|
89 |
tupled_items = [] |
|
90 |
for item in structured_items: |
|
91 |
# skip malformed items |
|
92 |
if not item.get('id') or not item.get('text'): |
|
93 |
continue |
|
94 |
tupled_items.append((str(item['id']), str(item['text']), str(item.get('key', item['id'])))) |
|
94 | 95 |
return tupled_items |
95 | 96 | |
97 | ||
96 | 98 |
def get_structured_items(data_source): |
97 | 99 |
data_source = get_real(data_source) |
98 | 100 |
if data_source.get('type') == 'formula': |
... | ... | |
113 | 115 |
return [] |
114 | 116 |
if len(value) == 0: |
115 | 117 |
return [] |
118 |
value = qommon.misc.json_encode_helper(value, get_publisher().site_charset) |
|
116 | 119 |
if isinstance(value[0], list) or isinstance(value[0], tuple): |
117 | 120 |
if len(value[0]) >= 3: |
118 | 121 |
return [{'id': x[0], 'text': x[1], 'key': x[2]} for x in value] |
... | ... | |
138 | 141 |
if '[' in url: |
139 | 142 |
vars = get_publisher().substitutions.get_context_variables() |
140 | 143 |
url = get_variadic_url(url, vars) |
141 |
charset = get_publisher().site_charset |
|
142 | 144 |
try: |
143 |
results = [] |
|
144 |
entries = json.load(urllib2.urlopen(url)) |
|
145 |
entries = qommon.misc.json_loads(urllib2.urlopen(url).read()) |
|
145 | 146 |
if type(entries) is not dict: |
146 | 147 |
raise ValueError('not a json dict') |
147 | 148 |
if type(entries.get('data')) is not list: |
148 |
- |