2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-17 20:45:44 +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:
Oliver
2023-10-05 21:19:28 +11:00
committed by GitHub
parent f5e8f27fcd
commit c7eb90347a
27 changed files with 760 additions and 405 deletions

View File

@ -95,6 +95,7 @@ Supported mixin classes are:
| [APICallMixin](./plugins/api.md) | Perform calls to external APIs |
| [AppMixin](./plugins/app.md) | Integrate additional database tables |
| [BarcodeMixin](./plugins/barcode.md) | Support custom barcode actions |
| [CurrencyExchangeMixin](./plugins/currency.md) | Custom interfaces for currency exchange rates |
| [EventMixin](./plugins/event.md) | Respond to events |
| [LabelPrintingMixin](./plugins/label.md) | Custom label printing support |
| [LocateMixin](./plugins/locate.md) | Locate and identify stock items |

View File

@ -0,0 +1,45 @@
---
title: Currency Exchange Mixin
---
## CurrencyExchangeMixin
The `CurrencyExchangeMixin` class enabled plugins to provide custom backends for updating currency exchange rate information.
Any implementing classes must provide the `update_exchange_rates` method. A simple example is shown below (with fake data).
```python
from plugin import InvenTreePlugin
from plugin.mixins import CurrencyExchangeMixin
class MyFirstCurrencyExchangePlugin(CurrencyExchangeMixin, InvenTreePlugin):
"""Sample currency exchange plugin"""
...
def update_exchange_rates(self, base_currency: str, symbols: list[str]) -> dict:
"""Update currency exchange rates.
This method *must* be implemented by the plugin class.
Arguments:
base_currency: The base currency to use for exchange rates
symbols: A list of currency symbols to retrieve exchange rates for
Returns:
A dictionary of exchange rates, or None if the update failed
Raises:
Can raise any exception if the update fails
"""
rates = {
'base_currency': 1.00
}
for sym in symbols:
rates[sym] = random.randrange(5, 15) * 0.1
return rates
```

View File

@ -31,7 +31,7 @@ from report.models import PurchaseOrderReport
class SampleReportPlugin(ReportMixin, InvenTreePlugin):
"""Sample plugin which provides extra context data to a report"""
NAME = "Report Plugin"
NAME = "Sample Report Plugin"
SLUG = "reportexample"
TITLE = "Sample Report Plugin"
DESCRIPTION = "A sample plugin which provides extra context data to a report"