From 9f74b8fbf7e43c09683992439dcab6f4e7a69564 Mon Sep 17 00:00:00 2001 From: Frederik Jaeckel Date: Sun, 21 Jan 2024 18:55:21 +0100 Subject: [PATCH] check regex-code when save online-source --- locale/de.po | 4 ++++ locale/en.po | 16 ++++++++++++++++ message.xml | 3 +++ onlinesource.py | 24 +++++++++++++++++++++--- tests/source.py | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 94 insertions(+), 3 deletions(-) diff --git a/locale/de.po b/locale/de.po index 5d55abe..2e027be 100644 --- a/locale/de.po +++ b/locale/de.po @@ -50,6 +50,10 @@ msgctxt "model:ir.message,text:msg_missing_url" msgid "URL for the online source '%(oname)s' is missing." msgstr "URL für die Onlinequelle '%(oname)s' fehlt." +msgctxt "model:ir.message,text:msg_bug_in_regexquery" +msgid "Error in regex code of field '%(fname)s': %(errmsg)s [%(code)s]" +msgstr "Fehler in Regex-Code des Feldes '%(fname)s': %(errmsg)s [%(code)s]" + ############## # ir.ui.menu # diff --git a/locale/en.po b/locale/en.po index 7e0923e..7d76175 100644 --- a/locale/en.po +++ b/locale/en.po @@ -38,6 +38,10 @@ msgctxt "model:ir.message,text:msg_missing_url" msgid "URL for the online source '%(oname)s' is missing." msgstr "URL for the online source '%(oname)s' is missing." +msgctxt "model:ir.message,text:msg_bug_in_regexquery" +msgid "Error in regex code of field '%(fname)s': %(errmsg)s [%(code)s]" +msgstr "Error in regex code of field '%(fname)s': %(errmsg)s [%(code)s]" + msgctxt "model:ir.ui.menu,name:menu_investment" msgid "Investment" msgstr "Investment" @@ -294,6 +298,18 @@ msgctxt "selection:investment.asset,updtdays:" msgid "Mon - Sun" msgstr "Mon - Sun" +msgctxt "field:investment.asset,updturl:" +msgid "URL" +msgstr "URL" + +msgctxt "help:investment.asset,updturl:" +msgid "URL for data retrieval." +msgstr "URL for data retrieval." + +msgctxt "field:investment.asset,updturl_enable:" +msgid "URL required" +msgstr "URL required" + msgctxt "model:investment.asset_source_rel,name:" msgid "Asset Source Relation" msgstr "Asset Source Relation" diff --git a/message.xml b/message.xml index 2ef9571..2d681ef 100644 --- a/message.xml +++ b/message.xml @@ -23,6 +23,9 @@ full copyright notices and license terms. --> URL for the online source '%(oname)s' is missing. + + Error in regex code of field '%(fname)s': %(errmsg)s [%(code)s] + diff --git a/onlinesource.py b/onlinesource.py index a74696a..4060ae4 100644 --- a/onlinesource.py +++ b/onlinesource.py @@ -260,6 +260,15 @@ class OnlineSource(ModelSQL, ModelView): """ pass + @classmethod + def validate(cls, records): + """ check regex-code + """ + for record in records: + for x in ['rgxdate', 'rgxrate', 'rgxident']: + if x: + record.get_regex_result('', x) + @classmethod def run_query_method(cls, osource, isin, nsin, symbol, url, debug=False): """ run selected query to retrive data @@ -380,13 +389,22 @@ class OnlineSource(ModelSQL, ModelView): def get_regex_result(self, html_text, field_name): """ run regex on html-text, convert result """ + OSource = Pool().get('investment.source') + rgxcode = getattr(self, field_name) or '' if len(rgxcode) == 0: return None - search_result = re.compile(rgxcode).search(html_text) - if search_result is None: - return None + try: + search_result = re.compile(rgxcode).search(html_text) + if search_result is None: + return None + except Exception as e1: + raise UserError(gettext( + 'investment.msg_bug_in_regexquery', + errmsg=str(e1), + fname=getattr(OSource, field_name).string, + code=rgxcode)) try: result = search_result.group(1) diff --git a/tests/source.py b/tests/source.py index f784e4c..43ac22b 100644 --- a/tests/source.py +++ b/tests/source.py @@ -6,6 +6,7 @@ from trytond.tests.test_tryton import with_transaction from trytond.pool import Pool from trytond.transaction import Transaction +from trytond.exceptions import UserError from decimal import Decimal from datetime import time, date, datetime from unittest.mock import MagicMock @@ -131,5 +132,54 @@ High 34,87 EUR 'rgxdate' ), date(2022, 3, 14)) + @with_transaction() + def test_waitlist_source_check_regex_validate(self): + """ create source, check validation of regex-code + """ + pool = Pool() + OSource = pool.get('investment.source') + + self.assertRaisesRegex( + UserError, + r"Error in regex code of field 'Date': nothing to repeat " + + r"at position 0 \[\*+ multiple repeat\]", + OSource.create, + [{ + 'name': 'Check date', + 'rgxdate': '** multiple repeat', + 'rgxrate': 'rate -- multiple repeat', + 'rgxident': 'identifiert ** multiple repeat', + }]) + + self.assertRaisesRegex( + UserError, + r"Error in regex code of field 'Rate': multiple repeat " + + r"at position 6 \[rate \*+ multiple repeat\]", + OSource.create, + [{ + 'name': 'Check rate', + 'rgxdate': '-- multiple repeat', + 'rgxrate': 'rate ** multiple repeat', + 'rgxident': 'identifiert -- multiple repeat', + }]) + + self.assertRaisesRegex( + UserError, + r"Error in regex code of field 'Identifier': multiple " + + r"repeat at position 13 \[identifiert \*+ multiple repeat\]", + OSource.create, + [{ + 'name': 'Check rgxident', + 'rgxdate': '-- multiple repeat', + 'rgxrate': 'rate -- multiple repeat', + 'rgxident': 'identifiert ** multiple repeat', + }]) + + OSource.create([{ + 'name': 'Check rgxident', + 'rgxdate': '-- multiple repeat', + 'rgxrate': 'rate -- multiple repeat', + 'rgxident': 'identifiert -- multiple repeat', + }]) # end SourceTestCase