Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import abc
- from collections import Counter
- from typing import List, TypedDict
- from backend_api.models.product import AreaName
- class ProductConfigurationTypeHint(TypedDict):
- """
- Type hint for product_configuration (argument in Configurator constructor)
- """
- area: int
- material_group: int
- material: int
- material_color: int
- class AbstractBaseConfigurator(abc.ABC):
- def __init__(
- self,
- user,
- product,
- product_currency,
- product_part_type,
- product_configuration: List[ProductConfigurationTypeHint],
- **kwargs
- ):
- self.user = user
- self.product = product
- self.product_currency = product_currency
- self.product_part_type = product_part_type # On admin site it calls Area
- self.product_configuration = product_configuration
- self.areas = AreaName.objects.filter(materialareaname__price__product_part__product=self.product)
- self.producer_discount = self.product.producer.producer_discounts.get(user=self.user)
- self.coefficient = self.producer_discount.coefficient
- self.delivery_price = self.producer_discount.delivery_price
- self.final_price = 0 # or None
- def validate_areas(self):
- """
- Each product have 'area' (AreaName instance, e.g. frame, seat, backrest), you must specify in admin panel.
- This method checks do the received 'areas' is equivalent to the expected 'areas' (described the line above).
- """
- expected_areas = self.areas.values_list('id', flat=True)
- received_areas = [sub_area.get('area') for sub_area in self.product_configuration]
- if Counter(expected_areas) != Counter(received_areas):
- raise ValueError(
- 'Not enough areas in the product_configuration!'
- f'Expected sub-areas: {expected_areas}. Received areas: {received_areas}.'
- )
- def validate_materials(self):
- for area_configuration in self.product_configuration:
- pass
- def get_combinations(self):
- raise NotImplemented
- def validate_configuration(self):
- self.validate_areas()
- def create_specification(self):
- raise NotImplemented
- def save_configuration(self):
- self.validate_configuration()
- self.create_specification()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement