mirror of
https://github.com/inventree/InvenTree.git
synced 2025-07-02 11:40:58 +00:00
Currency API Updates (#4300)
* Adds an API endpoint for manually updating / refreshing currency data from the server * Update currency rates manually from the settings page * Add 'last updated' information to the currency exchange backend * Load currency exchange data via API (on setings page) * Bump API version * Table cleanup
This commit is contained in:
@ -9,7 +9,7 @@ from django.views.decorators.csrf import csrf_exempt
|
||||
|
||||
from django_filters.rest_framework import DjangoFilterBackend
|
||||
from django_q.tasks import async_task
|
||||
from djmoney.contrib.exchange.models import Rate
|
||||
from djmoney.contrib.exchange.models import ExchangeBackend, Rate
|
||||
from rest_framework import filters, permissions, serializers
|
||||
from rest_framework.exceptions import NotAcceptable, NotFound
|
||||
from rest_framework.permissions import IsAdminUser
|
||||
@ -104,10 +104,7 @@ class WebhookView(CsrfExemptMixin, APIView):
|
||||
|
||||
|
||||
class CurrencyExchangeView(APIView):
|
||||
"""API endpoint for displaying currency information
|
||||
|
||||
TODO: Add a POST hook to refresh / update the currency exchange data
|
||||
"""
|
||||
"""API endpoint for displaying currency information"""
|
||||
|
||||
permission_classes = [
|
||||
permissions.IsAuthenticated,
|
||||
@ -122,9 +119,17 @@ class CurrencyExchangeView(APIView):
|
||||
except Exception:
|
||||
rates = []
|
||||
|
||||
# Information on last update
|
||||
try:
|
||||
backend = ExchangeBackend.objects.get(name='InvenTreeExchange')
|
||||
updated = backend.last_update
|
||||
except Exception:
|
||||
updated = None
|
||||
|
||||
response = {
|
||||
'base_currency': common.models.InvenTreeSetting.get_setting('INVENTREE_DEFAULT_CURRENCY', 'USD'),
|
||||
'exchange_rates': {}
|
||||
'exchange_rates': {},
|
||||
'updated': updated,
|
||||
}
|
||||
|
||||
for rate in rates:
|
||||
@ -133,6 +138,29 @@ class CurrencyExchangeView(APIView):
|
||||
return Response(response)
|
||||
|
||||
|
||||
class CurrencyRefreshView(APIView):
|
||||
"""API endpoint for manually refreshing currency exchange rates.
|
||||
|
||||
User must be a 'staff' user to access this endpoint
|
||||
"""
|
||||
|
||||
permission_classes = [
|
||||
permissions.IsAuthenticated,
|
||||
permissions.IsAdminUser,
|
||||
]
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
"""Performing a POST request will update currency exchange rates"""
|
||||
|
||||
from InvenTree.tasks import update_exchange_rates
|
||||
|
||||
update_exchange_rates
|
||||
|
||||
return Response({
|
||||
'success': 'Exchange rates updated',
|
||||
})
|
||||
|
||||
|
||||
class SettingsList(ListAPI):
|
||||
"""Generic ListView for settings.
|
||||
|
||||
@ -452,6 +480,7 @@ common_api_urls = [
|
||||
# Currencies
|
||||
re_path(r'^currency/', include([
|
||||
re_path(r'^exchange/', CurrencyExchangeView.as_view(), name='api-currency-exchange'),
|
||||
re_path(r'^refresh/', CurrencyRefreshView.as_view(), name='api-currency-refresh'),
|
||||
])),
|
||||
|
||||
# Notifications
|
||||
|
Reference in New Issue
Block a user