mirror of
				https://github.com/inventree/InvenTree.git
				synced 2025-10-31 13:15:43 +00:00 
			
		
		
		
	Extend ReportMixin class to support labels (#4678)
* Extend ReportMixin context to shim label templates also * Update docs
This commit is contained in:
		| @@ -18,6 +18,7 @@ import part.models | |||||||
| import stock.models | import stock.models | ||||||
| from InvenTree.helpers import normalize, validateFilterString | from InvenTree.helpers import normalize, validateFilterString | ||||||
| from InvenTree.models import MetadataMixin | from InvenTree.models import MetadataMixin | ||||||
|  | from plugin.registry import registry | ||||||
|  |  | ||||||
| try: | try: | ||||||
|     from django_weasyprint import WeasyTemplateResponseMixin |     from django_weasyprint import WeasyTemplateResponseMixin | ||||||
| @@ -190,6 +191,13 @@ class LabelTemplate(MetadataMixin, models.Model): | |||||||
|         context['width'] = self.width |         context['width'] = self.width | ||||||
|         context['height'] = self.height |         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 |         return context | ||||||
|  |  | ||||||
|     def render_as_string(self, request, **kwargs): |     def render_as_string(self, request, **kwargs): | ||||||
|   | |||||||
| @@ -34,3 +34,16 @@ class ReportMixin: | |||||||
|             context: The context dictionary to add to |             context: The context dictionary to add to | ||||||
|         """ |         """ | ||||||
|         pass |         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 | ||||||
|   | |||||||
| @@ -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. | 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 | ### 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): | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user