def validate(self): """ Validating results from hits """ thereshold = .7 hits = Task.objects.filter(processed=False) print 'Treating %d hits' % (len(hits)) for hit in hits: results = list(hit.resulttask_set.filter(reference__isnull=False)) print 'Working on %d results' % (len(results)) # Getting associate product osm_from = hit.osm_from osm_to = hit.osm_to reference = hit.reference OsmFromProduct = globals()['%sProduct' % (osm_from.capitalize())] productFrom = OsmFromProduct.objects.get(reference=reference) kwargs = {osm_from + '_product': productFrom} product_match = ProductMatch.objects.filter(**kwargs) if len(product_match) > 0: product_match = product_match[0] else: product_match = None OsmToProduct = globals()['%sProduct' % (osm_to.capitalize())] length = len(results) values = {} # Setting thereshold for category beate et hygiene if 12 in [ c.parent_category.parent_category.id for c in productFrom.dalliz_category.all() ]: thereshold = .9 else: thereshold = .7 for r in results: value = r.reference if value in values: values[value] = values[value] + 1.0 / length else: values[value] = 1.0 / length sorted_values = sorted(values, key=lambda x: -values[x]) if len(sorted_values) > 0: max_value = values[sorted_values[0]] if max_value >= thereshold and sorted_values[0] is not None: print 'Thereshold !' process_validation = False reference_match = sorted_values[0] if reference_match == '0' or reference_match == 0: # No match if product_match is None: product_match = ProductMatch(**kwargs) setattr(product_match, osm_to + '_product', None) product_match.save() process_validation = True else: productTo = OsmToProduct.objects.filter( reference=reference_match) if len(productTo) > 0: productTo = productTo[0] if product_match is None: kwargs_bis = {osm_to + '_product': productTo} product_match = ProductMatch.objects.filter( **kwargs_bis) if len(product_match) > 0: product_match = product_match[0] else: product_match = ProductMatch(**kwargs) else: kwargs_bis = {osm_to + '_product': productTo} product_match_bis = ProductMatch.objects.filter( **kwargs_bis) if len(product_match_bis) == 0: pass else: product_match_bis = product_match_bis[0] # Migrate product_match_bis to product_match and delete for o in osms: if o != osm_from and o != osm_to: o_product = getattr( product_match_bis, o + '_product') setattr(product_match_bis, o + '_product', None) product_match_bis.save() setattr(product_match, o + '_product', o_product) product_match_bis.delete() setattr(product_match, osm_to + '_product', productTo) product_match.save() process_validation = True else: print 'Product from Osm_to not found ... reference = % %s' % reference_match if process_validation: for r in results: try: self.mtc.approve_assignment(r.assignementId) except Exception, e: print e hit.processed = True hit.save() try: self.mtc.disable_hit(hit.hitId) except Exception, e: print e # Getting Product else: # For the moment, if threshold not ok, approve everything for r in results: try: self.mtc.approve_assignment(r.assignementId) except Exception, e: print e hit.processed = True hit.save() try: self.mtc.disable_hit(hit.hitId) except Exception, e: print e
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))
def validate(self): """ Validating results from hits """ thereshold = .7 hits = Task.objects.filter(processed = False); print 'Treating %d hits'%(len(hits)) for hit in hits: results = list(hit.resulttask_set.filter(reference__isnull = False)) print 'Working on %d results'%(len(results)) # Getting associate product osm_from = hit.osm_from osm_to = hit.osm_to reference = hit.reference OsmFromProduct = globals()['%sProduct'%(osm_from.capitalize())] productFrom = OsmFromProduct.objects.get(reference = reference) kwargs = { osm_from+'_product': productFrom } product_match = ProductMatch.objects.filter(**kwargs) if len(product_match) >0: product_match = product_match[0] else: product_match = None OsmToProduct = globals()['%sProduct'%(osm_to.capitalize())] length = len(results) values = {} # Setting thereshold for category beate et hygiene if 12 in [c.parent_category.parent_category.id for c in productFrom.dalliz_category.all()]: thereshold = .9 else: thereshold = .7 for r in results: value = r.reference if value in values: values[value] = values[value] + 1.0/length else: values[value] = 1.0/length sorted_values = sorted(values, key = lambda x : -values[x]) if len(sorted_values)>0: max_value = values[sorted_values[0]] if max_value>=thereshold and sorted_values[0] is not None: print 'Thereshold !' process_validation = False reference_match = sorted_values[0] if reference_match == '0' or reference_match == 0: # No match if product_match is None: product_match = ProductMatch(**kwargs) setattr(product_match, osm_to+'_product', None) product_match.save() process_validation = True else: productTo = OsmToProduct.objects.filter(reference = reference_match) if len(productTo) > 0: productTo = productTo[0] if product_match is None: kwargs_bis = { osm_to+'_product': productTo } product_match = ProductMatch.objects.filter(**kwargs_bis) if len(product_match)>0: product_match = product_match[0] else: product_match = ProductMatch(**kwargs) else: kwargs_bis = { osm_to+'_product': productTo } product_match_bis = ProductMatch.objects.filter(**kwargs_bis) if len(product_match_bis) == 0: pass else: product_match_bis = product_match_bis[0] # Migrate product_match_bis to product_match and delete for o in osms: if o != osm_from and o != osm_to: o_product = getattr(product_match_bis, o+'_product') setattr(product_match_bis, o+'_product', None) product_match_bis.save() setattr(product_match, o+'_product', o_product) product_match_bis.delete() setattr(product_match, osm_to+'_product', productTo) product_match.save() process_validation = True else: print 'Product from Osm_to not found ... reference = % %s'%reference_match if process_validation: for r in results: try: self.mtc.approve_assignment(r.assignementId) except Exception, e: print e hit.processed = True hit.save() try: self.mtc.disable_hit(hit.hitId) except Exception, e: print e # Getting Product else: # For the moment, if threshold not ok, approve everything for r in results: try: self.mtc.approve_assignment(r.assignementId) except Exception, e: print e hit.processed = True hit.save() try: self.mtc.disable_hit(hit.hitId) except Exception, e: print e