From 8df207d8e13c92650fd67bf57de383c21e336a86 Mon Sep 17 00:00:00 2001 From: Oliver Date: Sat, 22 Apr 2023 23:46:43 +1000 Subject: [PATCH] Extend ReportMixin class to support labels (#4678) * Extend ReportMixin context to shim label templates also * Update docs --- InvenTree/label/models.py | 8 ++++++++ InvenTree/plugin/base/integration/ReportMixin.py | 13 +++++++++++++ docs/docs/extend/plugins/report.md | 8 ++++++++ 3 files changed, 29 insertions(+) diff --git a/InvenTree/label/models.py b/InvenTree/label/models.py index 98096328c4..978aa8977b 100644 --- a/InvenTree/label/models.py +++ b/InvenTree/label/models.py @@ -18,6 +18,7 @@ import part.models import stock.models from InvenTree.helpers import normalize, validateFilterString from InvenTree.models import MetadataMixin +from plugin.registry import registry try: from django_weasyprint import WeasyTemplateResponseMixin @@ -190,6 +191,13 @@ class LabelTemplate(MetadataMixin, models.Model): context['width'] = self.width context['height'] = self.height + # Pass the context through to any registered plugins + plugins = registry.with_mixin('report') + + for plugin in plugins: + # Let each plugin add its own context data + plugin.add_label_context(self, self.object_to_print, request, context) + return context def render_as_string(self, request, **kwargs): diff --git a/InvenTree/plugin/base/integration/ReportMixin.py b/InvenTree/plugin/base/integration/ReportMixin.py index 195d48401e..e4c6339d94 100644 --- a/InvenTree/plugin/base/integration/ReportMixin.py +++ b/InvenTree/plugin/base/integration/ReportMixin.py @@ -34,3 +34,16 @@ class ReportMixin: context: The context dictionary to add to """ pass + + def add_label_context(self, label_instance, model_instance, request, context): + """Add extra context to the provided label instance. + + By default, this method does nothing. + + Args: + label_instance: The label instance to add context to + model_instance: The model instance which initiated the label generation + request: The request object which initiated the label generation + context: The context dictionary to add to + """ + pass diff --git a/docs/docs/extend/plugins/report.md b/docs/docs/extend/plugins/report.md index b9791a2ffe..6482c501bc 100644 --- a/docs/docs/extend/plugins/report.md +++ b/docs/docs/extend/plugins/report.md @@ -6,6 +6,14 @@ 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. +### Add Report Context + +A plugin which implements the ReportMixin mixin can define the `add_report_context` method, allowing custom context data to be added to a report template at time of printing. + +### Add Label Context + +Additionally the `add_label_context` method, allowing custom context data to be added to a label template at time of printing. + ### 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):