From 30cf97d85b6382c3869efde346203d260a3fe379 Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Tue, 3 Oct 2023 03:12:33 +0200 Subject: [PATCH] Exchange rate emergency fix (#5632) * fixed LOG style errors * Disabled exchange rate checks See https://github.com/inventree/InvenTree/issues/5631 * Implement frakfurter.app as new exchange host Closes #5631 * Revert "Disabled exchange rate checks" This reverts commit 8ed7fbcba5e190d58d7545efbc903e8f72ff1ac7. * Revert "fixed LOG style errors" This reverts commit 578da01a678947af2db1493751b1c85a183b7d7a. * switched to requests * Revert "Revert "fixed LOG style errors"" This reverts commit 78e5eb0e69a15d4960742bc3ffaabe68a97187e3. * Revert "Revert "Revert "fixed LOG style errors""" This reverts commit d21838959fcdfd6e274e58dbdffaa73c2233f78e. * Revert "Revert "Revert "Revert "fixed LOG style errors"""" This reverts commit 3881923c0b3935691ad8d89ea4fbbf9c094c33de. * re-enable checks * extended debug message * add base cur * reverted name change --- InvenTree/InvenTree/exchange.py | 27 +++++++++++++++++---------- InvenTree/InvenTree/tests.py | 5 +---- InvenTree/common/tests.py | 4 ---- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/InvenTree/InvenTree/exchange.py b/InvenTree/InvenTree/exchange.py index 0225270f27..3889ebf157 100644 --- a/InvenTree/InvenTree/exchange.py +++ b/InvenTree/InvenTree/exchange.py @@ -1,12 +1,11 @@ -"""Exchangerate backend to use `exchangerate.host` to get rates.""" +"""Exchangerate backend to use `frankfurter.app` to get rates.""" -import ssl +from decimal import Decimal from urllib.error import URLError -from urllib.request import urlopen from django.db.utils import OperationalError -import certifi +import requests from djmoney.contrib.exchange.backends.base import SimpleExchangeBackend from common.settings import currency_code_default, currency_codes @@ -15,19 +14,19 @@ from common.settings import currency_code_default, currency_codes class InvenTreeExchange(SimpleExchangeBackend): """Backend for automatically updating currency exchange rates. - Uses the `exchangerate.host` service API + Uses the `frankfurter.app` service API """ name = "InvenTreeExchange" def __init__(self): """Set API url.""" - self.url = "https://api.exchangerate.host/latest" + self.url = "https://api.frankfurter.app/latest" super().__init__() def get_params(self): - """Placeholder to set API key. Currently not required by `exchangerate.host`.""" + """Placeholder to set API key. Currently not required by `frankfurter.app`.""" # No API key is required return { } @@ -40,14 +39,22 @@ class InvenTreeExchange(SimpleExchangeBackend): url = self.get_url(**kwargs) try: - context = ssl.create_default_context(cafile=certifi.where()) - response = urlopen(url, timeout=5, context=context) - return response.read() + response = requests.get(url=url, timeout=5) + return response.content except Exception: # Something has gone wrong, but we can just try again next time # Raise a TypeError so the outer function can handle this raise TypeError + def get_rates(self, **params): + """Intersect the requested currency codes with the available codes.""" + rates = super().get_rates(**params) + + # Add the base currency to the rates + rates[params["base_currency"]] = Decimal("1.0") + + return rates + def update_rates(self, base_currency=None): """Set the requested currency codes and get rates.""" # Set default - see B008 diff --git a/InvenTree/InvenTree/tests.py b/InvenTree/InvenTree/tests.py index 12d6c97fd8..8fec1fff28 100644 --- a/InvenTree/InvenTree/tests.py +++ b/InvenTree/InvenTree/tests.py @@ -807,10 +807,6 @@ class CurrencyTests(TestCase): def test_rates(self): """Test exchange rate update.""" - # 2023-09-28 check DISABLED due to https://github.com/inventree/InvenTree/issues/5631 - # TODO re-enable after #5631 is solved - return True - # Initially, there will not be any exchange rate information rates = Rate.objects.all() @@ -837,6 +833,7 @@ class CurrencyTests(TestCase): else: # pragma: no cover print("Exchange rate update failed - retrying") + print(f'Expected {currency_codes()}, got {[a.currency for a in rates]}') time.sleep(1) self.assertTrue(update_successful) diff --git a/InvenTree/common/tests.py b/InvenTree/common/tests.py index ef48462b84..ccf572f23a 100644 --- a/InvenTree/common/tests.py +++ b/InvenTree/common/tests.py @@ -1026,10 +1026,6 @@ class CurrencyAPITests(InvenTreeAPITestCase): def test_refresh_endpoint(self): """Call the 'refresh currencies' endpoint""" - # 2023-09-28 check DISABLED due to https://github.com/inventree/InvenTree/issues/5631 - # TODO re-enable after #5631 is solved - return True - from djmoney.contrib.exchange.models import Rate # Delete any existing exchange rate data