2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-06-12 10:05:39 +00:00

Report: Add date rendering (#6706)

* Validate timezone in settings.py

* Add helper functions for timezone information

- Extract server timezone
- Convert provided time to specified timezone

* Add more unit tests

* Remove debug print

* Test fix

* Add report helper tags

- format_date
- format_datetime
- Update report templates
- Unit tests

* Add setting to control report errors

- Only log errors to DB if setting is enabled

* Update example report

* Fixes for to_local_time

* Update type hinting

* Fix unit test typo
This commit is contained in:
Oliver
2024-03-14 12:09:14 +11:00
committed by GitHub
parent 7de87383b5
commit 610ea7b0b1
17 changed files with 225 additions and 17 deletions

View File

@ -40,7 +40,7 @@ margin-top: 4cm;
{% endblock %}
{% block bottom_left %}
content: "v{{report_revision}} - {{ date.isoformat }}";
content: "v{{report_revision}} - {% format_date date %}";
{% endblock %}
{% block bottom_center %}

View File

@ -186,7 +186,7 @@ margin-top: 4cm;
{% endblock %}
{% block bottom_left %}
content: "v{{report_revision}} - {{ date.isoformat }}";
content: "v{{report_revision}} - {% format_date date %}";
{% endblock %}
{% block header_content %}
@ -230,13 +230,13 @@ content: "v{{report_revision}} - {{ date.isoformat }}";
</tr>
<tr>
<th>{% trans "Issued" %}</th>
<td>{% render_date build.creation_date %}</td>
<td>{% format_date build.creation_date %}</td>
</tr>
<tr>
<th>{% trans "Target Date" %}</th>
<td>
{% if build.target_date %}
{% render_date build.target_date %}
{% format_date build.target_date %}
{% else %}
<em>Not specified</em>
{% endif %}

View File

@ -64,7 +64,7 @@ To return an element corresponding to a certain key in a container which support
{% endraw %}
```
## Formatting Numbers
## Number Formatting
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:
@ -78,7 +78,33 @@ The helper function `format_number` allows for some common number formatting opt
{% endraw %}
```
## Rendering Currency
## Date Formatting
For rendering date and datetime information, the following helper functions are available:
- `format_date`: Format a date object
- `format_datetime`: Format a datetime object
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:
| 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
A simple example of using the date formatting helper functions:
```html
{% raw %}
{% load report %}
Date: {% format_date my_date timezone="Australia/Sydney" %}
Datetime: {% format_datetime my_datetime format="%d-%m-%Y %H:%M%S" %}
{% endraw %}
```
## Currency Formatting
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: