asset/onlinesource: add fixed url,

pre defned online sources: add FAZ.net
This commit is contained in:
Frederik Jaeckel 2023-04-21 16:55:51 +02:00
parent 2166da5509
commit b28723cfb1
8 changed files with 120 additions and 13 deletions

View file

@ -8,6 +8,7 @@ from trytond.transaction import Transaction
from trytond.pool import Pool
from trytond.pyson import Eval, Bool, If, Date
from trytond.report import Report
from trytond.exceptions import UserError
from decimal import Decimal
from datetime import time
@ -84,8 +85,19 @@ class Asset(SymbolMixin, ModelSQL, ModelView):
help='Select sources for the course update. The course sources are tried until a valid value has been read.',
relation_name='investment.asset_source_rel',
origin='asset', target='source')
updtdays = fields.Selection(string='Select days', required=True,
selection=sel_updtdays)
updturl = fields.Char(
string='URL',
help='URL for data retrieval.',
states={
'invisible': Eval('updturl_enable', False) == False,
'required': Eval('updturl_enable', False) == True,
}, depends=['updturl_enable'])
updturl_enable = fields.Function(fields.Boolean(
string='URL required', readonly=True,
states={'invisible': True}),
'on_change_with_updturl_enable')
updtdays = fields.Selection(
string='Select days', required=True, selection=sel_updtdays)
updttime = fields.Time(string='Time',
states={
'readonly': ~Bool(Eval('updtsources')),
@ -206,6 +218,16 @@ class Asset(SymbolMixin, ModelSQL, ModelView):
"""
return 'work'
@fields.depends('updtsources')
def on_change_with_updturl_enable(self, name=None):
""" return True if a source has fixed-url
"""
if self.updtsources:
for usource in self.updtsources:
if usource.fixed_url == True:
return True
return False
@fields.depends('updtsources', 'updttime')
def on_change_updtsources(self):
""" clear time-fields

View file

@ -46,6 +46,10 @@ msgctxt "model:ir.message,text:msg_querytype_web"
msgid "Extract from web page"
msgstr "aus Webseite auslesen"
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."
##############
# ir.ui.menu #
@ -330,6 +334,18 @@ msgctxt "selection:investment.asset,updtdays:"
msgid "Mon - Sun"
msgstr "Mo - So"
msgctxt "field:investment.asset,updturl:"
msgid "URL"
msgstr "URL"
msgctxt "help:investment.asset,updturl:"
msgid "URL for data retrieval."
msgstr "URL für Datenabruf."
msgctxt "field:investment.asset,updturl_enable:"
msgid "URL required"
msgstr "URL required"
###############################
# investment.asset-source-rel #
@ -530,6 +546,14 @@ msgctxt "help:investment.source,query_method:"
msgid "Select the method to retrieve the data."
msgstr "Wählen Sie die Methode zum Abruf der Daten."
msgctxt "field:investment.source,fixed_url:"
msgid "Fixed URL"
msgstr "feste URL"
msgctxt "help:investment.source,fixed_url:"
msgid "URL must be defined at investment record."
msgstr "Die URL muss im Investitionsdatensatz definiert werden."
###################
# investment.rate #

View file

@ -34,6 +34,10 @@ msgctxt "model:ir.message,text:msg_querytype_web"
msgid "Extract from web page"
msgstr "Extract from web page"
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.ui.menu,name:menu_investment"
msgid "Investment"
msgstr "Investment"
@ -482,6 +486,14 @@ msgctxt "help:investment.source,query_method:"
msgid "Select the method to retrieve the data."
msgstr "Select the method to retrieve the data."
msgctxt "field:investment.source,fixed_url:"
msgid "Fixed URL"
msgstr "Fixed URL"
msgctxt "help:investment.source,fixed_url:"
msgid "URL must be defined at investment record."
msgstr "URL must be defined at investment record."
msgctxt "model:investment.rate,name:"
msgid "Rate"
msgstr "Rate"

View file

@ -20,6 +20,9 @@ full copyright notices and license terms. -->
<record model="ir.message" id="msg_querytype_web">
<field name="text">Extract from web page</field>
</record>
<record model="ir.message" id="msg_missing_url">
<field name="text">URL for the online source '%(oname)s' is missing.</field>
</record>
</data>
</tryton>

View file

@ -14,6 +14,7 @@ from trytond.model import ModelView, ModelSQL, fields
from trytond.pool import Pool
from trytond.pyson import Eval, Bool
from trytond.i18n import gettext
from trytond.exceptions import UserError
logger = logging.getLogger(__name__)
@ -60,6 +61,12 @@ class OnlineSource(ModelSQL, ModelView):
help='Select the method to retrieve the data.',
selection='get_query_methods')
url = fields.Char(string='URL', states=STATES_WEB, depends=DEPENDS_WEB)
fixed_url = fields.Boolean(
string='Fixed URL',
states={
'invisible': Eval('query_method', '') != 'web',
}, depends=DEPENDS_WEB,
help='URL must be defined at investment record.')
nohtml = fields.Boolean(
string='Remove HTML',
help='Removes HTML tags before the text is interpreted.',
@ -178,6 +185,12 @@ class OnlineSource(ModelSQL, ModelView):
"""
return True
@classmethod
def default_fixed_url(cls):
""" default: False
"""
return False
@fields.depends(*fields_check)
def on_change_nsin(self):
""" run request
@ -220,7 +233,7 @@ class OnlineSource(ModelSQL, ModelView):
def on_change_with_symbol(self, name=None):
return ''
@fields.depends('url', 'isin', 'nsin', 'symbol')
@fields.depends('url', 'isin', 'nsin', 'symbol', 'fixed_url')
def on_change_with_used_url(self, name=None):
""" get url for testing
"""
@ -229,6 +242,7 @@ class OnlineSource(ModelSQL, ModelView):
isin=self.isin,
nsin=self.nsin,
symbol=self.symbol,
url=self.url,
)
@classmethod
@ -246,7 +260,7 @@ class OnlineSource(ModelSQL, ModelView):
pass
@classmethod
def run_query_method(cls, osource, isin, nsin, symbol, debug=False):
def run_query_method(cls, osource, isin, nsin, symbol, url, debug=False):
""" run selected query to retrive data
result: {
'text': raw-text from query - for debug,
@ -265,6 +279,7 @@ class OnlineSource(ModelSQL, ModelView):
nsin=nsin,
symbol=symbol,
debug=debug,
url=url,
)
def call_online_source(self):
@ -274,7 +289,7 @@ class OnlineSource(ModelSQL, ModelView):
OSourc = Pool().get('investment.source')
result = OSourc.run_query_method(
self, self.isin, self.nsin,
self, self.isin, self.nsin, self.url,
self.symbol, debug=True)
if result is not None:
self.text = result.get('text', None)
@ -283,15 +298,24 @@ class OnlineSource(ModelSQL, ModelView):
self.fndrate = result.get('rate', None)
self.fndident = result.get('code', None)
def get_url_with_parameter(self, isin=None, nsin=None, symbol=None):
def get_url_with_parameter(
self, isin=None, nsin=None, symbol=None, url=None):
""" generate url
"""
if self.url:
return Template(self.url).substitute({
'isin': isin if isin is not None else '',
'nsin': nsin if nsin is not None else '',
'symbol': symbol if symbol is not None else '',
})
if self.fixed_url is True:
if url is None:
raise UserError(gettext(
'investment.msg_missing_url',
oname=self.rec_name,
))
return url
else :
if self.url:
return Template(self.url).substitute({
'isin': isin if isin is not None else '',
'nsin': nsin if nsin is not None else '',
'symbol': symbol if symbol is not None else '',
})
@classmethod
def update_rate(cls, asset):
@ -310,6 +334,7 @@ class OnlineSource(ModelSQL, ModelView):
isin=asset.isin,
nsin=asset.wkn,
symbol=asset.secsymb,
url=asset.updturl,
)
if len(updtsource.rgxident or '') > 0:
@ -387,7 +412,7 @@ class OnlineSource(ModelSQL, ModelView):
@classmethod
def read_from_website(
cls, updtsource, isin=None, nsin=None,
symbol=None, debug=False):
symbol=None, url=None, debug=False):
""" read from url, extract values
"""
result = {}
@ -401,6 +426,7 @@ class OnlineSource(ModelSQL, ModelView):
isin=isin,
nsin=nsin,
symbol=symbol,
url=url,
),
allow_redirects=True,
timeout=5.0)

View file

@ -78,6 +78,18 @@ full copyright notices and license terms. -->
<field name="rgxident">\nWKN / ISIN: [A-Z,0-9]+ / ([A-Z,0-9]+)\s+\n</field>
<field name="rgxidtype">isin</field>
</record>
<record model="investment.source" id="web_faz">
<field name="name">FAZ</field>
<field name="url">https://www.faz.net/aktuell/finanzen/kurs/etf/</field>
<field name="nohtml" eval="True"/>
<field name="fixed_url" eval="True"/>
<field name="rgxdate">(\d{2}\.\d{2}\.\d{4})</field>
<field name="rgxdatefmt">%d.%m.%y</field>
<field name="rgxrate">\nKurs(\d+.\d+)\s+€\s+\n</field>
<field name="rgxdecimal">,</field>
<field name="rgxident">\nWKN:.*\sISIN: ([A-Z,0-9]+).*\n</field>
<field name="rgxidtype">isin</field>
</record>
</data>
</tryton>

View file

@ -60,6 +60,11 @@ full copyright notices and license terms. -->
<label name="updtdays"/>
<field name="updtdays"/>
<label name="updturl"/>
<field name="updturl" colspan="3"/>
<field name="updturl_enable"/>
<newline/>
<field name="updtsources" colspan="4"/>
</page>
</notebook>

View file

@ -14,9 +14,12 @@ full copyright notices and license terms. -->
<field name="url" colspan="3"/>
<label name="nohtml"/>
<field name="nohtml"/>
<label id="labtp1" string=" "/>
<label xalign="0.0" colspan="3" name="nohtml"
string="URL parameter placeholders: ${isin}, ${nsin}, ${symbol}"/>
<label name="fixed_url"/>
<field name="fixed_url"/>
<newline/>
<separator colspan="6" name="url" string="Regular expressions to find data"/>