diff --git a/currency_rate_update_coingecko/__manifest__.py b/currency_rate_update_coingecko/__manifest__.py
index 5071893..09f3dc1 100644
--- a/currency_rate_update_coingecko/__manifest__.py
+++ b/currency_rate_update_coingecko/__manifest__.py
@@ -12,6 +12,6 @@
"installable": True,
"application": False,
"depends": ["currency_rate_update_mapping", "crypto_currency"],
- "data": ["data/res_currency_rate_provider.xml"],
+ "data": [],
"external_dependencies": {"python": ["pycgapi"]},
}
diff --git a/currency_rate_update_coingecko/data/res_currency_rate_provider.xml b/currency_rate_update_coingecko/data/res_currency_rate_provider.xml
deleted file mode 100644
index 9aacbd9..0000000
--- a/currency_rate_update_coingecko/data/res_currency_rate_provider.xml
+++ /dev/null
@@ -1,10 +0,0 @@
-
-
-
-
- CoinGecko
-
-
diff --git a/currency_rate_update_coingecko/models/res_currency_rate_provider_CoinGecko.py b/currency_rate_update_coingecko/models/res_currency_rate_provider_CoinGecko.py
index 5e2530d..3733951 100644
--- a/currency_rate_update_coingecko/models/res_currency_rate_provider_CoinGecko.py
+++ b/currency_rate_update_coingecko/models/res_currency_rate_provider_CoinGecko.py
@@ -25,7 +25,7 @@ def _get_supported_currencies(self):
# List of cryptocurrencies based on configured provider mappings
supported_currencies = (
self.env["res.currency.rate.provider.mapping"]
- .search([("provider_id", "=", self.id)])
+ .search([("provider_service", "=", self.service)])
.mapped("currency_id.name")
)
return supported_currencies
@@ -34,56 +34,9 @@ def _obtain_rates(self, base_currency, currencies, date_from, date_to):
self.ensure_one()
if self.service != "CoinGecko":
return super()._obtain_rates(base_currency, currencies, date_from, date_to)
- if date_from < date.today():
- return self._get_historical_rate_from_coingecko(
- date_from, date_to, base_currency
+ return self._get_historical_rate_from_coingecko(
+ date_from, date_to, base_currency
)
- else:
- return self._get_latest_rate_from_coingecko(base_currency)
-
- def _get_latest_rate_from_coingecko(self, base_currency):
- """Get all the exchange rates for today"""
- api = CoinGeckoAPI()
- today = date.today()
- data = {today: {}}
- for (
- currency
- ) in self.currency_ids.res_currency_rate_provider_mapping_ids.filtered(
- lambda l: l.provider_id == self
- ):
- try:
- coin_data = api.coin_historical_on_date(
- currency.provider_reference, today.strftime("%m-%d-%Y")
- )
- except Exception as e:
- _logger.warning(
- 'Currency Rate Provider "%(name)s" failed to obtain for %(currency)s currency'
- % {
- "name": self.name,
- "currency": currency.currency_id.name,
- },
- exc_info=True,
- )
- self.message_post(
- subject=_("Currency Rate Provider Failure"),
- body=_(
- 'Currency Rate Provider "%(name)s" failed to obtain data(check the rate provider mapping on the currency) :\n%(error)s'
- )
- % {
- "name": self.name,
- "currency": currency.currency_id.name,
- "error": str(e) if e else _("N/A"),
- },
- )
- continue
- rate = (
- coin_data.get("market_data")
- .get("current_price")
- .get(base_currency.lower(), 0)
- )
- if rate:
- data[today].update({currency.currency_id.name: 1 / rate})
- return data
def _get_historical_rate_from_coingecko(self, date_from, date_to, base_currency):
"""Get all the exchange rates from 'date_from' to 'date_to'"""
@@ -95,7 +48,7 @@ def _get_historical_rate_from_coingecko(self, date_from, date_to, base_currency)
for (
currency
) in self.currency_ids.res_currency_rate_provider_mapping_ids.filtered(
- lambda l: l.provider_id == self
+ lambda l: l.provider_service == self.service
):
try:
coin_data = api.coin_historical_on_date(
diff --git a/currency_rate_update_coingecko/tests/test_currency_rate_update_coingecko.py b/currency_rate_update_coingecko/tests/test_currency_rate_update_coingecko.py
index 90b355f..6735157 100644
--- a/currency_rate_update_coingecko/tests/test_currency_rate_update_coingecko.py
+++ b/currency_rate_update_coingecko/tests/test_currency_rate_update_coingecko.py
@@ -37,7 +37,7 @@ def setUpClass(cls):
cls.coingecko_provider_mapping = cls.CurrencyRateProviderMapping.create(
{
"currency_id": cls.lnk_currency.id,
- "provider_id": cls.coingecko_provider.id,
+ "provider_service": "CoinGecko",
"provider_reference": "chainlink",
}
)
diff --git a/currency_rate_update_mapping/models/res_currency_rate_provider_mapping.py b/currency_rate_update_mapping/models/res_currency_rate_provider_mapping.py
index 63e3670..afbf5d6 100644
--- a/currency_rate_update_mapping/models/res_currency_rate_provider_mapping.py
+++ b/currency_rate_update_mapping/models/res_currency_rate_provider_mapping.py
@@ -11,13 +11,20 @@ class ResCurrencyRateProviderMapping(models.Model):
string="Currency",
comodel_name="res.currency",
)
- provider_id = fields.Many2one(
+ provider_service = fields.Selection(
+ selection=lambda r: r.env["res.currency.rate.provider"]._fields["service"].selection,
string="Provider",
- comodel_name="res.currency.rate.provider",
- ondelete="restrict",
required=True,
)
provider_reference = fields.Char(
required=True,
help="Defines the reference to be used when fetching rates from the provider",
)
+
+ _sql_constraints = [
+ (
+ "provider_service_currency_id_uniq",
+ "UNIQUE(provider_service, currency_id)",
+ "This provider has already been setup for this currency.",
+ ),
+ ]
diff --git a/currency_rate_update_mapping/views/res_currency.xml b/currency_rate_update_mapping/views/res_currency.xml
index 65c2003..6984053 100644
--- a/currency_rate_update_mapping/views/res_currency.xml
+++ b/currency_rate_update_mapping/views/res_currency.xml
@@ -13,7 +13,7 @@
-
+