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 8ed7fbcba5.

* Revert "fixed LOG style errors"

This reverts commit 578da01a67.

* switched to requests

* Revert "Revert "fixed LOG style errors""

This reverts commit 78e5eb0e69.

* Revert "Revert "Revert "fixed LOG style errors"""

This reverts commit d21838959f.

* Revert "Revert "Revert "Revert "fixed LOG style errors""""

This reverts commit 3881923c0b.

* re-enable checks

* extended debug message

* add base cur

* reverted name change
This commit is contained in:
Matthias Mair 2023-10-03 03:12:33 +02:00 committed by GitHub
parent 352fb4f6ff
commit 30cf97d85b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 18 deletions

View File

@ -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

View File

@ -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)

View File

@ -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