Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ManualBPO(DataStructure):
- name_structure = 'manual_bpo'
- expected_columns = [
- 'Customer ID',
- 'Location ID',
- 'Product ID',
- ]
- column_data_types = {
- 'Customer ID': {
- "type": str,
- "required": True,
- },
- 'Location ID': {
- "type": int,
- "required": True,
- },
- 'Product ID': {
- "type": int,
- "required": True,
- },
- }
- column_name = {
- 'Customer ID': 'customer_id',
- 'Location ID': 'location_id',
- 'Product ID': 'product_id',
- }
- def loader_rule(self, data_to_insert):
- instances_to_create = []
- for data in data_to_insert:
- customer_id = data.get('customer_id')
- location_id = data.get('location_id')
- product_id = data.get('product_id')
- location = Location.objects.filter(external_id=str(f'{location_id}')).first()
- costumer = Customer.objects.filter(external_id=str(f'{customer_id}')).first()
- product = Product.objects.filter(id=product_id).first()
- bpo = BPO.objects.filter(
- product=product,
- customer=costumer,
- location=location,
- ).first()
- if bpo:
- continue
- if location and costumer and product:
- instances_to_create.append(
- BPO(
- product=product,
- customer=costumer,
- location=location,
- )
- )
- BPO.objects.bulk_create(instances_to_create)
- def validate_product_id(self, col_name: str, data: pd.DataFrame) -> pd.DataFrame:
- unique_customer_ids = data[col_name].unique()
- value_exists = []
- for product_id in unique_customer_ids:
- product = Product.objects.filter(id=product_id).first()
- if not product:
- value_exists.append(product_id)
- if value_exists:
- return self.rule_validated_output_data(
- error_massage='\n'.join([
- NoElementInDatabase(
- external_id,
- col_name,
- language_code=self.language_code
- ).description for external_id in value_exists]),
- data=data,
- passed_value=False,
- )
- return self.rule_validated_output_data(
- error_massage='',
- data=data,
- passed_value=False,
- )
- def validate_location_id(self, col_name: str, data: pd.DataFrame) -> pd.DataFrame:
- unique_customer_ids = data[col_name].unique()
- value_exists = []
- for location_id in unique_customer_ids:
- location = Location.objects.filter(external_id=str(f'{location_id}')).first()
- if not location:
- value_exists.append(location_id)
- if value_exists:
- return self.rule_validated_output_data(
- error_massage='\n'.join([
- NoElementInDatabase(
- external_id,
- col_name,
- language_code=self.language_code
- ).description for external_id in value_exists]),
- data=data,
- passed_value=False,
- )
- return self.rule_validated_output_data(
- error_massage='',
- data=data,
- passed_value=False,
- )
- def validate_customer_id(self, col_name: str, data: pd.DataFrame) -> pd.DataFrame:
- unique_customer_ids = data[col_name].unique()
- value_exists = []
- for customer_id in unique_customer_ids:
- customer = Customer.objects.filter(external_id=str(f'{customer_id}')).first()
- if not customer:
- value_exists.append(customer_id)
- if value_exists:
- return self.rule_validated_output_data(
- error_massage='\n'.join([
- NoElementInDatabase(
- external_id,
- col_name,
- language_code=self.language_code
- ).description for external_id in value_exists]),
- data=data,
- passed_value=False,
- )
- return self.rule_validated_output_data(
- error_massage='',
- data=data,
- passed_value=False,
- )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement