0001-add-real-options-list-to-ItemField-JSON-export-fixes.patch
tests/test_fields.py | ||
---|---|---|
278 | 278 |
form = Form() |
279 | 279 |
field.add_to_form(form) |
280 | 280 |
assert str(form.render()).count('"radio"') == 1 |
281 | ||
282 |
def test_item_export_options(): |
|
283 |
items_kwargs = [] |
|
284 |
items_kwargs.append({'items': ['a', 'b', 'c']}) |
|
285 |
items_kwargs.append({'data_source': { |
|
286 |
'type': 'formula', |
|
287 |
'value': '''['a', 'b', 'c']'''}}) |
|
288 | ||
289 |
for item_kwargs in items_kwargs: |
|
290 |
field = fields.ItemField(id='1', label='Foobar', show_as_radio=True, **item_kwargs) |
|
291 |
export = field.export_to_json() |
|
292 |
assert 'options' in export |
|
293 |
assert export['options'] == [{'value': 'a', 'label': 'a'}, {'value': 'b', 'label': 'b'}, |
|
294 |
{'value': 'c', 'label': 'c'}] |
wcs/fields.py | ||
---|---|---|
1200 | 1200 |
values.append(display_value) |
1201 | 1201 |
return values |
1202 | 1202 | |
1203 |
def export_to_json(self, include_id=False): |
|
1204 |
field = super(ItemField, self).export_to_json(include_id=include_id) |
|
1205 |
options = [] |
|
1206 |
for option in self.get_options(): |
|
1207 |
value = option[0] |
|
1208 |
try: |
|
1209 |
description = option[1] |
|
1210 |
except IndexError: |
|
1211 |
description = value |
|
1212 |
option = { |
|
1213 |
'value': value, |
|
1214 |
'label': description, |
|
1215 |
} |
|
1216 |
options.append(option) |
|
1217 |
if options: |
|
1218 |
field['options'] = options |
|
1219 |
return field |
|
1220 | ||
1221 | ||
1203 | 1222 |
register_field_class(ItemField) |
1204 | 1223 | |
1205 | 1224 | |
1206 |
- |