mirror of
https://github.com/inventree/InvenTree.git
synced 2025-07-11 15:34:15 +00:00
Exchange rate plugin (#5667)
* Add plugin mixin class for supporting exchange rates * Split some mixin classes out into their own files - mixins.py is becoming quite bloated! * Add some new settings for controlling currency updates * Adds basic plugin implementation * Refactor existing implementation - Builtin plugin uses frankfurter.app API - Better error / edge case handlign * Add sample plugin for currency exchange * Allow user to select which plugin to use for plugin updates * Observe user-configured setting for how often exchange rates are updated * Updates for some of the sample plugins * Fix plugin slug * Add doc page * Document simple example * Improve sample * Add blank page for currency settings info * More info in "config" page * Update docs again * Updated unit tests * Fill out default settings values when InvenTree runs * Add log messages * Significant improvement in default settings speed - Use bulk create - Be efficient - Dont' be inefficient * More strict checks * Refactor default values implementation - Don't run at startup - Run on list API - Implement generic @classmethod
This commit is contained in:
53
InvenTree/plugin/builtin/integration/currency_exchange.py
Normal file
53
InvenTree/plugin/builtin/integration/currency_exchange.py
Normal file
@ -0,0 +1,53 @@
|
||||
"""Builtin plugin for requesting exchange rates from an external API."""
|
||||
|
||||
|
||||
import logging
|
||||
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from plugin import InvenTreePlugin
|
||||
from plugin.mixins import APICallMixin, CurrencyExchangeMixin
|
||||
|
||||
logger = logging.getLogger('inventree')
|
||||
|
||||
|
||||
class InvenTreeCurrencyExchange(APICallMixin, CurrencyExchangeMixin, InvenTreePlugin):
|
||||
"""Default InvenTree plugin for currency exchange rates.
|
||||
|
||||
Fetches exchange rate information from frankfurter.app
|
||||
"""
|
||||
|
||||
NAME = "InvenTreeCurrencyExchange"
|
||||
SLUG = "inventreecurrencyexchange"
|
||||
AUTHOR = _('InvenTree contributors')
|
||||
TITLE = _("InvenTree Currency Exchange")
|
||||
DESCRIPTION = _("Default currency exchange integration")
|
||||
VERSION = "1.0.0"
|
||||
|
||||
def update_exchange_rates(self, base_currency: str, symbols: list[str]) -> dict:
|
||||
"""Request exchange rate data from external API"""
|
||||
|
||||
response = self.api_call(
|
||||
'latest',
|
||||
url_args={
|
||||
'from': [base_currency],
|
||||
'to': symbols,
|
||||
},
|
||||
simple_response=False
|
||||
)
|
||||
|
||||
if response.status_code == 200:
|
||||
|
||||
rates = response.json().get('rates', {})
|
||||
rates[base_currency] = 1.00
|
||||
|
||||
return rates
|
||||
|
||||
else:
|
||||
logger.warning("Failed to update exchange rates from %s: Server returned status %s", self.api_url, response.status_code)
|
||||
return None
|
||||
|
||||
@property
|
||||
def api_url(self):
|
||||
"""Return the API URL for this plugin"""
|
||||
return 'https://api.frankfurter.app'
|
Reference in New Issue
Block a user