From ad545bad24df3cd575d19010d388b418b833e9ee Mon Sep 17 00:00:00 2001 From: Oliver Date: Fri, 21 Apr 2023 15:28:31 +1000 Subject: [PATCH] Update to report plugin API (#4649) explicitly add the model instance when allowing plugins to add context data --- InvenTree/plugin/base/integration/ReportMixin.py | 5 +++-- InvenTree/plugin/samples/integration/report_plugin_sample.py | 4 ++-- InvenTree/report/models.py | 3 ++- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/InvenTree/plugin/base/integration/ReportMixin.py b/InvenTree/plugin/base/integration/ReportMixin.py index 10ea3906cf..195d48401e 100644 --- a/InvenTree/plugin/base/integration/ReportMixin.py +++ b/InvenTree/plugin/base/integration/ReportMixin.py @@ -22,13 +22,14 @@ class ReportMixin: super().__init__() self.add_mixin('report', True, __class__) - def add_report_context(self, instance, request, context): + 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: - instance: The report instance to add context to + 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 """ diff --git a/InvenTree/plugin/samples/integration/report_plugin_sample.py b/InvenTree/plugin/samples/integration/report_plugin_sample.py index f5433a7a39..c78346b89c 100644 --- a/InvenTree/plugin/samples/integration/report_plugin_sample.py +++ b/InvenTree/plugin/samples/integration/report_plugin_sample.py @@ -20,7 +20,7 @@ class SampleReportPlugin(ReportMixin, InvenTreePlugin): """Some custom function which is not required for the plugin to function""" 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""" # We can add any extra context data we want to the report @@ -32,7 +32,7 @@ class SampleReportPlugin(ReportMixin, InvenTreePlugin): context['random_int'] = self.some_custom_function() # 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 context['request_method'] = request.method diff --git a/InvenTree/report/models.py b/InvenTree/report/models.py index 5baf5be4c6..25127b6747 100644 --- a/InvenTree/report/models.py +++ b/InvenTree/report/models.py @@ -217,7 +217,8 @@ class ReportTemplateBase(MetadataMixin, ReportBase): plugins = registry.with_mixin('report') for plugin in plugins: - plugin.add_report_context(self, request, context) + # Let each plugin add its own context data + plugin.add_report_context(self, self.object_to_print, request, context) return context