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

1"""Custom validation routines for the 'importer' app.""" 

2 

3import json 

4 

5from django.core.exceptions import ValidationError 

6from django.utils.translation import gettext_lazy as _ 

7 

8# Define maximum limits for imported file data 

9IMPORTER_MAX_FILE_SIZE = 32 * 1024 * 1042 

10IMPORTER_MAX_ROWS = 5000 

11IMPORTER_MAX_COLS = 1000 

12 

13 

14def validate_data_file(data_file): 

15 """Validate the provided data file.""" 

16 import importer.operations 

17 

18 filesize = data_file.size 

19 

20 if filesize > IMPORTER_MAX_FILE_SIZE: 

21 raise ValidationError(_('Data file exceeds maximum size limit')) 

22 

23 dataset = importer.operations.load_data_file(data_file) 

24 

25 if not dataset.headers or len(dataset.headers) == 0: 

26 raise ValidationError(_('Data file contains no headers')) 

27 

28 if len(dataset.headers) > IMPORTER_MAX_COLS: 

29 raise ValidationError(_('Data file contains too many columns')) 

30 

31 if len(dataset) > IMPORTER_MAX_ROWS: 

32 raise ValidationError(_('Data file contains too many rows')) 

33 

34 

35def validate_importer_model_type(value): 

36 """Validate that the given model type is supported for importing.""" 

37 from importer.registry import supported_models 

38 

39 if value not in supported_models(): 

40 raise ValidationError(f"Unsupported model type '{value}'") 

41 

42 

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 

47 

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'))