Merge pull request #1598 from SchrodingersGat/fixer-currency-integration

Fixer currency integration
This commit is contained in:
Oliver 2021-05-18 22:59:14 +10:00 committed by GitHub
commit 6c91d067d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 1 deletions

View File

@ -44,3 +44,8 @@ class InvenTreeConfig(AppConfig):
schedule_type=Schedule.MINUTES, schedule_type=Schedule.MINUTES,
minutes=15 minutes=15
) )
InvenTree.tasks.schedule_task(
'InvenTree.tasks.update_exchange_rates',
schedule_type=Schedule.DAILY,
)

View File

@ -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' EXCHANGE_BACKEND = 'InvenTree.exchange.InvenTreeManualExchangeBackend'
# Extract email settings from the config file # Extract email settings from the config file

View 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
backend.update_rates(base_currency=base, symbols=currencies)
def send_email(subject, body, recipients, from_email=None): def send_email(subject, body, recipients, from_email=None):
""" """
Send an email with the specified subject and body, Send an email with the specified subject and body,

View File

@ -87,6 +87,12 @@ class InvenTreeSetting(models.Model):
'choices': djmoney.settings.CURRENCY_CHOICES, 'choices': djmoney.settings.CURRENCY_CHOICES,
}, },
'INVENTREE_FIXER_API_KEY': {
'name': _('fixer.io API key'),
'description': _('API key for fixer.io currency conversion service'),
'default': '',
},
'INVENTREE_DOWNLOAD_FROM_URL': { 'INVENTREE_DOWNLOAD_FROM_URL': {
'name': _('Download from URL'), 'name': _('Download from URL'),
'description': _('Allow download of remote images and files from external URL'), 'description': _('Allow download of remote images and files from external URL'),

View File

@ -20,6 +20,7 @@
{% include "InvenTree/settings/setting.html" with key="INVENTREE_BASE_URL" icon="fa-globe" %} {% include "InvenTree/settings/setting.html" with key="INVENTREE_BASE_URL" icon="fa-globe" %}
{% include "InvenTree/settings/setting.html" with key="INVENTREE_COMPANY_NAME" icon="fa-building" %} {% include "InvenTree/settings/setting.html" with key="INVENTREE_COMPANY_NAME" icon="fa-building" %}
{% include "InvenTree/settings/setting.html" with key="INVENTREE_DEFAULT_CURRENCY" icon="fa-dollar-sign" %} {% include "InvenTree/settings/setting.html" with key="INVENTREE_DEFAULT_CURRENCY" icon="fa-dollar-sign" %}
{% include "InvenTree/settings/setting.html" with key="INVENTREE_FIXER_API_KEY" icon="fa-key" %}
{% include "InvenTree/settings/setting.html" with key="INVENTREE_DOWNLOAD_FROM_URL" icon="fa-cloud-download-alt" %} {% include "InvenTree/settings/setting.html" with key="INVENTREE_DOWNLOAD_FROM_URL" icon="fa-cloud-download-alt" %}
</tbody> </tbody>
</table> </table>