Update to report plugin API (#4649)

explicitly add the model instance when allowing plugins to add context data
This commit is contained in:
Oliver 2023-04-21 15:28:31 +10:00 committed by GitHub
parent 9198b52398
commit ad545bad24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 5 deletions

View File

@ -22,13 +22,14 @@ class ReportMixin:
super().__init__() super().__init__()
self.add_mixin('report', True, __class__) 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. """Add extra context to the provided report instance.
By default, this method does nothing. By default, this method does nothing.
Args: 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 request: The request object which initiated the report generation
context: The context dictionary to add to context: The context dictionary to add to
""" """

View File

@ -20,7 +20,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
@ -32,7 +32,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

View File

@ -217,7 +217,8 @@ class ReportTemplateBase(MetadataMixin, ReportBase):
plugins = registry.with_mixin('report') plugins = registry.with_mixin('report')
for plugin in plugins: 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 return context