From d5e459d8db24a7b3f5572d46a97b9467634d6c33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Gigandet?= Date: Wed, 19 Oct 2022 19:36:56 +0200 Subject: [PATCH] fix: remove old carbon footprint fields (#7541) This is to remove old carbon footprint fields (and associated computations) we implemented before the Eco-Score. Co-authored-by: Alex Garel --- cgi/product_jqm_multilingual.pl | 2 - cgi/product_multilingual.pl | 4 - lib/ProductOpener/Display.pm | 21 +--- lib/ProductOpener/Food.pm | 133 ------------------------- lib/ProductOpener/Ingredients.pm | 164 ------------------------------- scripts/update_all_products.pl | 27 ++++- 6 files changed, 28 insertions(+), 323 deletions(-) mode change 100755 => 100644 scripts/update_all_products.pl diff --git a/cgi/product_jqm_multilingual.pl b/cgi/product_jqm_multilingual.pl index 8decb28dc1998..702db75eb7997 100755 --- a/cgi/product_jqm_multilingual.pl +++ b/cgi/product_jqm_multilingual.pl @@ -416,8 +416,6 @@ extract_ingredients_from_text($product_ref); extract_ingredients_classes_from_text($product_ref); detect_allergens_from_text($product_ref); - compute_carbon_footprint_from_ingredients($product_ref); - compute_carbon_footprint_from_meat_or_fish($product_ref); # Food category rules for sweeetened/sugared beverages # French PNNS groups from categories diff --git a/cgi/product_multilingual.pl b/cgi/product_multilingual.pl index 636c5a0f348dd..0f60e87a977ae 100755 --- a/cgi/product_multilingual.pl +++ b/cgi/product_multilingual.pl @@ -289,13 +289,11 @@ () my @fields = @ProductOpener::Config::product_fields; if ($admin) { - push @fields, "environment_impact_level"; # Let admins edit any other fields if (defined single_param("fields")) { push @fields, split(/,/, single_param("fields")); } - } if (($action eq 'process') and (($type eq 'add') or ($type eq 'edit'))) { @@ -585,8 +583,6 @@ () extract_ingredients_classes_from_text($product_ref); $log->debug("detect_allergens_from_text") if $log->is_debug(); detect_allergens_from_text($product_ref); - compute_carbon_footprint_from_ingredients($product_ref); - compute_carbon_footprint_from_meat_or_fish($product_ref); # Food category rules for sweetened/sugared beverages # French PNNS groups from categories diff --git a/lib/ProductOpener/Display.pm b/lib/ProductOpener/Display.pm index b9840de00b5bb..e637b14efa54d 100644 --- a/lib/ProductOpener/Display.pm +++ b/lib/ProductOpener/Display.pm @@ -5165,15 +5165,6 @@ sub customize_response_for_product ($request_ref, $product_ref) { localize_ecoscore($cc, $product_ref); foreach my $field (split(/,/, $fields)) { - - # On demand carbon footprint tags -- deactivated: the environmental footprint infocard is now replaced by the Eco-Score details - if (0 and (not $carbon_footprint_computed) and ($field =~ /^environment_infocard/) - or ($field =~ /^environment_impact_level/)) - { - compute_carbon_footprint_infocard($product_ref); - $carbon_footprint_computed = 1; - } - if ($field eq "product_display_name") { $customized_product_ref->{$field} = remove_tags_and_quote(product_name_brand_quantity($product_ref)); } @@ -5183,8 +5174,8 @@ sub customize_response_for_product ($request_ref, $product_ref) { $customized_product_ref->{$field} = display_nutrition_table($product_ref, undef); } - # The environment infocard now displays the Eco-Score details - elsif (($field =~ /^environment_infocard/) or ($field eq "ecoscore_details_simple_html")) { + # Eco-Score details in simple HTML + elsif ($field eq "ecoscore_details_simple_html") { if ((1 or $show_ecoscore) and (defined $product_ref->{ecoscore_data})) { $customized_product_ref->{$field} = display_ecoscore_calculation_details_simple_html($cc, $product_ref->{ecoscore_data}); @@ -8648,14 +8639,6 @@ HTML $template_data_ref->{admin} = $admin; - # the carbon footprint infocard has been replaced by the Eco-Score details - if (0 and $admin) { - compute_carbon_footprint_infocard($product_ref); - $template_data_ref->{display_field_environment_infocard} = display_field($product_ref, 'environment_infocard'); - $template_data_ref->{carbon_footprint_from_meat_or_fish_debug} - = $product_ref->{"carbon_footprint_from_meat_or_fish_debug"}; - } - # Platform for producers: data quality issues and improvements opportunities if ($server_options{producers_platform}) { diff --git a/lib/ProductOpener/Food.pm b/lib/ProductOpener/Food.pm index 4db6e14937940..dab8f8756e003 100644 --- a/lib/ProductOpener/Food.pm +++ b/lib/ProductOpener/Food.pm @@ -82,7 +82,6 @@ BEGIN { &compute_unknown_nutrients &compute_nutrient_levels &compute_units_of_alcohol - &compute_carbon_footprint_infocard &compare_nutriments @@ -1727,138 +1726,6 @@ sub compute_serving_size_data ($product_ref) { return; } -sub compute_carbon_footprint_infocard ($product_ref) { - - # compute the environment impact level - # -> currently only for prepared meals - - # Limit to France, as the carbon values from ADEME are intended for France - - if ( - not( (has_tag($product_ref, "countries", "en:france")) - and (defined $product_ref->{ingredients_text}) - and (length($product_ref->{ingredients_text}) > 5)) - ) - { - - my @product_fields_to_delete = ( - "environment_impact_level", "environment_impact_level_tags", - "environment_infocard", "environment_infocard_en", - "environment_infocard_fr" - ); - - remove_fields($product_ref, \@product_fields_to_delete); - - return; - } - - if (has_tag($product_ref, "categories", "en:meals")) { - - $product_ref->{environment_impact_level} = "en:low"; - - if ( (defined $product_ref->{nutriments}) - and (defined $product_ref->{nutriments}{"carbon-footprint-from-meat-or-fish_product"})) - { - - if ($product_ref->{nutriments}{"carbon-footprint-from-meat-or-fish_product"} < 250) { - $product_ref->{environment_impact_level} = "en:medium"; - } - else { - $product_ref->{environment_impact_level} = "en:high"; - } - } - - $product_ref->{environment_impact_level_tags} = [$product_ref->{environment_impact_level}]; - } - - # compute the carbon footprint infocard when we have a carbon footprint from meat or fish - - if ( (defined $product_ref->{nutriments}) - and (defined $product_ref->{nutriments}{"carbon-footprint-from-meat-or-fish_product"})) - { - - foreach my $lang ("en", "fr") { - my $html = "

" . $Lang{carbon_impact_from_meat_or_fish}{$lang} . "

"; - - $html .= ""; - - $html .= "

"; - - $html - .= "

" - . $Lang{methodology}{$lang} . "

" . "

" - . $Lang{carbon_footprint_note_foodges_ademe}{$lang} . "

" . "

" - . $Lang{carbon_footprint_note_sustainable_annual_emissions}{$lang} . "

" . "

" - . $Lang{carbon_footprint_note_uncertainty}{$lang} . "

"; - - $product_ref->{"environment_infocard_" . $lang} = $html; - } - - # copy the main language - if (defined $product_ref->{"environment_infocard_" . $product_ref->{lc}}) { - $product_ref->{environment_infocard} = $product_ref->{"environment_infocard_" . $product_ref->{lc}}; - } - else { - $product_ref->{environment_infocard} = $product_ref->{environment_infocard_en}; - } - } - - return; -} - sub compute_unknown_nutrients ($product_ref) { $product_ref->{unknown_nutrients_tags} = []; diff --git a/lib/ProductOpener/Ingredients.pm b/lib/ProductOpener/Ingredients.pm index a4e987b4fdfb1..3bedc39c3e6ec 100644 --- a/lib/ProductOpener/Ingredients.pm +++ b/lib/ProductOpener/Ingredients.pm @@ -41,8 +41,6 @@ ingredients (is the product vegetarian, vegan, does it contain palm oil etc.) detect_allergens_from_text($product_ref); - compute_carbon_footprint_from_ingredients($product_ref); - =head1 DESCRIPTION [..] @@ -61,9 +59,6 @@ BEGIN { &separate_additive_class - &compute_carbon_footprint_from_ingredients - &compute_carbon_footprint_from_meat_or_fish - &split_generic_name_from_ingredients &clean_ingredients_text_for_lang &cut_ingredients_text_for_lang @@ -638,165 +633,6 @@ foreach my $f (readdir(DH)) { } closedir(DH); -sub compute_carbon_footprint_from_ingredients ($product_ref) { - - if (defined $product_ref->{nutriments}) { - delete $product_ref->{nutriments}{"carbon-footprint-from-known-ingredients_100g"}; - } - - remove_tag($product_ref, "misc", "en:environment-infocard"); - remove_tag($product_ref, "misc", "en:carbon-footprint-from-known-ingredients"); - - delete $product_ref->{"carbon_footprint_from_known_ingredients_debug"}; - - # Limit to France, as the carbon values from ADEME are intended for France - if ((has_tag($product_ref, "countries", "en:france")) and (defined $product_ref->{ingredients})) { - - my $carbon_footprint = 0; - my $carbon_percent = 0; - - foreach my $ingredient_ref (@{$product_ref->{ingredients}}) { - - $log->debug("carbon-footprint-from-known-ingredients_100g", {id => $ingredient_ref->{id}}) - if $log->is_debug(); - - if ((defined $ingredient_ref->{percent}) and ($ingredient_ref->{percent} > 0)) { - - $log->debug("carbon-footprint-from-known-ingredients_100g", {percent => $ingredient_ref->{percent}}) - if $log->is_debug(); - - my $carbon_footprint_ingredient = get_inherited_property('ingredients', $ingredient_ref->{id}, - "carbon_footprint_fr_foodges_value:fr"); - - if (defined $carbon_footprint_ingredient) { - $carbon_footprint += $ingredient_ref->{percent} * $carbon_footprint_ingredient; - $carbon_percent += $ingredient_ref->{percent}; - - if (not defined $product_ref->{"carbon_footprint_from_known_ingredients_debug"}) { - $product_ref->{"carbon_footprint_from_known_ingredients_debug"} = ""; - } - $product_ref->{"carbon_footprint_from_known_ingredients_debug"} - .= $ingredient_ref->{id} . " " - . $ingredient_ref->{percent} - . "% x $carbon_footprint_ingredient = " - . $ingredient_ref->{percent} * $carbon_footprint_ingredient . " g - "; - } - } - } - - if ($carbon_footprint > 0) { - $product_ref->{nutriments}{"carbon-footprint-from-known-ingredients_100g"} = $carbon_footprint; - $product_ref->{carbon_footprint_percent_of_known_ingredients} = $carbon_percent; - - defined $product_ref->{misc_tags} or $product_ref->{misc_tags} = []; - add_tag($product_ref, "misc", "en:carbon-footprint-from-known-ingredients"); - } - } - - return; -} - -sub compute_carbon_footprint_from_meat_or_fish ($product_ref) { - - if (defined $product_ref->{nutriments}) { - delete $product_ref->{nutriments}{"carbon-footprint-from-meat-or-fish"}; - delete $product_ref->{nutriments}{"carbon-footprint-from-meat-or-fish_100g"}; - delete $product_ref->{nutriments}{"carbon-footprint-from-meat-or-fish_serving"}; - delete $product_ref->{nutriments}{"carbon-footprint-from-meat-or-fish_product"}; - } - - remove_tag($product_ref, "misc", "en:carbon-footprint-from-meat-or-fish"); - - delete $product_ref->{"carbon_footprint_from_meat_or_fish_debug"}; - - # Compute the carbon footprint from meat or fish ingredients, when the percentage is known - - #ingredients: [ - #{ - #rank: 1, - #text: "Eau", - #id: "en:water" - #}, - #{ - #percent: "10.9", - #text: "_saumon_", - #rank: 2, - #id: "en:salmon" - #}, - my @parents = qw( - en:beef-meat - en:pork-meat - en:veal-meat - en:rabbit-meat - en:chicken-meat - en:turkey-meat - en:smoked-salmon - en:salmon - ); - - # Values from FoodGES - - my %carbon = ( - "en:beef-meat" => 35.8, - "en:pork-meat" => 7.4, - "en:veal-meat" => 20.5, - "en:rabbit-meat" => 8.1, - "en:chicken-meat" => 4.9, - "en:turkey-meat" => 6.5, - "en:smoked-salmon" => 5.5, - "en:salmon" => 6.5, - "en:smoked-trout" => 5.5, - "en:trout" => 6.5, - ); - - # Limit to France, as the carbon values from ADEME are intended for France - - if ((has_tag($product_ref, "countries", "en:france")) and (defined $product_ref->{ingredients})) { - - my $carbon_footprint = 0; - - foreach my $ingredient_ref (@{$product_ref->{ingredients}}) { - - $log->debug("compute_carbon_footprint_from_meat_or_fish", {id => $ingredient_ref->{id}}) - if $log->is_debug(); - - if ((defined $ingredient_ref->{percent}) and ($ingredient_ref->{percent} > 0)) { - - $log->debug("compute_carbon_footprint_from_meat_or_fish", {percent => $ingredient_ref->{percent}}) - if $log->is_debug(); - - foreach my $parent (@parents) { - if (is_a('ingredients', $ingredient_ref->{id}, $parent)) { - $carbon_footprint += $ingredient_ref->{percent} * $carbon{$parent}; - $log->debug("found a parent with carbon footprint", {parent => $parent}) if $log->is_debug(); - - if (not defined $product_ref->{"carbon_footprint_from_meat_or_fish_debug"}) { - $product_ref->{"carbon_footprint_from_meat_or_fish_debug"} = ""; - } - $product_ref->{"carbon_footprint_from_meat_or_fish_debug"} - .= $ingredient_ref->{id} . " => " - . $parent . " " - . $ingredient_ref->{percent} - . "% x $carbon{$parent} = " - . $ingredient_ref->{percent} * $carbon{$parent} . " g - "; - - last; - } - } - } - } - - if ($carbon_footprint > 0) { - $product_ref->{nutriments}{"carbon-footprint-from-meat-or-fish_100g"} = $carbon_footprint; - $product_ref->{"carbon_footprint_from_meat_or_fish_debug"} =~ s/ - $//; - defined $product_ref->{misc_tags} or $product_ref->{misc_tags} = []; - add_tag($product_ref, "misc", "en:carbon-footprint-from-meat-or-fish"); - } - } - - return; -} - sub extract_ingredients_from_image ($product_ref, $id, $ocr_engine, $results_ref) { my $lc = $product_ref->{lc}; diff --git a/scripts/update_all_products.pl b/scripts/update_all_products.pl old mode 100755 new mode 100644 index 5f2df44221a23..cb3344ae3ed21 --- a/scripts/update_all_products.pl +++ b/scripts/update_all_products.pl @@ -115,6 +115,7 @@ my $remove_team = ''; my $remove_label = ''; my $remove_nutrient = ''; +my $remove_old_carbon_footprint = ''; my $fix_spanish_ingredientes = ''; my $team = ''; my $assign_categories_properties = ''; @@ -172,6 +173,7 @@ "remove-team=s" => \$remove_team, "remove-label=s" => \$remove_label, "remove-nutrient=s" => \$remove_nutrient, + "remove-old-carbon-footprint" => \$remove_old_carbon_footprint, "fix-spanish-ingredientes" => \$fix_spanish_ingredientes, "team=s" => \$team, "restore-values-deleted-by-user=s" => \$restore_values_deleted_by_user, @@ -243,11 +245,12 @@ and (not $remove_team) and (not $remove_label) and (not $remove_nutrient) + and (not $remove_old_carbon_footprint) and (not $mark_as_obsolete_since_date) and (not $compute_main_countries) and (not $assign_categories_properties) and (not $restore_values_deleted_by_user) - and not($delete_debug_tags) + and (not $delete_debug_tags) and (not $compute_codes) and (not $compute_carbon) and (not $compute_ecoscore) @@ -550,6 +553,28 @@ } } + if ($remove_old_carbon_footprint) { + my @product_fields_to_delete = ( + "environment_impact_level", "environment_impact_level_tags", + "environment_infocard", "environment_infocard_en", + "environment_infocard_fr", "carbon_footprint_from_known_ingredients_debug", + "carbon_footprint_from_meat_or_fish_debug" + ); + remove_fields($product_ref, \@product_fields_to_delete); + + if (defined $product_ref->{nutriments}) { + delete $product_ref->{nutriments}{"carbon-footprint-from-known-ingredients_100g"}; + delete $product_ref->{nutriments}{"carbon-footprint-from-meat-or-fish"}; + delete $product_ref->{nutriments}{"carbon-footprint-from-meat-or-fish_100g"}; + delete $product_ref->{nutriments}{"carbon-footprint-from-meat-or-fish_serving"}; + delete $product_ref->{nutriments}{"carbon-footprint-from-meat-or-fish_product"}; + } + + remove_tag($product_ref, "misc", "en:environment-infocard"); + remove_tag($product_ref, "misc", "en:carbon-footprint-from-known-ingredients"); + remove_tag($product_ref, "misc", "en:carbon-footprint-from-meat-or-fish"); + } + # Some Spanish products had their ingredients list wrongly cut after "Ingredientes" # before: Brócoli*. (* Ingredientes procedentes de la agricultura ecológica). Categoría I. # after: procedentes de la agricultura ecológica). Categoría I.