-
-
Notifications
You must be signed in to change notification settings - Fork 384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: ingredients knowledge panels #10904
Draft
stephanegigandet
wants to merge
17
commits into
main
Choose a base branch
from
ingredients-panels
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
5a3a5d0
feat: ingredients knowledge panels
stephanegigandet dbb3330
ingredients templates
stephanegigandet c81a152
fixes for mobile app
stephanegigandet 2eccff5
test: Update tests results
bfc4513
double backticks
stephanegigandet 99c52e4
update tests
stephanegigandet fc47017
panels without content cannot be expanded
stephanegigandet 47dc289
panels without content cannot be expanded
stephanegigandet e6c7ad6
Update lib/ProductOpener/KnowledgePanelsIngredients.pm
stephanegigandet 98b62b4
Update lib/ProductOpener/KnowledgePanelsIngredients.pm
stephanegigandet 8c18816
fix template
stephanegigandet 972b015
< 2%
stephanegigandet c504614
< 2%
stephanegigandet e2e7427
< 2%
stephanegigandet b26162f
merged
stephanegigandet 8fbe28f
lint
stephanegigandet 2a9cdf6
test: Update tests results
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
# This file is part of Product Opener. | ||
# | ||
# Product Opener | ||
# Copyright (C) 2011-2023 Association Open Food Facts | ||
# Contact: [email protected] | ||
# Address: 21 rue des Iles, 94100 Saint-Maur des Fossés, France | ||
# | ||
# Product Opener is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as | ||
# published by the Free Software Foundation, either version 3 of the | ||
# License, or (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
=head1 NAME | ||
|
||
ProductOpener::KnowledgePanelsIngredients - Generate knowledge panels to report a problem with the data or the product | ||
|
||
=head1 SYNOPSIS | ||
|
||
Knowledge panels to indicate how to report a problem with the product data, | ||
or with the product (e.g. link to report to authorities like SignalConso in France) | ||
|
||
=cut | ||
|
||
package ProductOpener::KnowledgePanelsIngredients; | ||
|
||
use ProductOpener::PerlStandards; | ||
use Exporter qw< import >; | ||
|
||
use Log::Any qw($log); | ||
|
||
BEGIN { | ||
use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS); | ||
@EXPORT_OK = qw( | ||
&create_ingredients_list_panel | ||
&create_data_quality_panel | ||
); # symbols to export on request | ||
%EXPORT_TAGS = (all => [@EXPORT_OK]); | ||
} | ||
|
||
use vars @EXPORT_OK; | ||
|
||
use ProductOpener::KnowledgePanels | ||
qw(create_panel_from_json_template add_taxonomy_properties_in_target_languages_to_object); | ||
use ProductOpener::Tags qw(:all); | ||
|
||
use Encode; | ||
use Data::DeepAccess qw(deep_get); | ||
|
||
=head2 create_ingredients_list_panel ( $product_ref, $target_lc, $target_cc, $options_ref ) | ||
|
||
Creates a panel with a list of ingredients as individual panels. | ||
|
||
=head3 Arguments | ||
|
||
=head4 product reference $product_ref | ||
|
||
=head4 language code $target_lc | ||
|
||
=head4 country code $target_cc | ||
|
||
=cut | ||
|
||
sub create_ingredients_list_panel ($product_ref, $target_lc, $target_cc, $options_ref) { | ||
|
||
$log->debug("create ingredients list panel", {code => $product_ref->{code}}) if $log->is_debug(); | ||
|
||
# Create a panel only if the product has ingredients | ||
|
||
if ((defined $product_ref->{ingredients_tags}) and (scalar @{$product_ref->{ingredients_tags}} > 0)) { | ||
|
||
my $ingredient_i = 0; # sequence number for ingredients | ||
# creates each individual panels for each ingredient | ||
my @ingredients_panels_ids | ||
stephanegigandet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
= create_ingredients_panels_recursive($product_ref, \$ingredient_i, 0, $product_ref->{ingredients}, | ||
$target_lc, $target_cc, $options_ref); | ||
my $ingredients_list_panel_data_ref = {ingredients_panels_ids => \@ingredients_panels_ids}; | ||
|
||
# create the panel that reference ingredients panels | ||
create_panel_from_json_template( | ||
stephanegigandet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"ingredients_list", | ||
"api/knowledge-panels/health/ingredients/ingredients_list.tt.json", | ||
$ingredients_list_panel_data_ref, | ||
$product_ref, $target_lc, $target_cc, $options_ref | ||
); | ||
|
||
} | ||
return; | ||
} | ||
|
||
sub create_ingredients_panels_recursive ($product_ref, $ingredient_i_ref, $level, $ingredients_ref, $target_lc, | ||
$target_cc, $options_ref) | ||
{ | ||
|
||
my @ingredients_panels_ids = (); | ||
|
||
foreach my $ingredient_ref (@$ingredients_ref) { | ||
|
||
push @ingredients_panels_ids, | ||
create_ingredient_panel($product_ref, $ingredient_i_ref, $level, $ingredient_ref, $target_lc, $target_cc, | ||
$options_ref); | ||
if (defined $ingredient_ref->{ingredients}) { | ||
push @ingredients_panels_ids, | ||
create_ingredients_panels_recursive($product_ref, $ingredient_i_ref, $level + 1, | ||
$ingredient_ref->{ingredients}, | ||
$target_lc, $target_cc, $options_ref); | ||
} | ||
|
||
} | ||
|
||
return @ingredients_panels_ids; | ||
} | ||
|
||
sub create_ingredient_panel ($product_ref, $ingredient_i_ref, $level, $ingredient_ref, $target_lc, $target_cc, | ||
$options_ref) | ||
{ | ||
|
||
$$ingredient_i_ref++; | ||
my $ingredient_panel_id = "ingredient_" . $$ingredient_i_ref; | ||
|
||
my $ingredient_panel_data_ref | ||
= {ingredient_id => $ingredient_ref->{id}, level => $level, ingredient => $ingredient_ref}; | ||
|
||
# Wikipedia abstracts, in target language or English | ||
|
||
my $target_lcs_ref = [$target_lc]; | ||
if ($target_lc ne "en") { | ||
push @$target_lcs_ref, "en"; | ||
} | ||
|
||
add_taxonomy_properties_in_target_languages_to_object($ingredient_panel_data_ref, "ingredients", | ||
$ingredient_ref->{id}, ["wikipedia_url", "wikipedia_title", "wikipedia_abstract"], | ||
$target_lcs_ref); | ||
|
||
# We check if the knowledge content for this ingredient (and language/country) is available. | ||
# If it is it will be displayed instead of the wikipedia extract | ||
my $ingredient_description = get_knowledge_content("ingredients", $ingredient_ref->{id}, $target_lc, $target_cc); | ||
|
||
if (defined $ingredient_description) { | ||
$ingredient_panel_data_ref->{ingredient_description} = $ingredient_description; | ||
} | ||
|
||
create_panel_from_json_template($ingredient_panel_id, "api/knowledge-panels/health/ingredients/ingredient.tt.json", | ||
$ingredient_panel_data_ref, $product_ref, $target_lc, $target_cc, $options_ref); | ||
|
||
return $ingredient_panel_id; | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
templates/api/knowledge-panels/health/ingredients/ingredient.tt.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
{ | ||
"level": "info", | ||
"topics": [ | ||
"health" | ||
], | ||
"size": "small", | ||
"title_element": { | ||
// double backticks: convert to a single line without newlines and extra spaces | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😲 cool ! |
||
"title": `` | ||
[% dash = "—" %] | ||
[% dash.repeat(panel.level) %] | ||
|
||
[% display_taxonomy_tag_name("ingredients", panel.ingredient_id) %][% sep %]: | ||
[% IF panel.ingredient.percent.defined %] | ||
[% sprintf("%.1f", panel.ingredient.percent) %]% | ||
[% ELSIF panel.ingredient.percent_estimate.defined %] | ||
[% IF panel.ingredient.percent_estimate < 2 %] | ||
< 2% ([% lang("estimate") %]) | ||
[% ELSE %] | ||
[% sprintf("%.1f", panel.ingredient.percent_estimate) %]% ([% lang("estimate") %]) | ||
[% END %] | ||
[% END %] | ||
``, | ||
}, | ||
"elements": [ | ||
[% IF panel.ingredient_description %] | ||
{ | ||
"element_type": "text", | ||
"text_element": { | ||
"html": `[% panel.ingredient_description %]` | ||
alexgarel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
[% ELSIF panel.wikipedia_abstract %] | ||
{ | ||
"element_type": "text", | ||
"text_element": { | ||
"html": `<strong>[% edq(panel.wikipedia_title) %][% sep %]:</strong> [% edq(panel.wikipedia_abstract) %]`, | ||
"source_text": `Wikipedia`, | ||
"source_url": `[% panel.wikipedia_url %]`, | ||
"source_lc": "[% panel.wikipedia_url_lc %]", | ||
"source_language": "[% panel.wikipedia_url_language %]" | ||
} | ||
} | ||
[% END %] | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
templates/api/knowledge-panels/health/ingredients/ingredients_list.tt.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"level": "info", | ||
"topics": [ | ||
"health" | ||
], | ||
"expanded": true, | ||
"title_element": { | ||
"title":"[% lang("ingredient_information") %]", | ||
}, | ||
"elements": [ | ||
[% FOREACH ingredient_panel_id IN panel.ingredients_panels_ids %] | ||
{ | ||
"element_type": "panel", | ||
"panel_element": { | ||
"panel_id": "[% ingredient_panel_id %]", | ||
} | ||
}, | ||
[% END %] | ||
] | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea :-)