def process_annotation( self, insight: ProductInsight, data: Optional[Dict] = None, auth: Optional[OFFAuthentication] = None, ) -> AnnotationResult: barcode = insight.barcode lang = insight.data["lang"] field_name = "ingredients_text_{}".format(lang) product = get_product(barcode, [field_name]) if product is None: return MISSING_PRODUCT_RESULT original_ingredients = insight.data["text"] corrected = insight.data["corrected"] expected_ingredients = product.get(field_name) if expected_ingredients != original_ingredients: logger.warning("ingredients have changed since spellcheck insight " "creation (product {})".format(barcode)) return AnnotationResult( status=AnnotationStatus.error_updated_product.name, description= "the ingredient list has been updated since spellcheck", ) save_ingredients( barcode, corrected, lang=lang, insight_id=insight.id, auth=auth, ) return UPDATED_ANNOTATION_RESULT
def update_product( self, insight: ProductInsight, session_cookie: Optional[str] = None) -> AnnotationResult: if not product_exists(insight.barcode): return MISSING_PRODUCT_RESULT barcode = insight.barcode try: product_ingredient: ProductIngredient = ( ProductIngredient.select().where( ProductIngredient.barcode == barcode).get()) except ProductIngredient.DoesNotExist: logger.warning("Missing product ingredient for product " "{}".format(barcode)) return AnnotationResult(status="error_no_matching_ingredient", description="no ingredient is associated " "with insight (internal error)") ingredient_str = product_ingredient.ingredients product = get_product(barcode, fields=["ingredients_text"]) if product is None: logger.warning("Missing product: {}".format(barcode)) return MISSING_PRODUCT_RESULT expected_ingredients = product.get("ingredients_text") if expected_ingredients != ingredient_str: logger.warning("ingredients have changed since spellcheck insight " "creation (product {})".format(barcode)) return AnnotationResult( status=AnnotationStatus.error_updated_product.name, description="the ingredient list has been " "updated since spellcheck") full_correction = self.generate_full_correction( ingredient_str, insight.data['start_offset'], insight.data['end_offset'], insight.data['correction']) save_ingredients(barcode, full_correction, insight_id=insight.id, session_cookie=session_cookie) self.update_related_insights(insight) product_ingredient.ingredients = full_correction product_ingredient.save() return UPDATED_ANNOTATION_RESULT
def correct_ingredient( country: str, ingredient: str, pattern: str, correction: str, comment: str, auth: OFFAuthentication, dry_run: bool = False, ): if dry_run: print("*** Dry run ***") ingredient_field = "ingredients_text_{}".format(country) products = list(iter_products(country, ingredient)) print("{} products".format(len(products))) re_patterns = get_patterns(pattern, correction) for product in products: barcode = product.get("code") print( "Fixing {}/product/{}".format( BaseURLProvider().country(country).get(), barcode ) ) product = get_product(barcode, fields=[ingredient_field]) if product is None: print("Product not found: {}".format(barcode)) continue ingredients = product[ingredient_field] corrected = generate_correction(ingredients, re_patterns) if ingredients == corrected: print("No modification after correction, skipping") continue else: print(ingredients) print(corrected) print("-" * 15) if not dry_run: save_ingredients( barcode, corrected, lang=country, comment=comment, auth=auth )