Skip to content

Commit

Permalink
feat: add serving size to health knowledge panel (#6793)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgarel authored May 20, 2022
1 parent b8e36da commit 1144af5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lib/ProductOpener/KnowledgePanels.pm
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,8 @@ sub create_health_card_panel($$$) {

create_physical_activities_panel($product_ref, $target_lc, $target_cc);

create_serving_size_panel($product_ref, $target_lc, $target_cc);

create_ingredients_panel($product_ref, $target_lc, $target_cc);

create_additives_panel($product_ref, $target_lc, $target_cc);
Expand Down Expand Up @@ -835,6 +837,42 @@ sub create_nutrition_facts_table_panel($$$) {
}



=head2 create_serving_size_panel( $product_ref, $target_lc, $target_cc )
Creates a knowledge panel with portion size.
=head3 Arguments
=head4 product reference $product_ref
Loaded from the MongoDB database, Storable files, or the OFF API.
=head4 language code $target_lc
Returned attributes contain both data and strings intended to be displayed to users.
This parameter sets the desired language for the user facing strings.
=head4 country code $target_cc
=cut

sub create_serving_size_panel($$$) {

my $product_ref = shift;
my $target_lc = shift;
my $target_cc = shift;

$log->debug("create serving size panel", { code => $product_ref->{code}, nutriscore_data => $product_ref->{nutriscore_data} }) if $log->is_debug();

# Generate a panel only for food products that have a serving size
if (defined $product_ref->{serving_size}) {
my $panel_data_ref = {};
create_panel_from_json_template("serving_size", "api/knowledge-panels/health/nutrition/serving_size.tt.json",
$panel_data_ref, $product_ref, $target_lc, $target_cc);
}
}

=head2 create_physical_activities_panel ( $product_ref, $target_lc, $target_cc )
Creates a knowledge panel to indicate how much time is needed to burn the calories of a product
Expand Down
3 changes: 3 additions & 0 deletions templates/api/knowledge-panels/health/health_card.tt.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
[% IF panels.nutrition_facts_table.defined %]
"nutrition_facts_table",
[% END %]
[% IF panels.serving_size.defined %]
"serving_size",
[% END %]
],
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"level": "info",
"topics": [
"health"
],
"type": "inline",
"expanded": true,
"elements": [
{
"element_type": "text",
"text_element": {
"html": `
<strong>[% lang('serving_size') %][% sep %]:</strong>
[% product.serving_size %]
`,
}
},
]
}

0 comments on commit 1144af5

Please sign in to comment.