darkor

configurator id to db object

Jul 7th, 2022 (edited)
489
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.66 KB | None
  1. def _product_configuration_ids_to_objects(
  2.     self, product_configuration: List[InputAreaConfigurationDTO]
  3. ) -> List[AreaConfigurationDTO]:
  4.     """
  5.    Takes list of areas configurations (area_id, material_id, material_group_id, color_id).
  6.    Returns list of DTOs consist of objects (PaletteColor etc.) instead of ids.
  7.    """
  8.  
  9.     area_ids = []
  10.     material_ids = []
  11.     material_groups_ids = []
  12.     color_ids = []
  13.  
  14.     for area_configuration in product_configuration:
  15.         area_ids.append(area_configuration.area_id)
  16.         material_ids.append(area_configuration.material_id)
  17.         material_groups_ids.append(area_configuration.material_group_id)
  18.         if area_configuration.color_id:  # Color id is not required parameter
  19.             color_ids.append(area_configuration.color_id)
  20.  
  21.     # The main idea of such long code is to get objects from db not in loop but using filter()
  22.     # and 'id__in' to reduce db hits.
  23.     areas = AreaName.objects.filter(id__in=area_ids)
  24.     materials = ProductMaterialImage.objects.filter(id__in=material_ids).select_related('default_color__palette')
  25.     material_groups = ProductMaterial.objects.filter(id__in=material_groups_ids)
  26.     colors = PaletteColor.objects.filter(id__in=color_ids).select_related('palette')
  27.  
  28.     areas_dict = {area.id: area for area in areas}
  29.     materials_dict = {material.id: material for material in materials}
  30.     material_groups_dict = {material_group.id: material_group for material_group in material_groups}
  31.     colors_dict = {color.id: color for color in colors}
  32.  
  33.     configurations: List[AreaConfigurationDTO] = []
  34.  
  35.     for area_configuration in product_configuration:
  36.         area = areas_dict.get(area_configuration.area_id)
  37.         if not area:
  38.             raise ConfigurationDatabaseError('Wrong area_id.')
  39.  
  40.         material = materials_dict.get(area_configuration.material_id)
  41.         if not material:
  42.             raise ConfigurationDatabaseError('Wrong material_id.')
  43.  
  44.         material_group = material_groups_dict.get(area_configuration.material_group_id)
  45.         if not material_group:
  46.             raise ConfigurationDatabaseError('Wrong material_group_id.')
  47.  
  48.         if area_configuration.color_id:
  49.             color = colors_dict.get(area_configuration.color_id)
  50.             if not color:
  51.                 raise ConfigurationDatabaseError('Wrong color_id.')
  52.         else:
  53.             color = material.default_color
  54.  
  55.         configurations.append(
  56.             AreaConfigurationDTO(
  57.                 area=area,
  58.                 material=material,
  59.                 material_group=material_group,
  60.                 color=color,
  61.             )
  62.         )
  63.  
  64.     return configurations
RAW Paste Data Copied