datasource: fix get_items on wrong formula (#56980)
gitea-wip/wcs/pipeline/head There was a failure building this commit Details

This commit is contained in:
Lauréline Guérin 2021-09-20 08:18:11 +02:00
parent b4ee372fd1
commit 4d9e132798
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
2 changed files with 25 additions and 2 deletions

View File

@ -160,6 +160,20 @@ def test_python_datasource_errors(pub, error_email, http_requests, emails, caplo
assert logged_error.workflow_id is None
assert logged_error.summary == "[DATASOURCE] Python data source ('2') gave a non-iterable result"
datasource = {
'type': 'formula',
'value': '[{"mairie-a-rdv", "Mairie A"}, {"mairie-b-rdv", "Mairie B"}]',
'record_on_errors': True,
}
assert data_sources.get_items(datasource) == []
if pub.is_using_postgresql():
assert pub.loggederror_class.count() == 3
logged_error = pub.loggederror_class.select()[2]
assert logged_error.workflow_id is None
assert logged_error.summary == (
'[DATASOURCE] Python data source (\'[{"mairie-a-rdv", "Mairie A"}, {"mairie-b-rdv", "Mairie B"}]\') gave a non usable result'
)
def test_python_datasource_with_evalutils(pub):
plain_list = [

View File

@ -34,6 +34,7 @@ from .qommon.misc import get_variadic_url
from .qommon.publisher import get_publisher_class
from .qommon.storage import StorableObject
from .qommon.template import Template
from .qommon.templatetags.qommon import unlazy
from .qommon.xml_storage import XmlStorableObject
data_source_functions = {}
@ -328,9 +329,17 @@ def _get_structured_items(data_source, mode=None):
elif len(value[0]) == 1:
return [{'id': x[0], 'text': x[0]} for x in value]
return value
elif isinstance(value[0], str):
elif isinstance(unlazy(value[0]), str):
return [{'id': x, 'text': x} for x in value]
return value
elif isinstance(value[0], dict):
return value
get_publisher().record_error(
'Python data source (%r) gave a non usable result' % data_source.get('value'),
context='[DATASOURCE]',
notify=data_source.get('notify_on_errors'),
record=data_source.get('record_on_errors'),
)
return []
except Exception as exc:
get_publisher().record_error(
'Failed to eval() Python data source (%r)' % data_source.get('value'),