Skip to content

Commit

Permalink
Feat: manufacturing place + origins of ingredients knowledge panels +…
Browse files Browse the repository at this point in the history
… Normalize all panels (#6069)

* Feat: manufacturing place + origins of ingredients knowledge panels

* use dot-notation

* normalize all panels

* normalize all panels

* add comment
  • Loading branch information
stephanegigandet authored Nov 15, 2021
1 parent b3f8ac5 commit d37011a
Show file tree
Hide file tree
Showing 21 changed files with 315 additions and 57 deletions.
16 changes: 14 additions & 2 deletions html/js/display-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,26 @@ function displayPointers(pointers) {
var markers = [];
for (var i = 0; i < pointers.length; ++i) {
var pointer = pointers[i];
var marker = new L.marker(pointer);
var coordinates;

// If pointer is an array, it just contains (lat, lng) geo coordinates
if (Array.isArray(pointer)) {
coordinates = pointer;
}
// Otherwise we have a structured object
// e.g. from a map element of a knowledge panel
else {
coordinates = [ pointer.geo.lat, pointer.geo.lng ];
}

var marker = new L.marker(coordinates);
markers.push(marker);
}

if (markers.length > 0) {
L.featureGroup(markers).addTo(actualMap);
fitBoundsToAllLayers(actualMap);
actualMap.setZoom(10);
actualMap.setZoom(8);
}
});
}
Expand Down
6 changes: 5 additions & 1 deletion lib/ProductOpener/Display.pm
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,11 @@ sub process_template($$$) {
};
$template_data_ref->{sprintf} = sub($$) {
return sprintf ($_[0], $_[1]);
};
};

$template_data_ref->{encode_json} = sub($) {
return JSON::PP->new->utf8->canonical->encode($_[0]);
};

return($tt->process($template_filename, $template_data_ref, $result_content_ref));
}
Expand Down
79 changes: 76 additions & 3 deletions lib/ProductOpener/KnowledgePanels.pm
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ sub create_knowledge_panels($$$$) {
$product_ref->{"knowledge_panels_" . $target_lc} = {};

# Test panel to test the start of the API

if ($product_ref->{code} eq "3017620422003") {
# Disabled, kept as reference when we create a "Do you know" panel
if ($product_ref->{code} eq "3017620422003--disabled") {

my $test_panel_ref = {
parent_panel_id => "root",
Expand Down Expand Up @@ -165,6 +165,10 @@ sub create_knowledge_panels($$$$) {
create_ecoscore_panel($product_ref, $target_lc, $target_cc);

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

# Create the root panel that contains the panels we want to show directly on the product page
create_panel_from_json_template("root", "api/knowledge-panels/root.tt.json",
{}, $product_ref, $target_lc, $target_cc);
}


Expand Down Expand Up @@ -525,11 +529,80 @@ sub create_environment_card_panel($$$) {

# Tell the environment card template to include packaging recycling panel
$panel_data_ref->{packaging_recycling} = 1;


# Create panel for manufacturing place
$panel_data_ref->{manufacturing_place} = create_manufacturing_place_panel($product_ref, $target_lc, $target_cc);

# Origins of ingredients for the environment card
create_panel_from_json_template("origins_of_ingredients", "api/knowledge-panels/environment/origins_of_ingredients.tt.json",
$panel_data_ref, $product_ref, $target_lc, $target_cc);

# Create the environment_card panel
create_panel_from_json_template("environment_card", "api/knowledge-panels/environment/environment_card.tt.json",
$panel_data_ref, $product_ref, $target_lc, $target_cc);
}


=head2 create_manufacturing_place_panel ( $product_ref, $target_lc, $target_cc )
Creates a knowledge panel when we know the location of the manufacturing place,
usually through a packaging code.
=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
The Eco-Score depends on the country of the consumer (as the transport bonus/malus depends on it)
=head3 Return value
1 to indicate that the panel has been created
0 to indicate that the panel was not created (if we don't have enough data for the product)
=cut

sub create_manufacturing_place_panel($$$) {

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

$log->debug("create_manufacturing_place_panel", { code => $product_ref->{code} }) if $log->is_debug();

# Go through the product packaging codes, keep the first one with associated geo coordinates
if (defined $product_ref->{emb_codes_tags}) {
foreach my $packager_code_tagid (@{$product_ref->{emb_codes_tags}}) {
# we will create a panel for the first known location
if (exists $packager_codes{$packager_code_tagid}) {
$log->debug("packager code found for the canon_tagid", { cc => $packager_codes{$packager_code_tagid}{cc} }) if $log->is_debug();
my ($lat, $lng) = get_packager_code_coordinates($packager_code_tagid);
if ((defined $lat) and (defined $lng)) {

my $panel_data_ref = {
packager_code_data => $packager_codes{$packager_code_tagid},
lat => $lat + 0.0,
lng => $lng + 0.0,
};

create_panel_from_json_template("manufacturing_place", "api/knowledge-panels/environment/manufacturing_place.tt.json",
$panel_data_ref, $product_ref, $target_lc, $target_cc);

return 1;
}
}
}
}

return 0;
}

1;
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
[% SET co2_100g = product.ecoscore_data.agribalyse.co2_total / 10 %]
[% SET driving_100g = co2_100g * 100 / 19.3 %]
{
"parent_panel_id": "root",
"type" : "score",
"level" :"info",
"topics": [
"environment"
],
"expanded": false,
[% IF driving_100g >= 3 %]
"evaluation": "bad",
[% ELSIF driving_100g >= 1.5 %]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
{
"parent_panel_id": "ecoscore",
"type" : "score",
"level" :"info",
"grade": "[% panel.agribalyse_grade %]",
"topics": [
"environment"
],
"title_element": {
"title": "[% lang('average_impact_of_the_category') %][% sep %]: [% panel.agribalyse_grade FILTER upper %] (Score: [% panel.agribalyse_score %]/100)",
"subtitle": "[% lang('categories_s') FILTER ucfirst %][% sep %]: [% panel.agribalyse_category_name %]"
"subtitle": "[% lang('categories_s') FILTER ucfirst %][% sep %]: [% panel.agribalyse_category_name %]",
"type": "grade",
"grade": "[% panel.agribalyse_grade %]",
},
"elements": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
{
"parent_panel_id": "root",
"type": "score",
"level": "info",
"grade": "[% panel.grade %]",
"topics": [
"environment"
],
"title_element": {
"icon_url": "[% static_subdomain %]/images/attributes/ecoscore-[% panel.grade %].svg",
"title": "[% panel.title %]",
"type": "grade",
"grade": "[% panel.grade %]",
},
"elements": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
{
"parent_panel_id": "root",
"type": "score",
"level": "info",
"grade": "unknown",
"topics": [
"environment"
],
"title_element": {
"icon_url": "[% static_subdomain %]/images/attributes/ecoscore-unknown.svg",
"title": "[% lang("attribute_ecoscore_unknown_title") %] - [% lang("attribute_ecoscore_unknown_description_short") %]",
"type": "grade",
"grade": "unknown",
},
"elements": [
{
"element_type": "text",
"text_element": {
"text_type": "summary",
"type": "summary",
"html": "We could not compute the Eco-Score of this product as it is missing some data, could you help complete it?"
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"parent_panel_id": "ecoscore",
"type" : "score",
"level" :"info",
"topics": [
"environment"
Expand All @@ -18,7 +16,7 @@
{
"element_type": "text",
"text_element": {
"text_type": "warning",
"type": "warning",
"html": `
[% lang('ecoscore_ingredients_not_indicated') %]<br><br>
[% lang('ecoscore_please_add_the_ingredients') %]<br><br>
Expand Down Expand Up @@ -64,7 +62,6 @@
"element_type": "table",
"table_element": {
"id": "ecoscore_origins_of_ingredients_table",
"table_type": "percents",
"title": "[% lang('ecoscore_origins_of_ingredients') %]",
"columns": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"parent_panel_id": "ecoscore",
"type" : "score",
"level" :"info",
"topics": [
"environment"
Expand Down Expand Up @@ -48,7 +46,6 @@
"element_type": "table",
"table_element": {
"id": "ecoscore_packaging_components",
"type": "table",
"title": "[% lang('packaging_parts') %]",
"columns": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"parent_panel_id": "ecoscore",
"type" : "score",
"level" :"info",
"topics": [
"environment"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
{
"parent_panel_id": "ecoscore",
"type" : "score",
"level" :"info",
"topics": [
"environment"
Expand All @@ -17,7 +15,7 @@
{
"element_type": "text",
"text_element": {
"text_type": "summary",
"type": "summary",
"html": `
[% lang('ecoscore_ingredients_unknown') %]
[% lang('ecoscore_edit_for_more_precise_ecoscore') %]<br><br>
Expand All @@ -39,7 +37,7 @@
{
"element_type": "text",
"text_element": {
"text_type": "summary",
"type": "summary",
"html": `
<p><strong>[% lang('contains_palm_oil') %]</strong></p>
<p>[% lang('contains_palm_oil_description') %]</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
[% SET sum_of_bonuses_and_maluses = product.ecoscore_data.adjustments.production_system.value + product.ecoscore_data.adjustments.packaging.value + product.ecoscore_data.adjustments.threatened_species.value + product.ecoscore_data.adjustments.origins_of_ingredients.value %]
{
"parent_panel_id": "root",
"type": "score",
"level": "info",
"grade": "[% panel.grade %]",
"topics": [
"environment"
],
"title_element": {
"title": "Impact for this product: [% panel.grade FILTER upper %] (Score: [% panel.score %]/100)",
"subtitle": "[% lang("front_alt") %][% sep %]: [% product_name_brand_quantity(product) %]",
"type": "grade",
"grade": "[% panel.grade %]",
},
"elements": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"parent_panel_id": "root",
"level": "info",
"type": "card",
"expanded": true,
"topics": [
"environment"
],
Expand Down Expand Up @@ -34,7 +33,17 @@
],
},
},
[% END %]
[% END %]
{
"element_type": "panel_group",
"panel_group_element": {
"title": "[% lang('ecoscore_transportation') %]",
"panel_ids": [
[% IF panel.manufacturing_place %]"manufacturing_place",[% END %]
"origins_of_ingredients",
],
},
},
[% IF panel.palm_oil %]
{
"element_type": "panel_group",
Expand Down
1 change: 0 additions & 1 deletion templates/api/knowledge-panels/environment/label.tt.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"parent_panel_id": "ecoscore",
"level" :"info",
"topics": [
"environment"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"level" :"info",
"topics": [
"environment"
],
"evaluation": "neutral",
"expanded": true,
"title_element": {
"title": "[% lang('manufacturing_places_s') FILTER ucfirst %]",
[% IF panel.packager_code_data.cc == 'fr' %]
"subtitle": "[% panel.packager_code_data.commune %] - [% display_taxonomy_tag('countries', 'en:france') %]",
[% ELSIF panel.packager_code_data.cc == 'ch' %]
"subtitle": "[% panel.packager_code_data.full_address %]",
[% ELSIF panel.packager_code_data.cc == 'es' %]
"subtitle": "[% panel.packager_code_data.provincia_localidad %]",
[% ELSIF panel.packager_code_data.cc == 'uk' %]
"subtitle": "[% panel.packager_code_data.district %]",
[% END %]
"icon_url": "[% static_subdomain %]/images/icons/dist/transportation.svg",
"icon_color_from_evaluation": true,
},
"elements": [
{
"element_type": "map",
"map_element": {
"pointers": [
{
"geo": {
"lat": [% panel.lat %],
"lng": [% panel.lng %],
}
}
]
}
},
]
}
Loading

0 comments on commit d37011a

Please sign in to comment.