Merge pull request #1612 from SchrodingersGat/exchange-fixes

Remove debug message
This commit is contained in:
Oliver 2021-05-28 13:26:14 +10:00 committed by GitHub
commit 4c78f88aa3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 86 additions and 3 deletions

View File

@ -4,8 +4,9 @@ import logging
from django.apps import AppConfig
from django.core.exceptions import AppRegistryNotReady
from django.conf import settings
from InvenTree.ready import canAppAccessDatabase
from InvenTree.ready import isInTestMode, canAppAccessDatabase
import InvenTree.tasks
@ -20,6 +21,9 @@ class InvenTreeConfig(AppConfig):
if canAppAccessDatabase():
self.start_background_tasks()
if not isInTestMode():
self.update_exchange_rates()
def start_background_tasks(self):
try:
@ -49,3 +53,53 @@ class InvenTreeConfig(AppConfig):
'InvenTree.tasks.update_exchange_rates',
schedule_type=Schedule.DAILY,
)
def update_exchange_rates(self):
"""
Update exchange rates each time the server is started, *if*:
a) Have not been updated recently (one day or less)
b) The base exchange rate has been altered
"""
try:
from djmoney.contrib.exchange.models import ExchangeBackend
from datetime import datetime, timedelta
from InvenTree.tasks import update_exchange_rates
except AppRegistryNotReady:
pass
base_currency = settings.BASE_CURRENCY
update = False
try:
backend = ExchangeBackend.objects.get(name='InvenTreeExchange')
last_update = backend.last_update
if last_update is not None:
delta = datetime.now().date() - last_update.date()
if delta > timedelta(days=1):
print(f"Last update was {last_update}")
update = True
else:
# Never been updated
print("Exchange backend has never been updated")
update = True
# Backend currency has changed?
if not base_currency == backend.base_currency:
print(f"Base currency changed from {backend.base_currency} to {base_currency}")
update = True
except (ExchangeBackend.DoesNotExist):
print("Exchange backend not found - updating")
update = True
except:
# Some other error - potentially the tables are not ready yet
return
if update:
update_exchange_rates()

View File

@ -1,6 +1,17 @@
import sys
def isInTestMode():
"""
Returns True if the database is in testing mode
"""
if 'test' in sys.argv:
return True
return False
def canAppAccessDatabase():
"""
Returns True if the apps.py file can access database records.

View File

@ -168,10 +168,24 @@ def update_exchange_rates():
try:
from InvenTree.exchange import InvenTreeExchange
from djmoney.contrib.exchange.models import ExchangeBackend, Rate
from django.conf import settings
except AppRegistryNotReady:
# Apps not yet loaded!
return
except:
# Other error?
return
# Test to see if the database is ready yet
try:
backend = ExchangeBackend.objects.get(name='InvenTreeExchange')
except ExchangeBackend.DoesNotExist:
pass
except:
# Some other error
print("Database not ready")
return
backend = InvenTreeExchange()
print(f"Updating exchange rates from {backend.url}")
@ -182,6 +196,9 @@ def update_exchange_rates():
backend.update_rates(base_currency=base)
# Remove any exchange rates which are not in the provided currencies
Rate.objects.filter(backend="InvenTreeExchange").exclude(currency__in=settings.CURRENCIES).delete()
def send_email(subject, body, recipients, from_email=None):
"""

View File

@ -775,6 +775,9 @@ class SettingsView(TemplateView):
class CurrencyRefreshView(RedirectView):
"""
POST endpoint to refresh / update exchange rates
"""
url = reverse_lazy("settings-currencies")
@ -783,8 +786,6 @@ class CurrencyRefreshView(RedirectView):
On a POST request we will attempt to refresh the exchange rates
"""
print("POST!")
# Will block for a little bit
InvenTree.tasks.update_exchange_rates()