Advertisement
Mochinov

Untitled

Oct 18th, 2023
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1.  
  2. class ManualBPO(DataStructure):
  3.  
  4. name_structure = 'manual_bpo'
  5.  
  6. expected_columns = [
  7. 'Customer ID',
  8. 'Location ID',
  9. 'Product ID',
  10. ]
  11.  
  12. column_data_types = {
  13. 'Customer ID': {
  14. "type": str,
  15. "required": True,
  16. },
  17. 'Location ID': {
  18. "type": int,
  19. "required": True,
  20. },
  21. 'Product ID': {
  22. "type": int,
  23. "required": True,
  24. },
  25. }
  26.  
  27. column_name = {
  28. 'Customer ID': 'customer_id',
  29. 'Location ID': 'location_id',
  30. 'Product ID': 'product_id',
  31. }
  32.  
  33. def loader_rule(self, data_to_insert):
  34. instances_to_create = []
  35.  
  36. for data in data_to_insert:
  37. customer_id = data.get('customer_id')
  38. location_id = data.get('location_id')
  39. product_id = data.get('product_id')
  40.  
  41. location = Location.objects.filter(external_id=str(f'{location_id}')).first()
  42. costumer = Customer.objects.filter(external_id=str(f'{customer_id}')).first()
  43. product = Product.objects.filter(id=product_id).first()
  44.  
  45. bpo = BPO.objects.filter(
  46. product=product,
  47. customer=costumer,
  48. location=location,
  49. ).first()
  50.  
  51. if bpo:
  52. continue
  53.  
  54. if location and costumer and product:
  55. instances_to_create.append(
  56. BPO(
  57. product=product,
  58. customer=costumer,
  59. location=location,
  60. )
  61. )
  62.  
  63. BPO.objects.bulk_create(instances_to_create)
  64.  
  65. def validate_product_id(self, col_name: str, data: pd.DataFrame) -> pd.DataFrame:
  66. unique_customer_ids = data[col_name].unique()
  67. value_exists = []
  68. for product_id in unique_customer_ids:
  69. product = Product.objects.filter(id=product_id).first()
  70. if not product:
  71. value_exists.append(product_id)
  72.  
  73. if value_exists:
  74. return self.rule_validated_output_data(
  75. error_massage='\n'.join([
  76. NoElementInDatabase(
  77. external_id,
  78. col_name,
  79. language_code=self.language_code
  80. ).description for external_id in value_exists]),
  81. data=data,
  82. passed_value=False,
  83. )
  84. return self.rule_validated_output_data(
  85. error_massage='',
  86. data=data,
  87. passed_value=False,
  88. )
  89.  
  90. def validate_location_id(self, col_name: str, data: pd.DataFrame) -> pd.DataFrame:
  91. unique_customer_ids = data[col_name].unique()
  92. value_exists = []
  93. for location_id in unique_customer_ids:
  94. location = Location.objects.filter(external_id=str(f'{location_id}')).first()
  95. if not location:
  96. value_exists.append(location_id)
  97.  
  98. if value_exists:
  99. return self.rule_validated_output_data(
  100. error_massage='\n'.join([
  101. NoElementInDatabase(
  102. external_id,
  103. col_name,
  104. language_code=self.language_code
  105. ).description for external_id in value_exists]),
  106. data=data,
  107. passed_value=False,
  108. )
  109.  
  110. return self.rule_validated_output_data(
  111. error_massage='',
  112. data=data,
  113. passed_value=False,
  114. )
  115.  
  116. def validate_customer_id(self, col_name: str, data: pd.DataFrame) -> pd.DataFrame:
  117. unique_customer_ids = data[col_name].unique()
  118. value_exists = []
  119. for customer_id in unique_customer_ids:
  120. customer = Customer.objects.filter(external_id=str(f'{customer_id}')).first()
  121. if not customer:
  122. value_exists.append(customer_id)
  123.  
  124. if value_exists:
  125. return self.rule_validated_output_data(
  126. error_massage='\n'.join([
  127. NoElementInDatabase(
  128. external_id,
  129. col_name,
  130. language_code=self.language_code
  131. ).description for external_id in value_exists]),
  132. data=data,
  133. passed_value=False,
  134. )
  135. return self.rule_validated_output_data(
  136. error_massage='',
  137. data=data,
  138. passed_value=False,
  139. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement