Coverage for src/backend/InvenTree/plugin/base/supplier/helpers.py: 67%

29 statements  

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

1"""Dataclasses for supplier plugins.""" 

2 

3from dataclasses import dataclass 

4from typing import Optional 

5 

6import common.models 

7import part.models as part_models 

8 

9 

10@dataclass 

11class Supplier: 

12 """Data class to represent a supplier. 

13 

14 Note that one plugin can connect to multiple suppliers this way with e.g. different credentials. 

15 

16 Attributes: 

17 slug (str): A unique identifier for the supplier. 

18 name (str): The human-readable name of the supplier. 

19 """ 

20 

21 slug: str 

22 name: str 

23 

24 

25@dataclass 

26class SearchResult: 

27 """Data class to represent a search result from a supplier. 

28 

29 Attributes: 

30 sku (str): The stock keeping unit identifier for the part. 

31 name (str): The name of the part. 

32 exact (bool): Indicates if the search result is an exact match. 

33 description (Optional[str]): A brief description of the part. 

34 price (Optional[str]): The price of the part as a string. 

35 link (Optional[str]): A URL link to the part on the supplier's website. 

36 image_url (Optional[str]): A URL to an image of the part. 

37 id (Optional[str]): An optional identifier for the part (part_id), defaults to sku if not provided 

38 existing_part (Optional[part_models.Part]): An existing part in the system that matches this search result, if any. 

39 """ 

40 

41 sku: str 

42 name: str 

43 exact: bool 

44 description: Optional[str] = None 

45 price: Optional[str] = None 

46 link: Optional[str] = None 

47 image_url: Optional[str] = None 

48 id: Optional[str] = None 

49 existing_part: Optional[part_models.Part] = None 

50 

51 def __post_init__(self): 

52 """Post-initialization to set the ID if not provided.""" 

53 if not self.id: 

54 self.id = self.sku 

55 

56 

57@dataclass 

58class ImportParameter: 

59 """Data class to represent a parameter for a part during import. 

60 

61 Attributes: 

62 name (str): The name of the parameter. 

63 value (str): The value of the parameter. 

64 on_category (Optional[bool]): Indicates if the parameter is associated with a category. This will be automatically set by InvenTree 

65 parameter_template (Optional[ParameterTemplate]): The associated parameter template, if any. 

66 """ 

67 

68 name: str 

69 value: str 

70 on_category: Optional[bool] = False 

71 parameter_template: Optional[common.models.ParameterTemplate] = None 

72 

73 def __post_init__(self): 

74 """Post-initialization to fetch the parameter template if not provided.""" 

75 if not self.parameter_template: 

76 try: 

77 self.parameter_template = common.models.ParameterTemplate.objects.get( 

78 name__iexact=self.name 

79 ) 

80 except common.models.ParameterTemplate.DoesNotExist: 

81 pass 

82 

83 

84class PartNotFoundError(Exception): 

85 """Exception raised when a part is not found during import.""" 

86 

87 

88class PartImportError(Exception): 

89 """Exception raised when an error occurs during part import."""