mirror of
https://github.com/inventree/InvenTree.git
synced 2025-04-30 20:46:47 +00:00
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
This commit is contained in:
parent
352fb4f6ff
commit
30cf97d85b
@ -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.error import URLError
|
||||||
from urllib.request import urlopen
|
|
||||||
|
|
||||||
from django.db.utils import OperationalError
|
from django.db.utils import OperationalError
|
||||||
|
|
||||||
import certifi
|
import requests
|
||||||
from djmoney.contrib.exchange.backends.base import SimpleExchangeBackend
|
from djmoney.contrib.exchange.backends.base import SimpleExchangeBackend
|
||||||
|
|
||||||
from common.settings import currency_code_default, currency_codes
|
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):
|
class InvenTreeExchange(SimpleExchangeBackend):
|
||||||
"""Backend for automatically updating currency exchange rates.
|
"""Backend for automatically updating currency exchange rates.
|
||||||
|
|
||||||
Uses the `exchangerate.host` service API
|
Uses the `frankfurter.app` service API
|
||||||
"""
|
"""
|
||||||
|
|
||||||
name = "InvenTreeExchange"
|
name = "InvenTreeExchange"
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
"""Set API url."""
|
"""Set API url."""
|
||||||
self.url = "https://api.exchangerate.host/latest"
|
self.url = "https://api.frankfurter.app/latest"
|
||||||
|
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
def get_params(self):
|
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
|
# No API key is required
|
||||||
return {
|
return {
|
||||||
}
|
}
|
||||||
@ -40,14 +39,22 @@ class InvenTreeExchange(SimpleExchangeBackend):
|
|||||||
url = self.get_url(**kwargs)
|
url = self.get_url(**kwargs)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
context = ssl.create_default_context(cafile=certifi.where())
|
response = requests.get(url=url, timeout=5)
|
||||||
response = urlopen(url, timeout=5, context=context)
|
return response.content
|
||||||
return response.read()
|
|
||||||
except Exception:
|
except Exception:
|
||||||
# Something has gone wrong, but we can just try again next time
|
# Something has gone wrong, but we can just try again next time
|
||||||
# Raise a TypeError so the outer function can handle this
|
# Raise a TypeError so the outer function can handle this
|
||||||
raise TypeError
|
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):
|
def update_rates(self, base_currency=None):
|
||||||
"""Set the requested currency codes and get rates."""
|
"""Set the requested currency codes and get rates."""
|
||||||
# Set default - see B008
|
# Set default - see B008
|
||||||
|
@ -807,10 +807,6 @@ class CurrencyTests(TestCase):
|
|||||||
def test_rates(self):
|
def test_rates(self):
|
||||||
"""Test exchange rate update."""
|
"""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
|
# Initially, there will not be any exchange rate information
|
||||||
rates = Rate.objects.all()
|
rates = Rate.objects.all()
|
||||||
|
|
||||||
@ -837,6 +833,7 @@ class CurrencyTests(TestCase):
|
|||||||
|
|
||||||
else: # pragma: no cover
|
else: # pragma: no cover
|
||||||
print("Exchange rate update failed - retrying")
|
print("Exchange rate update failed - retrying")
|
||||||
|
print(f'Expected {currency_codes()}, got {[a.currency for a in rates]}')
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
self.assertTrue(update_successful)
|
self.assertTrue(update_successful)
|
||||||
|
@ -1026,10 +1026,6 @@ class CurrencyAPITests(InvenTreeAPITestCase):
|
|||||||
def test_refresh_endpoint(self):
|
def test_refresh_endpoint(self):
|
||||||
"""Call the 'refresh currencies' endpoint"""
|
"""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
|
from djmoney.contrib.exchange.models import Rate
|
||||||
|
|
||||||
# Delete any existing exchange rate data
|
# Delete any existing exchange rate data
|
||||||
|
Loading…
x
Reference in New Issue
Block a user