mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Update exchange rates when launching the server
- Ensures that the exchange rates don't get messed up if the base currency is changed!
This commit is contained in:
parent
52fc698b51
commit
4ddeab3330
@ -4,6 +4,7 @@ import logging
|
|||||||
|
|
||||||
from django.apps import AppConfig
|
from django.apps import AppConfig
|
||||||
from django.core.exceptions import AppRegistryNotReady
|
from django.core.exceptions import AppRegistryNotReady
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
from InvenTree.ready import canAppAccessDatabase
|
from InvenTree.ready import canAppAccessDatabase
|
||||||
import InvenTree.tasks
|
import InvenTree.tasks
|
||||||
@ -19,6 +20,7 @@ class InvenTreeConfig(AppConfig):
|
|||||||
|
|
||||||
if canAppAccessDatabase():
|
if canAppAccessDatabase():
|
||||||
self.start_background_tasks()
|
self.start_background_tasks()
|
||||||
|
self.update_exchange_rates()
|
||||||
|
|
||||||
def start_background_tasks(self):
|
def start_background_tasks(self):
|
||||||
|
|
||||||
@ -49,3 +51,49 @@ class InvenTreeConfig(AppConfig):
|
|||||||
'InvenTree.tasks.update_exchange_rates',
|
'InvenTree.tasks.update_exchange_rates',
|
||||||
schedule_type=Schedule.DAILY,
|
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
|
||||||
|
|
||||||
|
if update:
|
||||||
|
update_exchange_rates()
|
||||||
|
@ -168,6 +168,7 @@ def update_exchange_rates():
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
from InvenTree.exchange import InvenTreeExchange
|
from InvenTree.exchange import InvenTreeExchange
|
||||||
|
from djmoney.contrib.exchange.models import Rate
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
except AppRegistryNotReady:
|
except AppRegistryNotReady:
|
||||||
# Apps not yet loaded!
|
# Apps not yet loaded!
|
||||||
@ -182,6 +183,9 @@ def update_exchange_rates():
|
|||||||
|
|
||||||
backend.update_rates(base_currency=base)
|
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):
|
def send_email(subject, body, recipients, from_email=None):
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user