Coverage for src/backend/InvenTree/data_exporter/tasks.py: 19%
25 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"""Background tasks for the exporting app."""
3from django.contrib.auth.models import User
4from django.test.client import RequestFactory
6import structlog
8from common.models import DataOutput
10logger = structlog.get_logger('inventree')
13def export_data(
14 view_class,
15 user_id: int,
16 query_params: dict,
17 plugin_key: str,
18 export_format: str,
19 export_context: dict,
20 output_id: int,
21):
22 """Perform the data export task using the provided parameters.
24 Arguments:
25 view_class: The class of the view to export data from
26 user_id: The ID of the user who requested the export
27 query_params: Query parameters for the export
28 plugin_key: The key for the export plugin
29 export_format: The output format for the export
30 export_context: Additional options for the export
31 output_id: The ID of the DataOutput instance to write to
33 This function is designed to be called by the background task,
34 to avoid blocking the web server.
35 """
36 from plugin import registry
38 if (plugin := registry.get_plugin(plugin_key, active=True)) is None:
39 logger.warning("export_data: Plugin '%s' not found", plugin_key)
40 return
42 if (user := User.objects.filter(pk=user_id).first()) is None:
43 logger.warning('export_data: User not found: %d', user_id)
44 return
46 if (output := DataOutput.objects.filter(pk=output_id).first()) is None:
47 logger.warning('export_data: Output object not found: %d', output_id)
48 return
50 # Recreate the request object - this is required for the view to function correctly
51 # Note that the request object cannot be pickled, so we need to recreate it here
52 request = RequestFactory()
53 request.user = user
54 request.query_params = query_params
56 view = view_class()
57 view.request = request
58 view.args = getattr(view, 'args', ())
59 view.kwargs = getattr(view, 'kwargs', {})
60 view.format_kwarg = getattr(view, 'format_kwarg', None)
62 view.export_data(plugin, export_format, export_context, output)