Skip to content

Commit

Permalink
Merge pull request #75 from onesteinbv/16.0-update-coingecko_module
Browse files Browse the repository at this point in the history
Removed data file,made changes so that provider service is used for mapping instead of rate provider and constraint for provider mapping
  • Loading branch information
ByteMeAsap authored Dec 9, 2024
2 parents 998079c + f4cddbf commit 8bef5ca
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 67 deletions.
2 changes: 1 addition & 1 deletion currency_rate_update_coingecko/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]},
}
10 changes: 0 additions & 10 deletions currency_rate_update_coingecko/data/res_currency_rate_provider.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'"""
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
),
]
2 changes: 1 addition & 1 deletion currency_rate_update_mapping/views/res_currency.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<page string="Rate Provider Mappings" name="rate_provider_mappings">
<field name="res_currency_rate_provider_mapping_ids" widget="one2many">
<tree editable="top" limit="25">
<field name="provider_id"/>
<field name="provider_service"/>
<field name="provider_reference"/>
</tree>
</field>
Expand Down

0 comments on commit 8bef5ca

Please sign in to comment.