json cell: add log_errors parameter (#17826)

This commit is contained in:
Thomas NOËL 2017-07-28 00:42:18 +02:00
parent 1f0eaa3057
commit c6cf29c988
2 changed files with 14 additions and 2 deletions

View File

@ -831,6 +831,7 @@ class JsonCellBase(CellBase):
template_string = None template_string = None
varnames = None varnames = None
force_async = False force_async = False
log_errors = True
actions = {} actions = {}
additional_data = None additional_data = None
# [ # [
@ -857,7 +858,8 @@ class JsonCellBase(CellBase):
context[varname] = context['request'].GET[varname] context[varname] = context['request'].GET[varname]
self._json_content = None self._json_content = None
data_urls = [{'key': 'json', 'url': self.url, 'cache_duration': self.cache_duration}] data_urls = [{'key': 'json', 'url': self.url, 'cache_duration': self.cache_duration,
'log_errors': self.log_errors}]
data_urls.extend(self.additional_data or []) data_urls.extend(self.additional_data or [])
for data_url_dict in data_urls: for data_url_dict in data_urls:
@ -878,6 +880,7 @@ class JsonCellBase(CellBase):
without_user=True, without_user=True,
raise_if_not_cached=not(context.get('synchronous')), raise_if_not_cached=not(context.get('synchronous')),
invalidate_cache=invalidate_cache, invalidate_cache=invalidate_cache,
log_errors=data_url_dict.get('log_errors', self.log_errors),
) )
extra_context[data_key + '_url'] = url extra_context[data_key + '_url'] = url
extra_context[data_key + '_status'] = json_response.status_code extra_context[data_key + '_status'] = json_response.status_code
@ -1036,6 +1039,11 @@ class ConfigJsonCell(JsonCellBase):
return settings.JSON_CELL_TYPES[self.key].get('force_async', return settings.JSON_CELL_TYPES[self.key].get('force_async',
JsonCellBase.force_async) JsonCellBase.force_async)
@property
def log_errors(self):
return settings.JSON_CELL_TYPES[self.key].get('log_errors',
JsonCellBase.log_errors)
@property @property
def actions(self): def actions(self):
return settings.JSON_CELL_TYPES[self.key].get('actions', return settings.JSON_CELL_TYPES[self.key].get('actions',

View File

@ -341,6 +341,7 @@ def test_config_json_cell_with_param_in_url(app):
'test-config-json-cell': { 'test-config-json-cell': {
'name': 'Foobar', 'name': 'Foobar',
'url': 'http://foo?var=[identifier]', 'url': 'http://foo?var=[identifier]',
'log_errors': False,
'form': [ 'form': [
{ {
"varname": "identifier", "varname": "identifier",
@ -366,6 +367,7 @@ def test_config_json_cell_with_param_in_url(app):
resp = app.get(url) resp = app.get(url)
assert requests_get.call_count == 1 assert requests_get.call_count == 1
assert requests_get.call_args[0][0] == 'http://foo?var=plop' assert requests_get.call_args[0][0] == 'http://foo?var=plop'
assert requests_get.call_args[-1]['log_errors'] == False
def test_json_force_async(): def test_json_force_async():
cell = JsonCellBase() cell = JsonCellBase()
@ -407,7 +409,7 @@ def test_config_json_cell_additional_url(app):
'name': 'Foobar', 'name': 'Foobar',
'url': 'http://foo', 'url': 'http://foo',
'additional-data': [ 'additional-data': [
{'key': 'plop', 'url': 'http://bar'}, {'key': 'plop', 'url': 'http://bar', 'log_errors': False},
] ]
}}, }},
TEMPLATE_DIRS=['%s/templates-1' % os.path.abspath(os.path.dirname(__file__))]): TEMPLATE_DIRS=['%s/templates-1' % os.path.abspath(os.path.dirname(__file__))]):
@ -427,7 +429,9 @@ def test_config_json_cell_additional_url(app):
assert resp.body.strip() == '/var1=toto/var2=toto/' assert resp.body.strip() == '/var1=toto/var2=toto/'
assert len(requests_get.mock_calls) == 2 assert len(requests_get.mock_calls) == 2
assert requests_get.mock_calls[0][1][0] == 'http://foo' assert requests_get.mock_calls[0][1][0] == 'http://foo'
assert requests_get.mock_calls[0][-1]['log_errors'] == True
assert requests_get.mock_calls[1][1][0] == 'http://bar' assert requests_get.mock_calls[1][1][0] == 'http://bar'
assert requests_get.mock_calls[1][-1]['log_errors'] == False
with mock.patch('combo.utils.requests.get') as requests_get: with mock.patch('combo.utils.requests.get') as requests_get:
data = {'data': 'toto'} data = {'data': 'toto'}