55
66from commercetools import schemas , types
77from commercetools .testing .abstract import BaseModel , ServiceBackend
8- from commercetools .testing .utils import custom_fields_from_draft
8+ from commercetools .testing .utils import custom_fields_from_draft , get_product_variants
99
1010
1111class ProductsModel (BaseModel ):
@@ -149,8 +149,8 @@ def _create_price_from_draft(
149149 )
150150
151151
152- def _get_target_obj (obj : dict , action : types .ProductUpdateAction ):
153- staged = getattr (action , "staged" , False )
152+ def _get_target_obj (obj : dict , action : types .ProductUpdateAction , default_staged = False ):
153+ staged = getattr (action , "staged" , default_staged )
154154 if not staged and obj ["masterData" ]["current" ]:
155155 return obj ["masterData" ]["current" ]
156156 return obj ["masterData" ]["staged" ]
@@ -227,6 +227,54 @@ def updater(self, obj: dict, action: types.ProductAddVariantAction):
227227 return updater
228228
229229
230+ def _publish_product_action ():
231+ def updater (self , obj : dict , action : types .ProductPublishAction ):
232+ new = copy .deepcopy (obj )
233+ # not implemented scopes right now.
234+ if "staged" in new ["masterData" ]:
235+ new ["masterData" ]["current" ] = new ["masterData" ]["staged" ]
236+ new ["masterData" ]["hasStagedChanges" ] = False
237+ new ["masterData" ]["published" ] = True
238+ return new
239+
240+ return updater
241+
242+
243+ def _set_product_prices ():
244+ def updater (self , obj : dict , action : types .ProductSetPricesAction ):
245+ new = copy .deepcopy (obj )
246+ target_obj = _get_target_obj (new , action , default_staged = True )
247+ prices = []
248+ for price_draft in action .prices :
249+ price = types .Price (
250+ id = str (uuid .uuid4 ()),
251+ country = price_draft .country ,
252+ channel = price_draft .channel ,
253+ value = types .TypedMoney (
254+ fraction_digits = 2 ,
255+ cent_amount = price_draft .value .cent_amount ,
256+ currency_code = price_draft .value .currency_code ,
257+ type = types .MoneyType .CENT_PRECISION ,
258+ ),
259+ valid_from = price_draft .valid_from ,
260+ valid_until = price_draft .valid_until ,
261+ discounted = price_draft .discounted ,
262+ custom = price_draft .custom ,
263+ tiers = price_draft .tiers ,
264+ )
265+ prices .append (price )
266+
267+ schema = schemas .PriceSchema ()
268+ for variant in get_product_variants (target_obj ):
269+ if variant ["sku" ] == action .sku :
270+ variant ["prices" ] = schema .dump (prices , many = True )
271+ if action .staged :
272+ new ["masterData" ]["hasStagedChanges" ] = True
273+ return new
274+
275+ return updater
276+
277+
230278class ProductsBackend (ServiceBackend ):
231279 service_path = "products"
232280 model_class = ProductsModel
@@ -251,4 +299,6 @@ def urls(self):
251299 "changeSlug" : _update_productdata_attr ("slug" , "slug" ),
252300 "setAttribute" : _set_attribute_action (),
253301 "addVariant" : _add_variant_action (),
302+ "setPrices" : _set_product_prices (),
303+ "publish" : _publish_product_action (),
254304 }
0 commit comments