def to_representation(self, product): combination_result = [] if product.mode == ProductMode.VARIABLE_VARIATION_PARENT: for combination in get_all_available_combinations(product): combination_result.append({ "product": combination["result_product_pk"], "sku_part": combination["sku_part"], "hash": combination["hash"], "combination": { force_text(k): force_text(v) for k, v in six.iteritems(combination["variable_to_value"]) } }) return {"combinations": combination_result}
def to_representation(self, product): combination_result = [] if product.mode == ProductMode.VARIABLE_VARIATION_PARENT: for combination in get_all_available_combinations(product): combination_result.append({ "product": combination["result_product_pk"], "sku_part": combination["sku_part"], "hash": combination["hash"], "combination": { force_text(k): force_text(v) for k, v in six.iteritems(combination["variable_to_value"]) } }) return {"combinations": combination_result}
def test_product_variable_variation_link(admin_user): activate("en") client = _get_client(admin_user) shop = get_default_shop() product1 = create_product("product1", shop=shop) product2 = create_product("product2", shop=shop) product3 = create_product("product3", shop=shop) assert product1.mode == ProductMode.NORMAL var = ProductVariationVariable.objects.create(name="Complex Variable", product=product1) ProductVariationVariableValue.objects.create(variable=var, value="Value A") ProductVariationVariableValue.objects.create(variable=var, value="Value B") product1.verify_mode() product1.save() # create the combinations combinations = list(get_all_available_combinations(product1)) hash1 = combinations[0]["hash"] # this must be Complex Variable: Value A hash2 = combinations[1]["hash"] # this must be Complex Variable: Value B # link product1 <- product2 linkage_data = { "product": product2.pk, "hash": hash1, "status": ProductVariationLinkStatus.VISIBLE.value } response = client.post( "/api/shuup/product/%d/variable_variation/" % product1.pk, content_type="application/json", data=json.dumps(linkage_data), ) assert response.status_code == status.HTTP_201_CREATED # link product1 <- product3 linkage_data = { "product": product3.pk, "hash": hash2, "status": ProductVariationLinkStatus.VISIBLE.value } response = client.post( "/api/shuup/product/%d/variable_variation/" % product1.pk, content_type="application/json", data=json.dumps(linkage_data), ) assert response.status_code == status.HTTP_201_CREATED result1 = ProductVariationResult.objects.get(combination_hash=hash1) assert result1.product.pk == product1.pk assert result1.status.value == ProductVariationLinkStatus.VISIBLE.value assert result1.result.pk == product2.pk result2 = ProductVariationResult.objects.get(combination_hash=hash2) assert result2.product.pk == product1.pk assert result2.status.value == ProductVariationLinkStatus.VISIBLE.value assert result2.result.pk == product3.pk combinations = list(get_all_available_combinations(product1)) assert combinations[0]["result_product_pk"] == product2.pk assert combinations[1]["result_product_pk"] == product3.pk # update linkage_data = { "product": product2.pk, "hash": hash2, "status": ProductVariationLinkStatus.INVISIBLE.value } response = client.put( "/api/shuup/product/%d/variable_variation/" % product1.pk, content_type="application/json", data=json.dumps(linkage_data), ) assert response.status_code == status.HTTP_200_OK result2 = ProductVariationResult.objects.get(combination_hash=hash2) assert result2.product.pk == product1.pk assert result2.status.value == ProductVariationLinkStatus.INVISIBLE.value assert result2.result.pk == product2.pk # delete response = client.delete( "/api/shuup/product/%d/variable_variation/" % product1.pk, content_type="application/json", data=json.dumps({"hash": hash2}), ) assert response.status_code == status.HTTP_204_NO_CONTENT assert ProductVariationResult.objects.count() == 1 response = client.delete( "/api/shuup/product/%d/variable_variation/" % product1.pk, content_type="application/json", data=json.dumps({"hash": hash1}), ) assert response.status_code == status.HTTP_204_NO_CONTENT assert ProductVariationResult.objects.count() == 0 # delete something not existent response = client.delete( "/api/shuup/product/%d/variable_variation/" % product1.pk, content_type="application/json", data=json.dumps({"hash": hash1}), ) assert response.status_code == status.HTTP_404_NOT_FOUND # update not existent response = client.put( "/api/shuup/product/%d/variable_variation/" % product1.pk, content_type="application/json", data=json.dumps(linkage_data), ) assert response.status_code == status.HTTP_404_NOT_FOUND