mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Added InvenTreeFixerExchangeBackend class
This commit is contained in:
parent
6c91d067d7
commit
34ded08ee7
@ -1,4 +1,10 @@
|
||||
from django.conf import settings as inventree_settings
|
||||
|
||||
from djmoney import settings as djmoney_settings
|
||||
from djmoney.contrib.exchange.backends.base import BaseExchangeBackend
|
||||
from djmoney.contrib.exchange.backends import FixerBackend
|
||||
|
||||
from common.models import InvenTreeSetting
|
||||
|
||||
|
||||
class InvenTreeManualExchangeBackend(BaseExchangeBackend):
|
||||
@ -19,3 +25,41 @@ class InvenTreeManualExchangeBackend(BaseExchangeBackend):
|
||||
"""
|
||||
|
||||
return {}
|
||||
|
||||
|
||||
class InvenTreeFixerExchangeBackend(FixerBackend):
|
||||
"""
|
||||
Backend for updating currency exchange rates using Fixer.IO API
|
||||
"""
|
||||
|
||||
def get_api_key(self):
|
||||
""" Get API key from global settings """
|
||||
|
||||
fixer_api_key = InvenTreeSetting.get_setting('INVENTREE_FIXER_API_KEY', '').strip()
|
||||
|
||||
if not fixer_api_key:
|
||||
# API key not provided
|
||||
return None
|
||||
|
||||
return fixer_api_key
|
||||
|
||||
def __init__(self):
|
||||
""" Override FixerBackend init to get access_key from global settings """
|
||||
|
||||
fixer_api_key = self.get_api_key()
|
||||
|
||||
super().__init__(url=djmoney_settings.FIXER_URL, access_key=fixer_api_key)
|
||||
|
||||
def update_rates(self):
|
||||
""" Override update_rates method using currencies found in the settings """
|
||||
|
||||
currencies = ','.join(inventree_settings.CURRENCIES)
|
||||
|
||||
base = inventree_settings.BASE_CURRENCY
|
||||
|
||||
super().update_rates(base_currency=base, symbols=currencies)
|
||||
|
||||
def get_rates(self, **kwargs):
|
||||
""" Returns a mapping <currency>: <rate> """
|
||||
|
||||
return {}
|
||||
|
@ -515,7 +515,7 @@ CURRENCIES = CONFIG.get(
|
||||
|
||||
BASE_CURRENCY = CONFIG.get('base_currency', 'USD')
|
||||
|
||||
EXCHANGE_BACKEND = 'InvenTree.exchange.InvenTreeManualExchangeBackend'
|
||||
EXCHANGE_BACKEND = 'InvenTree.exchange.' + CONFIG.get('exchange_backend', 'InvenTreeManualExchangeBackend')
|
||||
|
||||
# Extract email settings from the config file
|
||||
email_config = CONFIG.get('email', {})
|
||||
|
@ -163,30 +163,24 @@ def check_for_updates():
|
||||
|
||||
def update_exchange_rates():
|
||||
"""
|
||||
If an API key for fixer.io has been provided, attempt to update currency exchange rates
|
||||
Update backend rates
|
||||
"""
|
||||
|
||||
try:
|
||||
import common.models
|
||||
from .exchange import InvenTreeManualExchangeBackend, InvenTreeFixerExchangeBackend
|
||||
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()
|
||||
# Get backend
|
||||
if 'InvenTreeManualExchangeBackend' in settings.EXCHANGE_BACKEND:
|
||||
backend = InvenTreeFixerExchangeBackend()
|
||||
else:
|
||||
backend = InvenTreeManualExchangeBackend()
|
||||
|
||||
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
|
||||
|
||||
backend.update_rates(base_currency=base, symbols=currencies)
|
||||
# Update rates
|
||||
backend.update_rates()
|
||||
|
||||
|
||||
def send_email(subject, body, recipients, from_email=None):
|
||||
|
@ -62,6 +62,13 @@ currencies:
|
||||
- JPY
|
||||
- NZD
|
||||
- USD
|
||||
# Define base currency (can also be defined in the global settings)
|
||||
# base_currency: USD
|
||||
# Define exchange backend
|
||||
# Choices are:
|
||||
# - InvenTreeManualExchangeBackend
|
||||
# - InvenTreeFixerExchangeBackend
|
||||
exchange_backend: InvenTreeFixerExchangeBackend
|
||||
|
||||
# Email backend configuration
|
||||
# Ref: https://docs.djangoproject.com/en/dev/topics/email/
|
||||
|
Loading…
Reference in New Issue
Block a user