data_source: don't record errors on preview (#54326)

This commit is contained in:
Lauréline Guérin 2021-05-27 16:17:22 +02:00
parent cf7c21bbbd
commit 08bb5f7387
No known key found for this signature in database
GPG Key ID: 1FAB9B9B4F93D473
6 changed files with 46 additions and 10 deletions

View File

@ -284,6 +284,16 @@ def test_data_sources_view(pub):
resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id)
assert '<a href="http://example.net/foo/bar"' in resp.text
# errors
data_source.data_source = {'type': 'json', 'value': 'http://remote.example.net/404'}
data_source.notify_on_errors = True
data_source.record_on_errors = True
data_source.store()
resp = app.get('/backoffice/settings/data-sources/%s/' % data_source.id)
assert 'Preview' not in resp.text
if pub.is_using_postgresql():
assert pub.loggederror_class.count() == 0 # error not recorded
# check geojson
geojson_file_path = os.path.join(pub.app_dir, 'test.geojson')
with open(geojson_file_path, 'w') as geojson_file:

View File

@ -1665,7 +1665,9 @@ def test_form_edit_item_field_data_source(pub):
resp = resp.form.submit('submit').follow()
data_source = NamedDataSource(name='Foobar')
data_source.data_source = {'type': 'formula', 'value': '[]'}
data_source.data_source = {'type': 'json', 'value': 'http://remote.example.net/404'}
data_source.record_on_errors = True
data_source.notify_on_errors = True
data_source.store()
resp = app.get('/backoffice/forms/1/fields/1/')
@ -1678,8 +1680,11 @@ def test_form_edit_item_field_data_source(pub):
]
resp.form['data_mode'].value = 'data-source'
resp.form['data_source$type'].value = 'foobar'
resp = resp.form.submit('submit').follow()
resp.form.submit('submit').follow()
resp = app.get('/backoffice/forms/1/')
assert FormDef.get(formdef.id).fields[0].data_source == {'type': 'foobar'}
if pub.is_using_postgresql():
assert pub.loggederror_class.count() == 0 # error not recorded
carddef = CardDef()
carddef.name = 'Baz'
@ -2639,6 +2644,8 @@ def test_form_comment_with_error_in_wscall(http_requests, pub):
'method': 'POST',
'post_data': {'c': 'd'},
}
wscall.record_on_errors = True
wscall.notify_on_errors = True
wscall.store()
FormDef.wipe()
@ -2650,6 +2657,8 @@ def test_form_comment_with_error_in_wscall(http_requests, pub):
app = login(get_app(pub))
resp = app.get('/backoffice/forms/%s/' % formdef.id)
assert 'x [webservice.xxx.foobar] x' in resp.text
if pub.is_using_postgresql():
assert pub.loggederror_class.count() == 0 # error not recorded
def test_form_new_computed_field(pub):

View File

@ -14,7 +14,7 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
from quixote import get_publisher, redirect
from quixote import get_publisher, get_request, redirect
from quixote.directory import Directory
from quixote.html import TemplateIO, htmltext
@ -319,7 +319,11 @@ class NamedDataSourcePage(Directory):
return formdefs
def preview_block(self):
data_source = self.datasource.extended_data_source
try:
get_request().disable_error_notifications = True
data_source = self.datasource.extended_data_source
finally:
get_request().disable_error_notifications = False
if data_source.get('type') not in ('json', 'geojson', 'formula', 'wcs:users'):
return ''
items = get_structured_items(data_source)

View File

@ -1144,6 +1144,7 @@ class FormDefPage(Directory):
field.id = i
if getattr(field, 'add_to_form', None):
try:
get_request().disable_error_notifications = True
field.add_to_form(form)
except Exception as e:
form.widgets.append(
@ -1152,6 +1153,8 @@ class FormDefPage(Directory):
% (_('Error previewing field.'), e)
)
)
finally:
get_request().disable_error_notifications = False
else:
if field.key == 'page':
if on_page:

View File

@ -478,14 +478,19 @@ class NamedDataSource(XmlStorableObject):
@property
def extended_data_source(self):
notify_on_errors = self.notify_on_errors
record_on_errors = self.record_on_errors
if getattr(get_request(), 'disable_error_notifications', None) is True:
notify_on_errors = False
record_on_errors = False
if self.type == 'geojson':
data_source = self.data_source.copy()
data_source.update(
{
'id_property': self.id_property,
'label_template_property': self.label_template_property,
'notify_on_errors': self.notify_on_errors,
'record_on_errors': self.record_on_errors,
'notify_on_errors': notify_on_errors,
'record_on_errors': record_on_errors,
}
)
return data_source
@ -496,8 +501,8 @@ class NamedDataSource(XmlStorableObject):
'data_attribute': self.data_attribute,
'id_attribute': self.id_attribute,
'text_attribute': self.text_attribute,
'notify_on_errors': self.notify_on_errors,
'record_on_errors': self.record_on_errors,
'notify_on_errors': notify_on_errors,
'record_on_errors': record_on_errors,
}
)
return data_source

View File

@ -276,10 +276,15 @@ class NamedWsCall(XmlStorableObject):
return {'webservice': WsCallsSubstitutionProxy()}
def call(self):
notify_on_errors = self.notify_on_errors
record_on_errors = self.record_on_errors
if getattr(get_request(), 'disable_error_notifications', None) is True:
notify_on_errors = False
record_on_errors = False
data = call_webservice(
cache=True,
notify_on_errors=self.notify_on_errors,
record_on_errors=self.record_on_errors,
notify_on_errors=notify_on_errors,
record_on_errors=record_on_errors,
**(self.request or {}),
)[2]
return json_loads(data)