Coverage for src/backend/InvenTree/importer/validators.py: 34%
30 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 validation routines for the 'importer' app."""
3import json
5from django.core.exceptions import ValidationError
6from django.utils.translation import gettext_lazy as _
8# Define maximum limits for imported file data
9IMPORTER_MAX_FILE_SIZE = 32 * 1024 * 1042
10IMPORTER_MAX_ROWS = 5000
11IMPORTER_MAX_COLS = 1000
14def validate_data_file(data_file):
15 """Validate the provided data file."""
16 import importer.operations
18 filesize = data_file.size
20 if filesize > IMPORTER_MAX_FILE_SIZE:
21 raise ValidationError(_('Data file exceeds maximum size limit'))
23 dataset = importer.operations.load_data_file(data_file)
25 if not dataset.headers or len(dataset.headers) == 0:
26 raise ValidationError(_('Data file contains no headers'))
28 if len(dataset.headers) > IMPORTER_MAX_COLS:
29 raise ValidationError(_('Data file contains too many columns'))
31 if len(dataset) > IMPORTER_MAX_ROWS:
32 raise ValidationError(_('Data file contains too many rows'))
35def validate_importer_model_type(value):
36 """Validate that the given model type is supported for importing."""
37 from importer.registry import supported_models
39 if value not in supported_models():
40 raise ValidationError(f"Unsupported model type '{value}'")
43def validate_field_defaults(value):
44 """Validate that the provided value is a valid dict."""
45 if value is None: 45 ↛ 46line 45 didn't jump to line 46 because the condition on line 45 was never true
46 return
48 if type(value) is not dict: 48 ↛ exitline 48 didn't return from function 'validate_field_defaults' because the condition on line 48 was always true
49 # OK if we can parse it as JSON
50 try:
51 value = json.loads(value)
52 except json.JSONDecodeError:
53 raise ValidationError(_('Value must be a valid dictionary object'))