Files
tryton/currency.py
T
Cédric Krier 82ce2eef2b Add currency_rs module
issue10687
review375281003
2021-10-09 13:03:31 +02:00

102 lines
3.5 KiB
Python

# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from decimal import Decimal
from lxml import objectify
from zeep import Client, Transport
from zeep.exceptions import Error
from trytond.cache import Cache
from trytond.config import config
from trytond.model import fields
from trytond.pool import PoolMeta
from trytond.pyson import Eval, If
from trytond.modules.currency.currency import CronFetchError
URL = (
'https://webservices.nbs.rs/CommunicationOfficeService1_0/'
'ExchangeRateXmlService.asmx?WSDL')
TIMEOUT = config.getfloat('currency_rs', 'requests_timeout', default=300)
def get_client(username, password, license_id):
client = Client(URL, transport=Transport(operation_timeout=TIMEOUT))
client.set_default_soapheaders({
'AuthenticationHeader': {
'UserName': username,
'Password': password,
'LicenceID': license_id,
},
})
return client
class Cron(metaclass=PoolMeta):
__name__ = 'currency.cron'
_states = {
'required': Eval('source') == 'nbs_rs',
'invisible': Eval('source') != 'nbs_rs',
}
_depends = ['source']
rs_username = fields.Char("Username", states=_states, depends=_depends)
rs_password = fields.Char("Password", states=_states, depends=_depends)
rs_license_id = fields.Char("License ID", states=_states, depends=_depends)
rs_list_type = fields.Selection(
'get_rs_list_types', "List Type", states=_states, depends=_depends)
_rs_list_types = Cache(__name__ + '.get_rs_list_types', context=False)
del _states, _depends
@classmethod
def __setup__(cls):
super().__setup__()
cls.source.selection.append(('nbs_rs', "Serbian National Bank"))
cls.currency.domain = [
cls.currency.domain or [],
If(Eval('source') == 'nbs_rs',
('code', '=', 'RSD'),
()),
]
if 'source' not in cls.currency.depends:
cls.currency.depends.append('source')
@fields.depends('rs_username', 'rs_password', 'rs_license_id')
def _rs_client(self):
return get_client(
self.rs_username, self.rs_password, self.rs_license_id)
@fields.depends('rs_list_type', methods=['_rs_client'])
def get_rs_list_types(self):
types = self._rs_list_types.get(None)
if types is not None:
return types
try:
client = self._rs_client()
response = client.service.GetExchangeRateListType()
except Error:
return [(self.rs_list_type, self.rs_list_type or "")]
types = []
data = objectify.fromstring(response)
for list_type in data.iterchildren():
types.append(
(str(list_type.ExchangeRateListTypeID), str(list_type.Name)))
self._rs_list_types.set(None, types)
return types
def fetch_nbs_rs(self, date):
try:
client = self._rs_client()
response = client.service.GetExchangeRateByDate(
date.strftime('%Y%m%d'), self.rs_list_type)
except Error as e:
raise CronFetchError() from e
data = objectify.fromstring(response)
return {
r.CurrencyCodeAlfaChar: (
Decimal(r.Unit.text) / Decimal(r.MiddleRate.text))
for r in data.iterchildren()
if r.Date == date.strftime('%d.%m.%Y')}