2
0
mirror of https://github.com/inventree/inventree-docs.git synced 2025-04-27 21:26:43 +00:00

Update docs for report mixin (#475)

This commit is contained in:
Oliver 2023-04-21 14:22:53 +10:00 committed by GitHub
parent 9282869fd9
commit defba77312
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,6 +6,28 @@ title: Report Mixin
The ReportMixin class provides a plugin with the ability to extend the functionality of custom [report templates](../../report/report.md). A plugin which implements the ReportMixin mixin class can add custom context data to a report template for rendering. The ReportMixin class provides a plugin with the ability to extend the functionality of custom [report templates](../../report/report.md). A plugin which implements the ReportMixin mixin class can add custom context data to a report template for rendering.
### Add Report Context
Custom context data can be provided to a report template using the `add_report_context` method:
```python
def add_report_context(self, report_instance, model_instance, request, context):
"""Add extra context to the provided report instance.
By default, this method does nothing.
Args:
report_instance: The report instance to add context to
model_instance: The model instance which initiated the report generation
request: The request object which initiated the report generation
context: The context dictionary to add to
"""
# Implemlented in the specific plugin code
...
"""
```
### Example ### Example
A sample plugin which provides additional context data to the report templates can be found [in the InvenTree source code](https://github.com/inventree/InvenTree/blob/master/InvenTree/plugin/samples/integration/report_plugin_sample.py): A sample plugin which provides additional context data to the report templates can be found [in the InvenTree source code](https://github.com/inventree/InvenTree/blob/master/InvenTree/plugin/samples/integration/report_plugin_sample.py):
@ -33,7 +55,7 @@ class SampleReportPlugin(ReportMixin, InvenTreePlugin):
"""Some custom function which is not required for the plugin to function""" """Some custom function which is not required for the plugin to function"""
return random.randint(0, 100) return random.randint(0, 100)
def add_report_context(self, instance, request, context): def add_report_context(self, report_instance, model_instance, request, context):
"""Add example content to the report instance""" """Add example content to the report instance"""
# We can add any extra context data we want to the report # We can add any extra context data we want to the report
@ -45,7 +67,7 @@ class SampleReportPlugin(ReportMixin, InvenTreePlugin):
context['random_int'] = self.some_custom_function() context['random_int'] = self.some_custom_function()
# We can also add extra data to the context which is specific to the report type # We can also add extra data to the context which is specific to the report type
context['is_purchase_order'] = isinstance(instance, PurchaseOrderReport) context['is_purchase_order'] = isinstance(report_instance, PurchaseOrderReport)
# We can also use the 'request' object to add extra context data # We can also use the 'request' object to add extra context data
context['request_method'] = request.method context['request_method'] = request.method