def set_match(request, osm, osm_from, product_id_to, product_id_from): response = {} # Getting product ProductFrom = available_osms[osm_from]['product'] ProductTo = available_osms[osm]['product'] product_from = ProductFrom.objects.get(id = product_id_from) product_to = ProductTo.objects.get(id = product_id_to) if request.method == 'POST': product_to.save() product_from.save() # Clearing match match_from = get_match(product_from, osm_from) # update this if exists if match_from is not None: if osm_from == 'auchan': match_from.auchan_product = None match_from.save() if osm_from == 'ooshop': match_from.ooshop_product = None match_from.save() if osm_from == 'monoprix': match_from.monoprix_product = None match_from.save() match_to = get_match(product_to, osm) # update/create this if match_to is None: if osm == 'auchan': match_to = ProductMatch(auchan_product = product_to) if osm == 'ooshop': match_to = ProductMatch(ooshop_product = product_to) if osm == 'monoprix': match_to = ProductMatch(monoprix_product = product_to) if osm_from == 'auchan': match_to.auchan_product = product_from match_to.save() if osm_from == 'ooshop': match_to.ooshop_product = product_from match_to.save() if osm_from == 'monoprix': match_to.monoprix_product = product_from match_to.save() # Setting same categories and tags dalliz_categories = product_to.dalliz_category.all() tags = product_to.tag.all() response['tags'] = [{'name':tag.name, 'id':tag.id} for tag in tags] response['categories'] = [{'id':x.id, 'name':(lambda i: i.parent_category.name+' / '+i.name if i.parent_category is not None else i.name)(x)} for x in dalliz_categories] set_categories_to_product(product_from, dalliz_categories,osm_from, set_match = False) set_tags_to_product(product_from, tags, osm_from, set_match = False) if request.method == 'DELETE': # Clearing match match_to = get_match(product_to, osm) # update this product = None if match_to is not None: if osm_from == 'auchan': product = match_to.auchan_product match_to.auchan_product = None match_to.save() if osm_from == 'ooshop': product = match_to.ooshop_product match_to.ooshop_product = None match_to.save() if osm_from == 'monoprix': product = match_to.monoprix_product match_to.monoprix_product = None match_to.save() if product: tags, categories = reset_product(product) response['tags'] = tags response['categories'] = categories return HttpResponse(json.dumps(response))