2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-14 11:05:41 +00:00

Format number (#8482)

* Add extra options for 'format_number' helper

* Update documentation

* Improved typing hints and docs cleanup

* Fix link
This commit is contained in:
Oliver
2024-11-14 16:11:01 +11:00
committed by GitHub
parent ae88124294
commit da112211e5
7 changed files with 193 additions and 78 deletions

View File

@ -7,7 +7,7 @@ title: Machines
InvenTree has a builtin machine registry. There are different machine types available where each type can have different drivers. Drivers and even custom machine types can be provided by plugins.
!!! info "Requires Redis"
If the machines features is used in production setup using workers, a shared [redis cache](../../start/docker.md#redis-cache) is required to function properly.
If the machines features is used in production setup using workers, a shared [redis cache](../../start/processes.md#cache-server) is required to function properly.
### Registry

View File

@ -41,6 +41,13 @@ A number of helper functions are available for accessing data contained in a par
To return the element at a given index in a container which supports indexed access (such as a [list](https://www.w3schools.com/python/python_lists.asp)), use the `getindex` function:
::: report.templatetags.report.getindex
options:
show_docstring_description: false
show_source: False
#### Example
```html
{% raw %}
{% getindex my_list 1 as value %}
@ -53,6 +60,13 @@ Item: {{ value }}
To return an element corresponding to a certain key in a container which supports key access (such as a [dictionary](https://www.w3schools.com/python/python_dictionaries.asp)), use the `getkey` function:
::: report.templatetags.report.getkey
options:
show_docstring_description: false
show_source: False
#### Example
```html
{% raw %}
<ul>
@ -66,8 +80,17 @@ To return an element corresponding to a certain key in a container which support
## Number Formatting
### format_number
The helper function `format_number` allows for some common number formatting options. It takes a number (or a number-like string) as an input, as well as some formatting arguments. It returns a *string* containing the formatted number:
::: report.templatetags.report.format_number
options:
show_docstring_description: false
show_source: False
#### Example
```html
{% raw %}
{% load report %}
@ -82,15 +105,24 @@ The helper function `format_number` allows for some common number formatting opt
For rendering date and datetime information, the following helper functions are available:
- `format_date`: Format a date object
- `format_datetime`: Format a datetime object
### format_date
Each of these helper functions takes a date or datetime object as an input, and returns a *string* containing the formatted date or datetime. The following additional arguments are available:
::: report.templatetags.report.format_date
options:
show_docstring_description: false
show_source: False
### format_datetime
::: report.templatetags.report.format_datetime
options:
show_docstring_description: false
show_source: False
### Date Formatting
If not specified, these methods return a result which uses ISO formatting. Refer to the [datetime format codes](https://docs.python.org/3/library/datetime.html#format-codes) for more information! |
| Argument | Description |
| --- | --- |
| timezone | Specify the timezone to render the date in. If not specified, uses the InvenTree server timezone |
| format | Specify the format string to use for rendering the date. If not specified, uses ISO formatting. Refer to the [datetime format codes](https://docs.python.org/3/library/datetime.html#format-codes) for more information! |
### Example
@ -106,8 +138,18 @@ Datetime: {% format_datetime my_datetime format="%d-%m-%Y %H:%M%S" %}
## Currency Formatting
### render_currency
The helper function `render_currency` allows for simple rendering of currency data. This function can also convert the specified amount of currency into a different target currency:
::: InvenTree.helpers_model.render_currency
options:
show_docstring_description: false
show_source: False
#### Example
```html
{% raw %}
{% load report %}
@ -124,20 +166,40 @@ 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 |
| max_decimal_places | Specify the maximum number of decimal places to render |
| include_symbol | Include currency symbol in rendered value (default = True) |
## Maths Operations
Simple mathematical operators are available, as demonstrated in the example template below:
### add
::: report.templatetags.report.add
options:
show_docstring_description: false
show_source: False
### subtract
::: report.templatetags.report.subtract
options:
show_docstring_description: false
show_source: False
### multiply
::: report.templatetags.report.multiply
options:
show_docstring_description: false
show_source: False
### divide
::: report.templatetags.report.divide
options:
show_docstring_description: false
show_source: False
### Example
```html
{% raw %}
<!-- Load the report helper functions -->
@ -170,10 +232,15 @@ Total: {% multiply line.purchase_price line.quantity %}<br>
*Media files* are any files uploaded to the InvenTree server by the user. These are stored under the `/media/` directory and can be accessed for use in custom reports or labels.
### Uploaded Images
### uploaded_image
You can access an uploaded image file if you know the *path* of the image, relative to the top-level `/media/` directory. To load the image into a report, use the `{% raw %}{% uploaded_image ... %}{% endraw %}` tag:
::: report.templatetags.report.uploaded_image
options:
show_docstring_description: false
show_source: False
```html
{% raw %}
<!-- Load the report helper functions -->
@ -199,7 +266,12 @@ The `{% raw %}{% uploaded_image %}{% endraw %}` tag supports some optional param
{% endraw %}```
### SVG Images
### encode_svg_image
::: report.templatetags.report.encode_svg_image
options:
show_docstring_description: false
show_source: False
SVG images need to be handled in a slightly different manner. When embedding an uploaded SVG image, use the `{% raw %}{% encode_svg_image ... %}{% endraw %}` tag:
@ -211,10 +283,15 @@ SVG images need to be handled in a slightly different manner. When embedding an
{% endraw %}
```
### Part images
### part_image
A shortcut function is provided for rendering an image associated with a Part instance. You can render the image of the part using the `{% raw %}{% part_image ... %}{% endraw %}` template tag:
::: report.templatetags.report.part_image
options:
show_docstring_description: false
show_source: False
```html
{% raw %}
<!-- Load the report helper functions -->
@ -225,7 +302,7 @@ A shortcut function is provided for rendering an image associated with a Part in
#### Image Arguments
Any optional arguments which can be used in the [uploaded_image tag](#uploaded-images) can be used here too.
Any optional arguments which can be used in the [uploaded_image tag](#uploaded_image) can be used here too.
#### Image Variations
@ -243,10 +320,15 @@ The *Part* model supports *preview* (256 x 256) and *thumbnail* (128 x 128) vers
```
### Company Images
### company_image
A shortcut function is provided for rendering an image associated with a Company instance. You can render the image of the company using the `{% raw %}{% company_image ... %}{% endraw %}` template tag:
::: report.templatetags.report.company_image
options:
show_docstring_description: false
show_source: False
```html
{% raw %}
<!-- Load the report helper functions -->
@ -326,7 +408,14 @@ You can add asset images to the reports and labels by using the `{% raw %}{% ass
## Part Parameters
If you need to load a part parameter for a particular Part, within the context of your template, you can use the `part_parameter` template tag.
If you need to load a part parameter for a particular Part, within the context of your template, you can use the `part_parameter` template tag:
::: report.templatetags.report.part_parameter
options:
show_docstring_description: false
show_source: False
### Example
The following example assumes that you have a report or label which contains a valid [Part](../part/part.md) instance:

View File

@ -177,7 +177,7 @@ Asset files can be rendered directly into the template as follows
If the requested asset name does not match the name of an uploaded asset, the template will continue without loading the image.
!!! info "Assets location"
You need to ensure your asset images to the report/assets directory in the [data directory](../start/intro.md#file-storage). Upload new assets via the [admin interface](../settings/admin.md) to ensure they are uploaded to the correct location on the server.
Upload new assets via the [admin interface](../settings/admin.md) to ensure they are uploaded to the correct location on the server.
## Report Snippets