def updater(self, obj: dict, action: types.ProductSetPricesAction):
        new = copy.deepcopy(obj)
        target_obj = _get_target_obj(new, getattr(action, "staged", True))
        prices = []
        for price_draft in action.prices:
            price = types.Price(
                id=str(uuid.uuid4()),
                country=price_draft.country,
                channel=price_draft.channel,
                value=types.TypedMoney(
                    fraction_digits=2,
                    cent_amount=price_draft.value.cent_amount,
                    currency_code=price_draft.value.currency_code,
                    type=types.MoneyType.CENT_PRECISION,
                ),
                valid_from=price_draft.valid_from,
                valid_until=price_draft.valid_until,
                discounted=price_draft.discounted,
                custom=price_draft.custom,
                tiers=price_draft.tiers,
            )
            prices.append(price)

        schema = schemas.PriceSchema()
        for variant in get_product_variants(target_obj):
            if variant["sku"] == action.sku:
                variant["prices"] = schema.dump(prices, many=True)
        if action.staged:
            new["masterData"]["hasStagedChanges"] = True
        return new
    def updater(self, obj: dict, action: models.ProductSetPricesAction):
        new = copy.deepcopy(obj)
        target_obj = _get_target_obj(new, getattr(action, "staged", True))
        prices = []
        for price_draft in action.prices:
            price = convert_draft_price(price_draft)
            prices.append(price)

        schema = PriceSchema()
        for variant in get_product_variants(target_obj):
            if variant["sku"] == action.sku:
                variant["prices"] = schema.dump(prices, many=True)
        if action.staged:
            new["masterData"]["hasStagedChanges"] = True
        return new
Example #3
0
    def updater(self, obj: dict, action: types.ProductChangePriceAction):
        new = copy.deepcopy(obj)
        staged = action.staged
        if staged is None:
            staged = True
        target_obj = _get_target_obj(new, staged)
        changed_price = convert_draft_price(action.price, action.price_id)
        schema = PriceSchema()

        found_price = True
        for variant in get_product_variants(target_obj):
            for index, price in enumerate(variant["prices"]):
                if price["id"] == action.price_id:
                    variant["prices"][index] = schema.dump(changed_price)
                    found_price = True
                    break
        if not found_price:
            raise ValueError("Could not find price with id %s" %
                             action.price_id)
        if staged:
            new["masterData"]["hasStagedChanges"] = True
        return new
 def updater(self, obj: Dict,
             action: models.ProductChangeMasterVariantAction):
     new = copy.deepcopy(obj)
     staged = action.staged
     if staged is None:
         staged = True
     target_obj = _get_target_obj(new, staged)
     master_variant = target_obj["masterVariant"]
     if master_variant and master_variant["sku"] == action.sku:
         return obj
     found_sku = False
     for variant in get_product_variants(target_obj):
         if variant["sku"] == action.sku:
             target_obj["variants"].append(target_obj["masterVariant"])
             target_obj["masterVariant"] = variant
             target_obj["variants"].remove(variant)
             found_sku = True
             break
     if not found_sku:
         raise ValueError("Could not find sku %s" % action.sku)
     if staged:
         new["masterData"]["hasStagedChanges"] = True
     return new
    def updater(self, obj: dict, action: models.ProductAddPriceAction):
        new = copy.deepcopy(obj)
        staged = action.staged
        if staged is None:
            staged = True
        target_obj = _get_target_obj(new, staged)
        new_price = convert_draft_price(action.price)
        schema = PriceSchema()

        found_sku = False
        for variant in get_product_variants(target_obj):
            if variant["sku"] == action.sku:
                if "prices" not in variant:
                    variant["prices"] = []
                elif not variant["prices"]:
                    variant["prices"] = []
                variant["prices"].append(schema.dump(new_price))
                found_sku = True
                break
        if not found_sku:
            raise ValueError("Could not find sku %s" % action.sku)
        if staged:
            new["masterData"]["hasStagedChanges"] = True
        return new