Improve documentation on currency rendering (#4768)

* Improve documentation on currency rendering

* Fix .devcontainer file
This commit is contained in:
Oliver 2023-05-05 23:43:12 +10:00 committed by GitHub
parent 1c3d037baf
commit a0f18d82cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 2 deletions

View File

@ -17,6 +17,6 @@ inv update
inv setup-dev
# remove existing gitconfig created by "Avoiding Dubious Ownership" step
# so that it gets copyied from host to the container to have your global
# so that it gets copyied from host to the container to have your global
# git config in container
rm -f /home/vscode/.gitconfig

View File

@ -1139,7 +1139,7 @@ def notify_responsible(instance, sender, content: NotificationBody = InvenTreeNo
)
def render_currency(money, decimal_places=None, currency=None, include_symbol=True, min_decimal_places=None):
def render_currency(money, decimal_places=None, currency=None, include_symbol=True, min_decimal_places=None, max_decimal_places=None):
"""Render a currency / Money object to a formatted string (e.g. for reports)
Arguments:
@ -1148,6 +1148,7 @@ def render_currency(money, decimal_places=None, currency=None, include_symbol=Tr
currency: Optionally convert to the specified currency
include_symbol: Render with the appropriate currency symbol
min_decimal_places: The minimum number of decimal places to render to. If unspecified, uses the PRICING_DECIMAL_PLACES_MIN setting.
max_decimal_places: The maximum number of decimal places to render to. If unspecified, uses the PRICING_DECIMAL_PLACES setting.
"""
if money in [None, '']:
@ -1170,6 +1171,9 @@ def render_currency(money, decimal_places=None, currency=None, include_symbol=Tr
if min_decimal_places is None:
min_decimal_places = common.models.InvenTreeSetting.get_setting('PRICING_DECIMAL_PLACES_MIN', 0)
if max_decimal_places is None:
max_decimal_places = common.models.InvenTreeSetting.get_setting('PRICING_DECIMAL_PLACES', 6)
value = Decimal(str(money.amount)).normalize()
value = str(value)
@ -1183,6 +1187,8 @@ def render_currency(money, decimal_places=None, currency=None, include_symbol=Tr
else:
decimal_places = max(decimal_places, 2)
decimal_places = max(decimal_places, max_decimal_places)
return moneyed.localization.format_money(
money,
decimal_places=decimal_places,

View File

@ -34,6 +34,16 @@ Total Price: {% render_currency order.total_price currency='NZD' decimal_places=
{% endraw %}
```
The following keyword arguments are available to the `render_currency` function:
| Argument | Description |
| --- | --- |
| currency | Specify the currency code to render in (will attempt conversion if different to provided currency) |
| decimal_places | Specify the number of decimal places to render |
| min_decimal_places | Specify the minimum number of decimal places to render (ignored if *decimal_places* is specified) |
| max_decimal_places | Specify the maximum number of decimal places to render (ignored if *decimal_places* is specified) |
| include_symbol | Include currency symbol in rendered value (default = True) |
## Maths Operations
Simple mathematical operators are available, as demonstrated in the example template below: