コード例 #1
0
    def updater(
        self, obj: dict, action: types.ProductTypeChangeLocalizedEnumValueLabelAction
    ):
        new = copy.deepcopy(obj)

        def update_attribute_type(type_info: dict):
            if type_info["name"] == "lenum":
                for value in type_info["values"]:
                    if value["key"] == action.new_value.key:
                        value["label"] = action.new_value.label
                        return new

            if type_info["name"] == "set":
                return update_attribute_type(type_info["elementType"])

        for attribute in new["attributes"]:
            if attribute["name"] == action.attribute_name:
                result = update_attribute_type(attribute["type"])
                if result is not None:
                    return result

        raise InternalUpdateError(
            "No attribute found with name %r"
            " and local enum value key %r"
            % (action.attribute_name, action.new_value.key)
        )
コード例 #2
0
    def updater(self, obj: dict, action: models.ProductTypeChangeLabelAction):
        new = copy.deepcopy(obj)

        for attribute in new["attributes"]:
            if attribute["name"] == action.attribute_name:
                attribute["label"] = action.label
                return new

        raise InternalUpdateError("No attribute found with name %r" %
                                  action.attribute_name)
コード例 #3
0
def remove_shipping_rate(backend: "ShippingMethodsBackend", obj: dict,
                         action: types.ShippingMethodRemoveShippingRateAction):
    target_zone_rate = None
    for zone_rate in obj["zoneRates"]:
        if zone_rate["zone"]["id"] == action.zone.id:
            target_zone_rate = zone_rate
            break

    if not target_zone_rate:
        raise InternalUpdateError("Zone does not exist")

    rate_to_delete = create_shipping_rate_from_draft(action.shipping_rate)
    for shipping_rate in target_zone_rate["shippingRates"]:
        existing_rate = schemas.ShippingRateSchema().load(shipping_rate)
        if existing_rate == rate_to_delete:
            target_zone_rate["shippingRates"].remove(shipping_rate)
            return copy.deepcopy(obj)

    raise InternalUpdateError("Shipping rate does not exist")
コード例 #4
0
    def updater(self, obj: dict,
                action: models.ProductTypeAddAttributeDefinitionAction):
        existing = [attr["name"] for attr in obj["attributes"]]

        if action.attribute.name in existing:
            raise InternalUpdateError(
                f"Attribute with name {action.attribute.name} already exists")

        attr_json = schema.dump(_create_attribute_from_draft(action.attribute))
        obj["attributes"].append(attr_json)
        return obj
コード例 #5
0
def remove_shipping_zone(backend: "ShippingMethodsBackend", obj: dict,
                         action: types.ShippingMethodRemoveZoneAction):
    new = copy.deepcopy(obj)
    for zone_rate in new["zoneRates"]:
        if zone_rate["zone"]["id"] == action.zone.id:
            new["zoneRates"].remove(zone_rate)
            break
    else:
        raise InternalUpdateError("Zone rate not found")

    return new
コード例 #6
0
def add_shipping_zone(backend: "ShippingMethodsBackend", obj: dict,
                      action: types.ShippingMethodAddZoneAction):
    new = copy.deepcopy(obj)
    if not new.get("zoneRates"):
        new["zoneRates"] = []
    for zone_rate in new["zoneRates"]:
        if zone_rate["zone"]["id"] == action.zone.id:
            raise InternalUpdateError("Zone already exists")
    new["zoneRates"].append(schemas.ZoneRateSchema().dump(
        types.ZoneRate(zone=action.zone, )))
    return new
コード例 #7
0
def change_tax_category(backend: "ShippingMethodsBackend", obj: dict,
                        action: types.ShippingMethodChangeTaxCategoryAction):
    for tax_category in backend.model._storage._stores["tax-category"].values():
        if action.tax_category.id and tax_category["id"] == action.tax_category.id:
            break
        if action.tax_category.key and tax_category["key"] == action.tax_category.key:
            break
    else:
        raise InternalUpdateError("Tax Category does not exist")
    new = copy.deepcopy(obj)
    new["taxCategory"] = schemas.TaxCategoryResourceIdentifierSchema().dump(action.tax_category)
    return new
コード例 #8
0
def convert_identifiers_to_references(
    channel_store: Dict,
    channel_identifiers: List[types.ChannelResourceIdentifier]
) -> List[types.ChannelReference]:
    channel_references: List[types.ChannelReference] = []
    for ci in channel_identifiers:
        channel_data: Optional[Dict] = None
        for c in channel_store.values():
            if ci.key and c["key"] == ci.key:
                channel_data = c
                break
            if ci.id and c["id"] == ci.id:
                channel_data = c
                break
        if not channel_data:
            raise InternalUpdateError("Channel not found.")
        channel: types.Channel = ChannelSchema().load(data=channel_data)
        if types.ChannelRoleEnum.PRODUCT_DISTRIBUTION not in channel.roles:
            raise InternalUpdateError(
                "Channel does not have product distribution role.")
        channel_references.append(types.ChannelReference(id=channel.id))
    return channel_references
コード例 #9
0
    def updater(self, obj: dict,
                action: types.ProductTypeChangeLocalizedEnumValueLabelAction):
        new = copy.deepcopy(obj)

        for attribute in new["attributes"]:
            if attribute["name"] == action.attribute_name:
                for lenum_value in attribute["type"]["values"]:
                    if lenum_value["key"] == action.new_value.key:
                        lenum_value["label"] = action.new_value.label
                        return new

        raise InternalUpdateError(
            "No attribute found with name %r"
            " and local enum value key %r" %
            (action.attribute_name, action.new_value.key))
コード例 #10
0
    def updater(self, obj, action: types.InventoryEntryUpdateAction):
        quantity = getattr(action, "quantity")

        new = copy.deepcopy(obj)

        if isinstance(action, types.InventoryEntryAddQuantityAction):
            new["availableQuantity"] += quantity
        elif isinstance(action, types.InventoryEntryRemoveQuantityAction):
            new["availableQuantity"] -= quantity
        elif isinstance(action, types.InventoryEntryChangeQuantityAction):
            new["availableQuantity"] = quantity
        else:
            raise InternalUpdateError("Unknown action to change stock: %r", action)

        # For now we don't reserve stock
        new["quantityOnStock"] = new["availableQuantity"]
        return new