Exemple #1
0
    def _create_from_draft(self,
                           draft: types.ShoppingListDraft,
                           id: Optional[str] = None) -> types.ShoppingList:
        object_id = str(uuid.UUID(id)) if id is not None else uuid.uuid4()

        line_items = None
        if draft.line_items:
            line_items = [
                self._create_line_item_from_draft(line_item)
                for line_item in draft.line_items
            ]

        text_line_items = None
        if draft.text_line_items:
            text_line_items = [
                self._create_line_item_text_from_draft(text_line_item)
                for text_line_item in draft.text_line_items
            ]

        return types.ShoppingList(
            id=str(object_id),
            version=1,
            custom=utils.create_from_draft(draft.custom),
            customer=draft.customer,
            delete_days_after_last_modification=draft.
            delete_days_after_last_modification,
            description=draft.description,
            key=draft.key,
            line_items=line_items,
            name=draft.name,
            slug=draft.slug,
            text_line_items=text_line_items,
            anonymous_id=draft.anonymous_id,
        )
 def _create_from_draft(
     self, draft: types.PaymentDraft, id: typing.Optional[str] = None
 ) -> types.Payment:
     object_id = str(uuid.UUID(id) if id is not None else uuid.uuid4())
     payment = types.Payment(
         id=str(object_id),
         key=draft.key,
         version=1,
         created_at=datetime.datetime.now(datetime.timezone.utc),
         last_modified_at=datetime.datetime.now(datetime.timezone.utc),
         customer=draft.customer,
         amount_authorized=draft.amount_authorized,
         amount_paid=utils._money_to_typed(draft.amount_paid),
         amount_planned=utils._money_to_typed(draft.amount_planned),
         amount_refunded=utils._money_to_typed(draft.amount_refunded),
         anonymous_id=draft.anonymous_id,
         payment_method_info=draft.payment_method_info,
         payment_status=draft.payment_status,
         transactions=[
             self._create_transaction_draft(transaction)
             for transaction in draft.transactions or []
         ],
         custom=utils.create_from_draft(draft.custom),
     )
     return payment
Exemple #3
0
    def _create_line_item_from_draft(
        self, line_item_draft: types.ShoppingListLineItemDraft
    ) -> types.ShoppingListLineItem:
        product_id = (uuid.UUID(line_item_draft.product_id)
                      if line_item_draft.product_id else None)
        product = utils.get_product_from_storage(self._storage,
                                                 product_id=product_id,
                                                 sku=line_item_draft.sku)

        if not product or not product.master_data or not product.master_data.current:
            raise NotImplementedError

        variant = product.master_data.current.master_variant
        variant_object: Optional[types.ProductVariant] = None
        if variant:
            variant_object = types.ProductVariant(id=variant.id,
                                                  sku=variant.sku)

        return types.ShoppingListLineItem(
            id=str(uuid.uuid4()),
            added_at=datetime.datetime.now(datetime.timezone.utc),
            custom=utils.create_from_draft(line_item_draft.custom),
            deactivated_at=None,
            name=product.master_data.current.name,
            product_id=line_item_draft.product_id,
            product_type=product.product_type,
            product_slug=product.master_data.current.slug,
            quantity=line_item_draft.quantity,
            variant=variant_object,
            variant_id=line_item_draft.variant_id,
        )
def convert_draft_price(price_draft: models.PriceDraft,
                        price_id: str = None) -> models.Price:
    tiers: Optional[List[models.PriceTier]] = None
    if price_draft.tiers:
        tiers = [utils.create_from_draft(tier) for tier in price_draft.tiers]
    return models.Price(
        id=price_id or str(uuid.uuid4()),
        country=price_draft.country,
        channel=price_draft.channel,
        value=utils._money_to_typed(price_draft.value),
        valid_from=price_draft.valid_from,
        valid_until=price_draft.valid_until,
        discounted=price_draft.discounted,
        custom=utils.create_from_draft(price_draft.custom),
        tiers=tiers,
    )
 def _create_line_item_from_draft(
         self, draft: types.CartDraft,
         line_item_draft: types.LineItemDraft) -> types.LineItem:
     line_id = str(uuid.uuid4())
     price = 1000
     return types.LineItem(
         id=line_id,
         name=types.LocalizedString({"en": line_id}),
         price=types.Price(value=types.CentPrecisionMoney(
             currency_code=draft.currency, cent_amount=price)),
         taxed_price=types.TaxedItemPrice(
             total_net=types.CentPrecisionMoney(
                 currency_code=draft.currency,
                 cent_amount=price * (line_item_draft.quantity or 0),
                 fraction_digits=2,
             ),
             total_gross=types.CentPrecisionMoney(
                 currency_code=draft.currency,
                 cent_amount=price * (line_item_draft.quantity or 0),
                 fraction_digits=2,
             ),
         ),
         total_price=types.CentPrecisionMoney(
             currency_code=draft.currency,
             cent_amount=price * (line_item_draft.quantity or 0),
         ),
         quantity=line_item_draft.quantity,
         price_mode=types.LineItemPriceMode.PLATFORM,
         line_item_mode=types.LineItemMode.STANDARD,
         custom=utils.create_from_draft(line_item_draft.custom),
     )
 def _create_from_draft(
         self,
         draft: types.CartDiscountDraft,
         id: typing.Optional[str] = None) -> types.CartDiscount:
     object_id = str(uuid.UUID(id) if id is not None else uuid.uuid4())
     return types.CartDiscount(
         id=str(object_id),
         version=1,
         key=draft.key,
         name=draft.name,
         description=draft.description,
         value=draft.value,
         target=draft.target,
         cart_predicate=draft.cart_predicate,
         is_active=draft.is_active or False,
         references=[],
         stacking_mode=draft.stacking_mode or types.StackingMode.STACKING,
         sort_order=draft.sort_order,
         valid_from=draft.valid_from,
         valid_until=draft.valid_until,
         requires_discount_code=draft.requires_discount_code,
         created_at=datetime.datetime.now(datetime.timezone.utc),
         last_modified_at=datetime.datetime.now(datetime.timezone.utc),
         custom=utils.create_from_draft(draft.custom),
     )
Exemple #7
0
 def _create_line_item_text_from_draft(
         self, text_line_item_draft: types.TextLineItemDraft
 ) -> types.TextLineItem:
     return types.TextLineItem(
         id=str(uuid.uuid4()),
         added_at=datetime.datetime.now(datetime.timezone.utc),
         custom=utils.create_from_draft(text_line_item_draft.custom),
         description=text_line_item_draft.description,
         name=text_line_item_draft.name,
         quantity=text_line_item_draft.quantity,
     )
Exemple #8
0
 def _create_from_draft(self,
                        draft: types.CustomerGroupDraft,
                        id: Optional[str] = None) -> types.CustomerGroup:
     object_id = uuid.UUID(id) if id is not None else uuid.uuid4()
     return types.CustomerGroup(
         id=str(object_id),
         key=draft.key,
         version=1,
         created_at=datetime.datetime.now(datetime.timezone.utc),
         name=draft.group_name,
         custom=utils.create_from_draft(draft.custom),
     )
    def _create_line_item_from_draft(
            self, draft: models.CartDraft,
            line_item_draft: models.LineItemDraft) -> models.LineItem:
        line_id = str(uuid.uuid4())
        price = 1000

        product_data = self._storage.get_by_resource_identifier(
            models.ProductResourceIdentifier(id=line_item_draft.product_id))
        product: models.Product = ProductSchema().load(product_data)

        variant = None
        for v in product.master_data.current.variants:
            if v.id == line_item_draft.variant_id:
                variant = v

        return models.LineItem(
            id=line_id,
            name=models.LocalizedString({"en": line_id}),
            product_id=line_item_draft.product_id,
            product_type=product.product_type,
            variant=variant,
            price=models.Price(
                id=str(uuid.uuid4()),
                value=models.CentPrecisionMoney(currency_code=draft.currency,
                                                cent_amount=price,
                                                fraction_digits=2),
            ),
            taxed_price=models.TaxedItemPrice(
                total_net=models.CentPrecisionMoney(
                    currency_code=draft.currency,
                    cent_amount=price * (line_item_draft.quantity or 0),
                    fraction_digits=2,
                ),
                total_gross=models.CentPrecisionMoney(
                    currency_code=draft.currency,
                    cent_amount=price * (line_item_draft.quantity or 0),
                    fraction_digits=2,
                ),
            ),
            total_price=models.CentPrecisionMoney(
                currency_code=draft.currency,
                cent_amount=price * (line_item_draft.quantity or 0),
                fraction_digits=2,
            ),
            quantity=line_item_draft.quantity,
            discounted_price_per_quantity=[],
            state=[],
            price_mode=models.LineItemPriceMode.PLATFORM,
            line_item_mode=models.LineItemMode.STANDARD,
            custom=utils.create_from_draft(line_item_draft.custom),
        )
    def _create_from_draft(
        self, draft: types.CustomerDraft, id: Optional[str] = None
    ) -> types.Customer:
        object_id = uuid.UUID(id) if id is not None else uuid.uuid4()

        return types.Customer(
            id=str(object_id),
            version=1,
            created_at=datetime.datetime.now(datetime.timezone.utc),
            last_modified_at=None,
            customer_number=draft.customer_number,
            email=draft.email,
            password=draft.password,
            first_name=draft.first_name,
            last_name=draft.last_name,
            middle_name=draft.middle_name,
            title=draft.title,
            date_of_birth=draft.date_of_birth,
            company_name=draft.company_name,
            vat_id=draft.vat_id,
            addresses=draft.addresses,
            default_shipping_address_id=(
                str(draft.default_shipping_address)
                if draft.default_shipping_address
                else None
            ),
            shipping_address_ids=(
                [str(address_id) for address_id in draft.shipping_addresses]
                if draft.shipping_addresses
                else None
            ),
            default_billing_address_id=(
                str(draft.default_billing_address)
                if draft.default_billing_address
                else None
            ),
            billing_address_ids=(
                [str(address_id) for address_id in draft.billing_addresses]
                if draft.billing_addresses
                else None
            ),
            is_email_verified=draft.is_email_verified,
            external_id=draft.external_id,
            customer_group=draft.customer_group,
            custom=utils.create_from_draft(draft.custom),
            locale=draft.locale,
            salutation=draft.salutation,
            key=draft.key,
        )
 def _create_from_draft(
     self, obj: types.ChannelDraft, id: typing.Optional[str] = None
 ) -> types.Channel:
     object_id = str(uuid.UUID(id) if id is not None else uuid.uuid4())
     return types.Channel(
         id=str(object_id),
         version=1,
         name=obj.name,
         description=obj.description,
         roles=obj.roles,
         key=obj.key,
         created_at=datetime.datetime.now(),
         last_modified_at=datetime.datetime.now(),
         custom=utils.create_from_draft(obj.custom),
     )
 def _create_from_draft(self,
                        draft: models.ChannelDraft,
                        id: typing.Optional[str] = None) -> models.Channel:
     object_id = str(uuid.UUID(id) if id is not None else uuid.uuid4())
     return models.Channel(
         id=str(object_id),
         version=1,
         name=draft.name,
         description=draft.description,
         roles=draft.roles,
         key=draft.key,
         created_at=datetime.datetime.now(datetime.timezone.utc),
         last_modified_at=datetime.datetime.now(datetime.timezone.utc),
         custom=utils.create_from_draft(draft.custom),
     )
Exemple #13
0
 def _create_from_draft(self,
                        draft: types.CategoryDraft,
                        id: typing.Optional[str] = None) -> types.Category:
     object_id = str(uuid.UUID(id) if id is not None else uuid.uuid4())
     return types.Category(
         id=str(object_id),
         version=1,
         name=draft.name,
         description=draft.description,
         slug=draft.slug,
         key=draft.key,
         created_at=datetime.datetime.now(datetime.timezone.utc),
         last_modified_at=datetime.datetime.now(datetime.timezone.utc),
         custom=utils.create_from_draft(draft.custom),
     )
Exemple #14
0
 def _create_from_draft(
     self, obj: types.InventoryEntryDraft, id: typing.Optional[str] = None
 ) -> types.InventoryEntry:
     object_id = str(uuid.UUID(id) if id is not None else uuid.uuid4())
     return types.InventoryEntry(
         id=str(object_id),
         version=1,
         created_at=datetime.datetime.now(),
         expected_delivery=obj.expected_delivery,
         last_modified_at=datetime.datetime.now(),
         quantity_on_stock=obj.quantity_on_stock,
         restockable_in_days=obj.restockable_in_days,
         sku=obj.sku,
         supply_channel=obj.supply_channel,
         custom=utils.create_from_draft(obj.custom),
     )
Exemple #15
0
 def _create_from_draft(
         self,
         draft: models.InventoryEntryDraft,
         id: typing.Optional[str] = None) -> models.InventoryEntry:
     object_id = str(uuid.UUID(id) if id is not None else uuid.uuid4())
     return models.InventoryEntry(
         id=str(object_id),
         version=1,
         created_at=datetime.datetime.now(datetime.timezone.utc),
         expected_delivery=draft.expected_delivery,
         last_modified_at=datetime.datetime.now(datetime.timezone.utc),
         quantity_on_stock=draft.quantity_on_stock,
         available_quantity=draft.quantity_on_stock,
         restockable_in_days=draft.restockable_in_days,
         sku=draft.sku,
         supply_channel=draft.supply_channel,
         custom=utils.create_from_draft(draft.custom),
     )
Exemple #16
0
    def _create_from_draft(
        self, draft: types.ReviewDraft, id: Optional[str] = None
    ) -> types.Review:
        object_id = uuid.UUID(id) if id is not None else uuid.uuid4()

        return types.Review(
            id=str(object_id),
            version=1,
            key=draft.key,
            created_at=datetime.datetime.now(datetime.timezone.utc),
            uniqueness_value=draft.uniqueness_value,
            locale=draft.locale,
            author_name=draft.author_name,
            title=draft.title,
            text=draft.text,
            target=draft.target,
            included_in_statistics=False,
            rating=draft.rating,
            state=None,
            customer=draft.customer,
            custom=utils.create_from_draft(draft.custom),
        )
Exemple #17
0
 def _create_from_draft(
     self, draft: types.DiscountCodeDraft, id: typing.Optional[str] = None
 ) -> types.DiscountCode:
     object_id = str(uuid.UUID(id) if id is not None else uuid.uuid4())
     return types.DiscountCode(
         id=str(object_id),
         version=1,
         name=draft.name,
         description=draft.description,
         code=draft.code,
         cart_discounts=draft.cart_discounts,
         cart_predicate=draft.cart_predicate,
         is_active=draft.is_active or False,
         max_applications=draft.max_applications,
         max_applications_per_customer=draft.max_applications_per_customer,
         groups=draft.groups or [],
         references=[],
         valid_from=draft.valid_from,
         valid_until=draft.valid_until,
         created_at=datetime.datetime.now(datetime.timezone.utc),
         last_modified_at=datetime.datetime.now(datetime.timezone.utc),
         custom=utils.create_from_draft(draft.custom),
     )
    def _create_from_draft(self,
                           draft: models.CartDraft,
                           id: typing.Optional[str] = None) -> models.Cart:
        object_id = str(uuid.UUID(id) if id is not None else uuid.uuid4())
        if draft.line_items:
            line_items = [
                self._create_line_item_from_draft(draft, line_item)
                for line_item in draft.line_items
            ]
        else:
            line_items = []

        total_price = None
        taxed_price = None

        if line_items:
            total_price = models.CentPrecisionMoney(
                currency_code=draft.currency,
                cent_amount=sum(line_item.taxed_price.total_gross.cent_amount
                                for line_item in line_items
                                if line_item.taxed_price
                                and line_item.taxed_price.total_gross),
                fraction_digits=2,
            )
            taxed_price = models.TaxedPrice(
                total_net=models.CentPrecisionMoney(
                    currency_code=draft.currency,
                    cent_amount=sum(line_item.taxed_price.total_net.cent_amount
                                    for line_item in line_items
                                    if line_item.taxed_price
                                    and line_item.taxed_price.total_net),
                    fraction_digits=2,
                ),
                total_gross=models.CentPrecisionMoney(
                    currency_code=draft.currency,
                    cent_amount=sum(
                        line_item.taxed_price.total_gross.cent_amount
                        for line_item in line_items if line_item.taxed_price
                        and line_item.taxed_price.total_gross),
                    fraction_digits=2,
                ),
                tax_portions=[
                    models.TaxPortion(
                        name="0% VAT",
                        rate=0,
                        amount=models.CentPrecisionMoney(
                            currency_code=draft.currency,
                            cent_amount=0,
                            fraction_digits=2,
                        ),
                    )
                ],
            )

        # Some fields such as itemShippingAddresses are currently missing. See
        # https://docs.commercetools.com/http-api-projects-carts for a complete overview
        return models.Cart(
            id=str(object_id),
            key=None,
            version=1,
            cart_state=models.CartState.ACTIVE,
            customer_id=draft.customer_id,
            customer_email=draft.customer_email,
            customer_group=draft.customer_group,
            anonymous_id=draft.anonymous_id,
            country=draft.country,
            inventory_mode=draft.inventory_mode,
            tax_mode=draft.tax_mode or models.TaxMode.PLATFORM,
            tax_rounding_mode=draft.tax_rounding_mode
            or models.RoundingMode.HALF_EVEN,
            tax_calculation_mode=draft.tax_calculation_mode
            or models.TaxCalculationMode.LINE_ITEM_LEVEL,
            line_items=line_items,
            custom_line_items=[],
            refused_gifts=[],
            shipping_address=draft.shipping_address,
            billing_address=draft.billing_address,
            locale=draft.locale,
            origin=draft.origin or models.CartOrigin.CUSTOMER,
            created_at=datetime.datetime.now(datetime.timezone.utc),
            last_modified_at=datetime.datetime.now(datetime.timezone.utc),
            custom=utils.create_from_draft(draft.custom),
            total_price=total_price,
            taxed_price=taxed_price,
        )
Exemple #19
0
    def _create_from_draft(self,
                           draft: types.CartDraft,
                           id: typing.Optional[str] = None) -> types.Cart:
        object_id = str(uuid.UUID(id) if id is not None else uuid.uuid4())
        line_items = [
            self._create_line_item_from_draft(draft, line_item)
            for line_item in draft.line_items
        ]
        total_price = None
        taxed_price = None
        if line_items:

            total_price = types.TypedMoney(
                type=types.MoneyType.CENT_PRECISION,
                currency_code=draft.currency,
                cent_amount=sum(line_item.taxed_price.total_gross.cent_amount
                                for line_item in line_items
                                if line_item.taxed_price
                                and line_item.taxed_price.total_gross),
                fraction_digits=2,
            )
            taxed_price = types.TaxedPrice(
                total_net=types.Money(
                    currency_code=draft.currency,
                    cent_amount=sum(line_item.taxed_price.total_net.cent_amount
                                    for line_item in line_items
                                    if line_item.taxed_price
                                    and line_item.taxed_price.total_net),
                ),
                total_gross=types.Money(
                    currency_code=draft.currency,
                    cent_amount=sum(
                        line_item.taxed_price.total_gross.cent_amount
                        for line_item in line_items if line_item.taxed_price
                        and line_item.taxed_price.total_gross),
                ),
                tax_portions=[
                    types.TaxPortion(
                        name="0% VAT",
                        amount=types.Money(currency_code=draft.currency,
                                           cent_amount=0),
                    )
                ],
            )

        # Some fields such as itemShippingAddresses are currently missing. See
        # https://docs.commercetools.com/http-api-projects-carts for a complete overview
        return types.Cart(
            id=str(object_id),
            version=1,
            cart_state=types.CartState.ACTIVE,
            customer_id=draft.customer_id,
            customer_email=draft.customer_email,
            customer_group=draft.customer_group,
            anonymous_id=draft.anonymous_id,
            country=draft.country,
            inventory_mode=draft.inventory_mode,
            tax_mode=draft.tax_mode,
            tax_rounding_mode=draft.tax_rounding_mode,
            tax_calculation_mode=draft.tax_calculation_mode,
            line_items=line_items,
            shipping_address=draft.shipping_address,
            billing_address=draft.billing_address,
            locale=draft.locale,
            origin=draft.origin,
            created_at=datetime.datetime.now(datetime.timezone.utc),
            last_modified_at=datetime.datetime.now(datetime.timezone.utc),
            custom=utils.create_from_draft(draft.custom),
            total_price=total_price,
            taxed_price=taxed_price,
        )