mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Schedule a daily task to update currency information
This commit is contained in:
parent
01d444279c
commit
663f5562e8
@ -44,3 +44,8 @@ class InvenTreeConfig(AppConfig):
|
||||
schedule_type=Schedule.MINUTES,
|
||||
minutes=15
|
||||
)
|
||||
|
||||
InvenTree.tasks.schedule_task(
|
||||
'InvenTree.tasks.update_exchange_rates',
|
||||
schedule_type=Schedule.DAILY,
|
||||
)
|
||||
|
@ -513,7 +513,8 @@ CURRENCIES = CONFIG.get(
|
||||
],
|
||||
)
|
||||
|
||||
# TODO - Allow live web-based backends in the future
|
||||
BASE_CURRENCY = CONFIG.get('base_currency', 'USD')
|
||||
|
||||
EXCHANGE_BACKEND = 'InvenTree.exchange.InvenTreeManualExchangeBackend'
|
||||
|
||||
# Extract email settings from the config file
|
||||
|
@ -161,6 +161,34 @@ def check_for_updates():
|
||||
)
|
||||
|
||||
|
||||
def update_exchange_rates():
|
||||
"""
|
||||
If an API key for fixer.io has been provided, attempt to update currency exchange rates
|
||||
"""
|
||||
|
||||
try:
|
||||
import common.models
|
||||
from django.conf import settings
|
||||
from djmoney.contrib.exchange.backends import FixerBackend
|
||||
except AppRegistryNotReady:
|
||||
# Apps not yet loaded!
|
||||
return
|
||||
|
||||
fixer_api_key = common.models.InvenTreeSetting.get_setting('INVENTREE_FIXER_API_KEY', '').strip()
|
||||
|
||||
if not fixer_api_key:
|
||||
# API key not provided
|
||||
return
|
||||
|
||||
backend = FixerBackend(access_key=fixer_api_key)
|
||||
|
||||
currencies = ','.join(settings.CURRENCIES)
|
||||
|
||||
base = settings.BASE_CURRENCY
|
||||
|
||||
result = backend.update_rates(base_currency=base, symbols=currencies)
|
||||
|
||||
|
||||
def send_email(subject, body, recipients, from_email=None):
|
||||
"""
|
||||
Send an email with the specified subject and body,
|
||||
|
Loading…
Reference in New Issue
Block a user