def _create_from_draft(self, obj: types.ProductDraft, id: typing.Optional[str] = None) -> types.Product: object_id = str(uuid.UUID(id) if id is not None else uuid.uuid4()) product = types.Product( id=str(object_id), key=obj.key, product_type=obj.product_type, version=1, created_at=datetime.datetime.now(), last_modified_at=datetime.datetime.now(), ) product_data = types.ProductData( name=obj.name, categories=obj.categories, category_order_hints=obj.category_order_hints, description=obj.description, master_variant=obj.master_variant, slug=obj.slug or types.LocalizedString(), ) if obj.publish: product.master_data = types.ProductCatalogData( staged=None, current=product_data, published=True) else: product.master_data = types.ProductCatalogData(staged=product_data, current=None, published=False) return product
def _create_from_draft(self, draft: types.ProductDraft, id: Optional[str] = None) -> types.Product: object_id = str(uuid.UUID(id) if id is not None else uuid.uuid4()) master_variant = None if draft.master_variant: master_variant = self._create_variant_from_draft( draft.master_variant) product_data = types.ProductData( name=draft.name, categories=draft.categories, category_order_hints=draft.category_order_hints, description=draft.description, master_variant=master_variant, variants=[ self._create_variant_from_draft(vd) for vd in draft.variants ] if draft.variants else [], slug=draft.slug or types.LocalizedString(), search_keywords=types.SearchKeywords(), ) if draft.publish: master_data = types.ProductCatalogData( staged=None, current=product_data, published=True, has_staged_changes=False, ) else: master_data = types.ProductCatalogData( staged=product_data, current=None, published=False, has_staged_changes=True, ) product = types.Product( id=str(object_id), key=draft.key, product_type=draft.product_type, master_data=master_data, version=1, created_at=datetime.datetime.now(datetime.timezone.utc), last_modified_at=datetime.datetime.now(datetime.timezone.utc), ) return product