""" Project: PARTS Website Author: Edward Middleton-Smith Precision And Research Technology Systems Limited Technology: Business Objects Feature: Dog Command Link Container Business Object Description: Business object for dog command link container DEPRECATED AS NOT REQUIRED - USE A LIST """ # internal import lib.argument_validation as av from business_objects.base import Base from business_objects.db_base import SQLAlchemy_ABC, Get_Many_Parameters_Base from business_objects.dog.dog_command_link import Dog_Command_Link from extensions import db from helpers.helper_app import Helper_App # external from pydantic import BaseModel from typing import ClassVar class Dog_Command_Link_Container(Base): NAME_ATTR_OPTION_TEXT: ClassVar[str] = '' NAME_ATTR_OPTION_VALUE: ClassVar[str] = Base.FLAG_ROWS categories: list def __init__(self): self.categories = [] def add_dog_command_link(self, dog_command_link): av.val_instance(dog_command_link, 'dog_command_link', 'Dog_Command_Link_Container.add_dog_command_link', Dog_Command_Link) self.categories.append(dog_command_link) def get_index_category_from_id(self, id_category): for index_category in range(len(self.categories)): category = self.categories[index_category] if category.id_category == id_category: return index_category raise ValueError(f"{av.error_msg_str(id_category, 'id_category', 'Dog_Command_Link_Container.get_index_category_from_id', int)}\nID not in list") def get_index_category_from_id_permutation(self, id_permutation): for index_category in range(len(self.categories)): category = self.categories[index_category] try: index_product = category.get_index_product_from_id_permutation(id_permutation) return index_category except: pass raise ValueError(f"{av.error_msg_str(id_permutation, 'id_permutation', 'Dog_Command_Link_Container.get_index_category_from_id_permutation', int)}. Permutation ID not in list") def add_product(self, product): av.val_instance(product, 'product', 'Dog_Command_Link_Container.add_product', Product) index_category = self.get_index_category_from_id(product.id_category) self.categories[index_category].add_product(product) def add_product_permutation(self, permutation): av.val_instance(permutation, 'permutation', 'Dog_Command_Link_Container.add_product_permutation', Product_Permutation) index_category = self.get_index_category_from_id(permutation.id_category) self.categories[index_category].add_product_permutation(permutation) def add_product_variation_type(self, variation_type): av.val_instance(variation_type, 'variation_type', 'Dog_Command_Link_Container.add_product_variation_type', Product_Variation_Type) variation = variation_type.variations[0] index_category = self.get_index_category_from_id(variation.id_category) self.categories[index_category].add_product_variation_type(variation_type) def add_product_price(self, price): av.val_instance(price, 'price', 'Dog_Command_Link_Container.add_product_price', Product_Price) index_category = self.get_index_category_from_id(price.id_category) self.categories[index_category].add_product_price(price) def add_product_image(self, image): av.val_instance(image, 'image', 'Dog_Command_Link_Container.add_product_image', Image) index_category = self.get_index_category_from_id(image.id_category) self.categories[index_category].add_product_image(image) def add_delivery_option(self, delivery_option): av.val_instance(delivery_option, 'delivery_option', 'Dog_Command_Link_Container.add_delivery_option', Delivery_Option) index_category = self.get_index_category_from_id(delivery_option.id_category) self.categories[index_category].add_delivery_option(delivery_option) def add_product_price_discount(self, discount): av.val_instance(discount, 'discount', 'Dog_Command_Link_Container.add_product_price_discount', Discount) index_category = self.get_index_category_from_id(discount.id_category) self.categories[index_category].add_product_price_discount(discount) def add_stock_item(self, stock_item): av.val_instance(stock_item, 'stock_item', 'Dog_Command_Link_Container.add_stock_item', Stock_Item) index_category = self.get_index_category_from_id(stock_item.id_category) self.categories[index_category].add_stock_item(stock_item) def get_all_product_variation_trees(self): for category in self.categories: category.get_all_product_variation_trees() def __repr__(self): return f'categories: {self.categories}' def get_category_count(self): return len(self.categories) def to_permutation_row_list(self): list_rows = [] for category in self.categories: list_rows += category.to_permutation_row_list() return list_rows def to_category_option_list(self): list_categories = [] for category in self.categories: list_categories.append({'value': category.id_category, 'text': category.name}) return list_categories def get_list_products(self): list_products = [] for category in self.categories: list_products += category.products return list_products def to_product_option_list(self): list_products = self.get_list_products() return [{'value': product.id_product, 'text': product.name, Product.ATTR_ID_Dog_Command_Link: product.id_category} for product in list_products] def get_product_option_lists_by_category(self): dict_lists_products = {} for category in self.categories: dict_lists_products[category.id_category] = category.to_product_option_list() return dict_lists_products def to_json(self): return { **self.get_shared_json_attributes(self), f'{self.FLAG_ROWS}': [category.to_json() for category in self.categories] } @classmethod def from_json(cls, json): return None def to_json_option(self): return None def to_temporary_record(self): excluded_attributes = { column.name: getattr(self, column.name) for column in self.__table__.columns if column.name not in ['created_on', 'created_by'] } return self.to_object_with_missing_attributes(excluded_attributes) def get_csv_ids_permutation(self): list_ids = [] for category in self.categories: list_ids += category.get_csv_ids_permutation() return ','.join(list_ids)