Coverage for src/backend/InvenTree/plugin/builtin/exporter/parameter_exporter.py: 37%
41 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 17:47 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 17:47 +0000
1"""Custom exporter for Parameter data."""
3from django.utils.translation import gettext_lazy as _
5from rest_framework import serializers
7from plugin import InvenTreePlugin
8from plugin.mixins import DataExportMixin
11class ParameterExportOptionsSerializer(serializers.Serializer):
12 """Custom export options for the ParameterExporter plugin."""
14 export_exclude_inactive_parameters = serializers.BooleanField(
15 default=True,
16 label=_('Exclude Inactive'),
17 help_text=_('Exclude parameters which are inactive'),
18 )
21class ParameterExporter(DataExportMixin, InvenTreePlugin):
22 """Builtin plugin for exporting Parameter data.
24 Extends the export process, to include all associated Parameter data.
25 """
27 NAME = 'Parameter Exporter'
28 SLUG = 'parameter-exporter'
29 TITLE = _('Parameter Exporter')
30 DESCRIPTION = _('Exporter for model parameter data')
31 VERSION = '2.0.0'
32 AUTHOR = _('InvenTree contributors')
34 ExportOptionsSerializer = ParameterExportOptionsSerializer
36 def supports_export(
37 self,
38 model_class: type,
39 user=None,
40 serializer_class=None,
41 view_class=None,
42 *args,
43 **kwargs,
44 ) -> bool:
45 """Supported if the base model implements the InvenTreeParameterMixin."""
46 from InvenTree.models import InvenTreeParameterMixin
48 return issubclass(model_class, InvenTreeParameterMixin)
50 def update_headers(self, headers, context, **kwargs):
51 """Update headers for the export."""
52 # Add in a header for each parameter
53 for pk, name in self.parameters.items():
54 headers[f'parameter_{pk}'] = str(name)
56 return headers
58 def prefetch_queryset(self, queryset):
59 """Ensure that the associated parameters are prefetched."""
60 from InvenTree.models import InvenTreeParameterMixin
62 queryset = InvenTreeParameterMixin.annotate_parameters(queryset)
63 return queryset
65 def export_data(
66 self, queryset, serializer_class, headers, context, output, **kwargs
67 ):
68 """Export parameter data."""
69 # Extract custom serializer options and cache
70 queryset = self.prefetch_queryset(queryset)
71 self.serializer_class = serializer_class
73 self.exclude_inactive = context.get('export_exclude_inactive_parameters', True)
75 # Keep a dict of observed parameters against their primary key
76 self.parameters = {}
78 # Serialize the queryset using DRF first
79 rows = self.serializer_class(
80 queryset, parameters=True, exporting=True, many=True
81 ).data
83 for row in rows:
84 # Extract the associated parameters from the serialized data
85 for parameter in row.get('parameters', []):
86 template_detail = parameter['template_detail']
87 template_id = template_detail['pk']
89 active = template_detail.get('enabled', True)
91 if not active and self.exclude_inactive:
92 continue
94 self.parameters[template_id] = template_detail['name']
96 row[f'parameter_{template_id}'] = parameter['data']
98 return rows