0001-api-export-structured-items-of-item-and-items-fields.patch
tests/test_api.py | ||
---|---|---|
500 | 500 |
FormDef.wipe() |
501 | 501 |
formdef = FormDef() |
502 | 502 |
formdef.name = 'test' |
503 |
formdef.fields = [fields.StringField(id='0', label='foobar')] |
|
503 |
formdef.fields = [ |
|
504 |
fields.StringField(id='0', label='foobar'), |
|
505 |
fields.ItemField(id='1', label='foobar1', varname='foobar1', |
|
506 |
data_source={ |
|
507 |
'type': 'json', |
|
508 |
'value': 'http://datasource.com', |
|
509 |
}), |
|
510 |
fields.ItemsField(id='2', label='foobar2', varname='foobar2', |
|
511 |
data_source={ |
|
512 |
'type': 'formula', |
|
513 |
'value': '[dict(id=i, text=\'label %s\' % i, foo=i) for i in range(10)]', |
|
514 |
}), |
|
515 |
] |
|
516 | ||
504 | 517 |
formdef.workflow_id = workflow.id |
505 | 518 |
formdef.store() |
506 | 519 | |
507 |
resp = get_app(pub).get('/api/formdefs/test/schema') |
|
508 |
resp2 = get_app(pub).get('/test/schema') |
|
520 |
with mock.patch('qommon.misc.urlopen') as urlopen: |
|
521 |
urlopen.side_effect = lambda *args: StringIO('''\ |
|
522 |
{"data": [{"id": 0, "text": "zéro", "foo": "bar"}, \ |
|
523 |
{"id": 1, "text": "uné", "foo": "bar1"}, \ |
|
524 |
{"id": 2, "text": "deux", "foo": "bar2"}]}''') |
|
525 |
resp = get_app(pub).get('/api/formdefs/test/schema') |
|
526 |
resp2 = get_app(pub).get('/test/schema') |
|
527 |
resp3 = get_app(pub).get(sign_url('/api/formdefs/test/schema?orig=coucou', '1234')) |
|
528 |
resp4 = get_app(pub).get(sign_url('/api/formdefs/test/schema?orig=coucou', '1234')) |
|
509 | 529 | |
510 | 530 |
# check schema |
511 | 531 |
assert resp.json == resp2.json |
... | ... | |
522 | 542 |
assert resp.json['fields'][0]['label'] == 'foobar' |
523 | 543 |
assert resp.json['fields'][0]['type'] == 'string' |
524 | 544 | |
545 |
assert resp.json['fields'][1]['label'] == 'foobar1' |
|
546 |
assert resp.json['fields'][1]['type'] == 'item' |
|
547 | ||
548 |
# check structured items are only exported for authenticated callers |
|
549 |
assert resp.json['fields'][1]['items'] == [] |
|
550 |
assert resp.json['fields'][2]['items'] == [] |
|
551 |
assert 'structured_items' not in resp.json['fields'][1] |
|
552 |
assert 'structured_items' not in resp.json['fields'][2] |
|
553 | ||
554 |
assert len(resp3.json['fields'][1]['structured_items']) == 3 |
|
555 |
assert resp3.json['fields'][1]['structured_items'][0]['id'] == 0 |
|
556 |
assert resp3.json['fields'][1]['structured_items'][0]['text'] == u'zéro' |
|
557 |
assert resp3.json['fields'][1]['structured_items'][0]['foo'] == 'bar' |
|
558 |
assert resp3.json['fields'][1]['items'][0] == u'zéro' |
|
559 | ||
560 |
assert resp3.json['fields'][2]['label'] == 'foobar2' |
|
561 |
assert resp3.json['fields'][2]['type'] == 'items' |
|
562 |
assert len(resp3.json['fields'][2]['structured_items']) == 10 |
|
563 |
assert resp3.json['fields'][2]['structured_items'][0]['id'] == 0 |
|
564 |
assert resp3.json['fields'][2]['structured_items'][0]['text'] == 'label 0' |
|
565 |
assert resp3.json['fields'][2]['structured_items'][0]['foo'] == 0 |
|
566 |
assert resp3.json['fields'][2]['items'][0] == 'label 0' |
|
567 | ||
568 |
# if structured_items fails no values |
|
569 |
assert 'structured_items' not in resp4.json['fields'][1] |
|
570 |
assert resp4.json['fields'][1]['items'] == [] |
|
571 | ||
525 | 572 |
# workflow checks |
526 | 573 |
assert len(resp.json['workflow']['statuses']) == 2 |
527 | 574 |
assert len(resp.json['workflow']['fields']) == 1 |
wcs/api.py | ||
---|---|---|
275 | 275 | |
276 | 276 |
def schema(self): |
277 | 277 |
get_response().set_content_type('application/json') |
278 |
return self.formdef.export_to_json() |
|
278 |
return self.formdef.export_to_json(anonymise=not is_url_signed())
|
|
279 | 279 | |
280 | 280 |
def submit(self): |
281 | 281 |
# expects json as input |
wcs/fields.py | ||
---|---|---|
186 | 186 |
def get_admin_attributes(self): |
187 | 187 |
return ['label', 'type', 'condition'] |
188 | 188 | |
189 |
def export_to_json(self, include_id=False): |
|
189 |
def export_to_json(self, include_id=False, anonymise=True):
|
|
190 | 190 |
field = {} |
191 | 191 |
if include_id: |
192 | 192 |
extra_fields = ['id'] |
... | ... | |
1386 | 1386 |
values.append(display_value) |
1387 | 1387 |
return values |
1388 | 1388 | |
1389 |
def export_to_json(self, include_id=False, anonymise=True): |
|
1390 |
field = super(ItemField, self).export_to_json(include_id=include_id, anonymise=anonymise) |
|
1391 |
if self.data_source and not anonymise: |
|
1392 |
structured_items = data_sources.get_structured_items(self.data_source) |
|
1393 |
if structured_items: |
|
1394 |
field['structured_items'] = structured_items |
|
1395 |
if not field['items']: |
|
1396 |
field['items'] = [item['text'] for item in structured_items] |
|
1397 |
return field |
|
1398 | ||
1399 | ||
1389 | 1400 |
register_field_class(ItemField) |
1390 | 1401 | |
1391 | 1402 | |
... | ... | |
1548 | 1559 |
structured_value.append(structured_option) |
1549 | 1560 |
return structured_value |
1550 | 1561 | |
1562 |
def export_to_json(self, include_id=False, anonymise=True): |
|
1563 |
field = super(ItemsField, self).export_to_json(include_id=include_id, anonymise=True) |
|
1564 |
if self.data_source and not anonymise: |
|
1565 |
structured_items = data_sources.get_structured_items(self.data_source) |
|
1566 |
if structured_items: |
|
1567 |
field['structured_items'] = structured_items |
|
1568 |
if not field['items']: |
|
1569 |
field['items'] = [item['text'] for item in structured_items] |
|
1570 |
return field |
|
1571 | ||
1551 | 1572 |
register_field_class(ItemsField) |
1552 | 1573 | |
1553 | 1574 |
wcs/formdef.py | ||
---|---|---|
657 | 657 |
d.update(self.get_field_data(field, widget)) |
658 | 658 |
return d |
659 | 659 | |
660 |
def export_to_json(self, include_id=False, indent=None): |
|
660 |
def export_to_json(self, include_id=False, indent=None, anonymise=True):
|
|
661 | 661 |
charset = get_publisher().site_charset |
662 | 662 |
root = {} |
663 | 663 |
root['name'] = unicode(self.name, charset) |
... | ... | |
691 | 691 |
root['fields'] = [] |
692 | 692 |
if self.fields: |
693 | 693 |
for field in self.fields: |
694 |
root['fields'].append(field.export_to_json(include_id=include_id)) |
|
694 |
root['fields'].append(field.export_to_json(include_id=include_id, anonymise=anonymise))
|
|
695 | 695 | |
696 | 696 |
if self.geolocations: |
697 | 697 |
root['geolocations'] = self.geolocations.copy() |
698 |
- |