Advertisement
darkor

configurtator new

May 19th, 2022
546
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import abc
  2. from collections import Counter
  3. from typing import List, TypedDict
  4.  
  5. from backend_api.models.product import AreaName
  6.  
  7.  
  8. class ProductConfigurationTypeHint(TypedDict):
  9.     """
  10.    Type hint for product_configuration (argument in Configurator constructor)
  11.    """
  12.     area: int
  13.     material_group: int
  14.     material: int
  15.     material_color: int
  16.  
  17.  
  18. class AbstractBaseConfigurator(abc.ABC):
  19.  
  20.     def __init__(
  21.         self,
  22.         user,
  23.         product,
  24.         product_currency,
  25.         product_part_type,
  26.         product_configuration: List[ProductConfigurationTypeHint],
  27.         **kwargs
  28.     ):
  29.         self.user = user
  30.         self.product = product
  31.         self.product_currency = product_currency
  32.         self.product_part_type = product_part_type  # On admin site it calls Area
  33.         self.product_configuration = product_configuration
  34.  
  35.         self.areas = AreaName.objects.filter(materialareaname__price__product_part__product=self.product)
  36.         self.producer_discount = self.product.producer.producer_discounts.get(user=self.user)
  37.         self.coefficient = self.producer_discount.coefficient
  38.         self.delivery_price = self.producer_discount.delivery_price
  39.  
  40.         self.final_price = 0  # or None
  41.  
  42.     def validate_areas(self):
  43.         """
  44.        Each product have 'area' (AreaName instance, e.g. frame, seat, backrest), you must specify in admin panel.
  45.        This method checks do the received 'areas' is equivalent to the expected 'areas' (described the line above).
  46.        """
  47.         expected_areas = self.areas.values_list('id', flat=True)
  48.         received_areas = [sub_area.get('area') for sub_area in self.product_configuration]
  49.  
  50.         if Counter(expected_areas) != Counter(received_areas):
  51.             raise ValueError(
  52.                 'Not enough areas in the product_configuration!'
  53.                 f'Expected sub-areas: {expected_areas}. Received areas: {received_areas}.'
  54.             )
  55.  
  56.     def validate_materials(self):
  57.         for area_configuration in self.product_configuration:
  58.             pass
  59.  
  60.     def get_combinations(self):
  61.         raise NotImplemented
  62.  
  63.     def validate_configuration(self):
  64.         self.validate_areas()
  65.  
  66.     def create_specification(self):
  67.         raise NotImplemented
  68.  
  69.     def save_configuration(self):
  70.         self.validate_configuration()
  71.         self.create_specification()
  72.  
Advertisement
RAW Paste Data Copied
Advertisement