Coverage for src/backend/InvenTree/importer/admin.py: 45%

36 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-07 17:47 +0000

1"""Admin site specification for the 'importer' app.""" 

2 

3from django.contrib import admin 

4 

5import importer.models 

6import importer.registry 

7 

8 

9class DataImportColumnMapAdmin(admin.TabularInline): 

10 """Inline admin for DataImportColumnMap model.""" 

11 

12 model = importer.models.DataImportColumnMap 

13 can_delete = False 

14 max_num = 0 

15 

16 def get_readonly_fields(self, request, obj=None): 

17 """Return the readonly fields for the admin interface.""" 

18 return ['field'] 

19 

20 def formfield_for_dbfield(self, db_field, request, **kwargs): 

21 """Override the choices for the column field.""" 

22 if db_field.name == 'column': 

23 # TODO: Implement this! 

24 queryset = self.get_queryset(request) 

25 

26 if queryset.count() > 0: 

27 session = queryset.first().session 

28 db_field.choices = [(col, col) for col in session.columns] 

29 

30 return super().formfield_for_choice_field(db_field, request, **kwargs) 

31 

32 

33@admin.register(importer.models.DataImportSession) 

34class DataImportSessionAdmin(admin.ModelAdmin): 

35 """Admin interface for the DataImportSession model.""" 

36 

37 list_display = ['id', 'data_file', 'status', 'user'] 

38 

39 list_filter = ['status'] 

40 

41 inlines = [DataImportColumnMapAdmin] 

42 

43 def get_readonly_fields(self, request, obj=None): 

44 """Update the readonly fields for the admin interface.""" 

45 fields = ['columns', 'status', 'timestamp'] 

46 

47 # Prevent data file from being edited after upload! 

48 if obj: 

49 fields += ['data_file'] 

50 else: 

51 fields += ['field_mapping'] 

52 

53 return fields 

54 

55 def formfield_for_dbfield(self, db_field, request, **kwargs): 

56 """Override the choices for the model_type field.""" 

57 if db_field.name == 'model_type': 

58 db_field.choices = importer.registry.supported_model_options() 

59 

60 return super().formfield_for_dbfield(db_field, request, **kwargs) 

61 

62 

63@admin.register(importer.models.DataImportRow) 

64class DataImportRowAdmin(admin.ModelAdmin): 

65 """Admin interface for the DataImportRow model.""" 

66 

67 list_display = ['id', 'session', 'row_index'] 

68 

69 def get_readonly_fields(self, request, obj=None): 

70 """Return the readonly fields for the admin interface.""" 

71 return ['session', 'row_index', 'row_data', 'errors', 'valid']