Skip to content
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

refactor: merge the products and product images directories for OFF, OBF, OPF and OPFF - WIP #10959

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
9 changes: 9 additions & 0 deletions cgi/product_jqm_multilingual.pl
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ =head1 DESCRIPTION
$product_ref = init_product($User_id, $Org_id, $code, $country);
$product_ref->{interface_version_created} = $interface_version;
}
else {
# There is an existing product
# If the product has a product_type and it is not the product_type of the server, redirect to the correct server

if ((defined $product_ref->{product_type}) and ($product_ref->{product_type} ne $options{product_type})) {
redirect_to_url($request_ref, 307,
format_subdomain($subdomain, $product_ref->{product_type}) . '/cgi/product_jqm.pl?code=' . $code);
}
}

# Process edit rules

Expand Down
27 changes: 19 additions & 8 deletions cgi/product_multilingual.pl
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,15 @@ ($product_ref)
if (not defined $product_ref) {
display_error_and_exit($request_ref, sprintf(lang("no_product_for_barcode"), $code), 404);
}
else {
# There is an existing product
# If the product has a product_type and it is not the product_type of the server, redirect to the correct server
# We use a 302 redirect so that browsers issue a GET request to display the form (even if we received a POST request)
if ((defined $product_ref->{product_type}) and ($product_ref->{product_type} ne $options{product_type})) {
redirect_to_url($request_ref, 302,
format_subdomain($subdomain, $product_ref->{product_type}) . '/cgi/product.pl?code=' . $code);
}
}
}
}

Expand Down Expand Up @@ -399,11 +408,15 @@ ($product_ref)

exists $product_ref->{new_server} and delete $product_ref->{new_server};

# 26/01/2017 - disallow barcode changes until we fix bug #677
if ($User{moderator} and (defined single_param("new_code")) and (single_param("new_code") ne "")) {
if ($User{moderator}) {
if ((defined single_param("new_code")) and (single_param("new_code") ne "")) {

change_product_server_or_code($product_ref, single_param("new_code"), \@errors);
$code = $product_ref->{code};
change_product_server_or_code($product_ref, single_param("new_code"), \@errors);
$code = $product_ref->{code};
}
if ((defined single_param("product_type")) and (single_param("product_type") ne "")) {
change_product_type($product_ref, single_param("product_type"), \@errors);
}
}

my @param_fields = ();
Expand Down Expand Up @@ -848,14 +861,12 @@ ($product_ref, $field, $language, $request_ref)

my $label_new_code = $Lang{new_code}{$lc};

# 26/01/2017 - disallow barcode changes until we fix bug #677
if ($User{moderator}) {
}

$template_data_ref_display->{org_id} = $Org_id;
$template_data_ref_display->{label_new_code} = $label_new_code;
$template_data_ref_display->{owner_id} = $Owner_id;

$template_data_ref_display->{product_types} = $options{product_types};

# obsolete products: restrict to admin on public site
# authorize owners on producers platform
if ($User{moderator} or $Owner_id) {
Expand Down
10 changes: 9 additions & 1 deletion lib/ProductOpener/APIProductRead.pm
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ use vars @EXPORT_OK;

use ProductOpener::Config qw/:all/;
use ProductOpener::Paths qw/%BASE_DIRS/;
use ProductOpener::Display qw/request_param single_param/;
use ProductOpener::Display qw/$subdomain redirect_to_url request_param single_param/;
use ProductOpener::Users qw/$Owner_id/;
use ProductOpener::Lang qw/$lc/;
use ProductOpener::Products qw/:all/;
use ProductOpener::Ingredients qw/flatten_sub_ingredients/;
use ProductOpener::API qw/add_error customize_response_for_product normalize_requested_code/;
use ProductOpener::URL qw(format_subdomain);

my $cc;

Expand Down Expand Up @@ -138,6 +139,13 @@ sub read_product_api ($request_ref) {
$response_ref->{result} = {id => "product_not_found"};
}
else {
# If the product has a product_type and it is not the product_type of the server, redirect to the correct server

if ((defined $product_ref->{product_type}) and ($product_ref->{product_type} ne $options{product_type})) {
redirect_to_url($request_ref, 302,
format_subdomain($subdomain, $product_ref->{product_type}) . $request_ref->{original_query_string});
}

$response_ref->{result} = {id => "product_found"};

add_images_urls_to_product($product_ref, $lc);
Expand Down
12 changes: 11 additions & 1 deletion lib/ProductOpener/APIProductWrite.pm
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ BEGIN {
use vars @EXPORT_OK;

use ProductOpener::Config qw/:all/;
use ProductOpener::Display qw/$country request_param single_param/;
use ProductOpener::Display qw/$subdomain redirect_to_url $country request_param single_param/;
use ProductOpener::Users qw/$Org_id $Owner_id $User_id/;
use ProductOpener::Lang qw/$lc/;
use ProductOpener::Products qw/:all/;
Expand All @@ -54,6 +54,7 @@ use ProductOpener::Packaging
qw/add_or_combine_packaging_component_data get_checked_and_taxonomized_packaging_component_data/;
use ProductOpener::Text qw/remove_tags_and_quote/;
use ProductOpener::Tags qw/%language_fields %writable_tags_fields add_tags_to_field compute_field_tags/;
use ProductOpener::URL qw(format_subdomain);

use Encode;

Expand Down Expand Up @@ -430,6 +431,15 @@ sub write_product_api ($request_ref) {
$product_ref = init_product($User_id, $Org_id, $code, $country);
$product_ref->{interface_version_created} = "20221102/api/v3";
}
else {
# There is an existing product
# If the product has a product_type and it is not the product_type of the server, redirect to the correct server

if ((defined $product_ref->{product_type}) and ($product_ref->{product_type} ne $options{product_type})) {
redirect_to_url($request_ref, 307,
format_subdomain($subdomain, $product_ref->{product_type}) . '/api/v3/product/' . $code);
}
}

# Use default request language if we did not get tags_lc
if (not defined $request_body_ref->{tags_lc}) {
Expand Down
13 changes: 11 additions & 2 deletions lib/ProductOpener/APITest.pm
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,14 @@ sub execute_request ($test_ref, $ua) {

my $response;

# For some tests, we don't want to follow redirects. We want to see the 302 responses, not the response to the final destination
if ((defined $test_ref->{expected_status_code}) and (int($test_ref->{expected_status_code} / 100) == 3)) {
$test_ua->max_redirect(0);
}
else {
$test_ua->max_redirect(3);
}

# Send the request
if ($method eq 'OPTIONS') {
# not yet supported by our (system) version of HTTP::Request::Common
Expand Down Expand Up @@ -541,9 +549,10 @@ sub check_request_response ($test_ref, $response, $test_id, $test_dir, $expected

my $response_content = $response->decoded_content;

# Check that we don't get an errore message generated by the Apache Server
# Check that we don't get an error message generated by the Apache Server
# e.g. "Apache/2.4.56 (Debian) Server at world.openfoodfacts.localhost Port 80"
if ($response_content =~ /Apache.*Server/) {
# unless it is a redirect
if (($response_content =~ /Apache.*Server/) and not($response->code =~ /^3\d\d$/)) {
fail("Received an Apache Server generated error message for test $test_case");
diag("Response content: " . $response_content);
}
Expand Down
135 changes: 135 additions & 0 deletions lib/ProductOpener/Config.pm
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,141 @@ if (not defined $flavor) {
}

use Module::Load;

autoload("ProductOpener::Config_$flavor");

# Add values common to all flavors

# define the normalization applied to change a string to a tag id (in particular for taxonomies)
# tag ids are also used in URLs.

# unaccent:
# - useful when accents are sometimes ommited (e.g. in French accents are often not present on capital letters),
# either in print, or when typed by users.
# - dangerous if different words (in the same context like ingredients or category names) have the same unaccented form
# lowercase:
# - useful when the same word appears in lowercase, with a first capital letter, or in all caps.

# IMPORTANT: if you change it, you need to change $BUILD_TAGS_VERSION in Tags.pm

%ProductOpener::Config::string_normalization_for_lang = (
# no_language is used for strings that are not in a specific language (e.g. user names)
no_language => {
unaccent => 1,
lowercase => 1,
},
# default is used for languages that do not have specified values
default => {
unaccent => 0,
lowercase => 1,
},
# German umlauts should not be converted (e.g. ä -> ae) as there are many conflicts
de => {
unaccent => 0,
lowercase => 1,
},
# French has very few actual conflicts caused by unaccenting (one counter example is "pâtes" and "pâtés")
# Accents or often not present in capital letters (beginning of word, or in all caps text).
fr => {
unaccent => 1,
lowercase => 1,
},
# Same for Spanish, Italian and Portuguese
ca => {
unaccent => 1,
lowercase => 1,
},
es => {
unaccent => 1,
lowercase => 1,
},
it => {
unaccent => 1,
lowercase => 1,
},
nl => {
unaccent => 1,
lowercase => 1,
},
pt => {
unaccent => 1,
lowercase => 1,
},
sk => {
unaccent => 1,
lowercase => 1,
},
# English has very few accented words, and they are very often not accented by users or in ingredients lists etc.
en => {
unaccent => 1,
lowercase => 1,
},
# xx: language less entries, also deaccent
xx => {
unaccent => 1,
lowercase => 1,
},
);

%ProductOpener::Config::admins = map {$_ => 1} qw(
alex-off
cha-delh
charlesnepote
gala-nafikova
hangy
manoncorneille
raphael0202
stephane
tacinte
teolemon
g123k
valimp
);

=head2 Available product types and flavors

=cut

$ProductOpener::Config::options{product_types} = [qw(food petfood beauty product)];
$ProductOpener::Config::options{product_types_flavors} = {
food => "off",
petfood => "opff",
beauty => "obf",
product => "opf"
};

$ProductOpener::Config::options{flavors_product_types}
= {reverse %{$ProductOpener::Config::options{product_types_flavors}}};

$ProductOpener::Config::options{product_types_domains} = {
food => "openfoodfacts.org",
petfood => "openpetfoodfacts.org",
beauty => "openbeautyfacts.org",
product => "openproductsfacts.org"
};

$ProductOpener::Config::options{other_servers} = {
obf => {
name => "Open Beauty Facts",
mongodb => "obf",
domain => "openbeautyfacts.org",
},
off => {
name => "Open Food Facts",
mongodb => "off",
domain => "openfoodfacts.org",
},
opf => {
name => "Open Products Facts",
mongodb => "opf",
domain => "openproductsfacts.org",
},
opff => {
prefix => "opff",
name => "Open Pet Food Facts",
mongodb => "opff",
domain => "openpetfoodfacts.org",
}
};

1;
23 changes: 0 additions & 23 deletions lib/ProductOpener/Config_off.pm
Original file line number Diff line number Diff line change
Expand Up @@ -177,21 +177,6 @@ $flavor = 'off';
},
);

%admins = map {$_ => 1} qw(
alex-off
cha-delh
charlesnepote
gala-nafikova
hangy
manoncorneille
raphael0202
stephane
tacinte
teolemon
g123k
valimp
);

%options = (
site_name => "Open Food Facts",
product_type => "food",
Expand Down Expand Up @@ -1099,30 +1084,22 @@ $options{current_server} = "off";
$options{other_servers} = {
obf => {
name => "Open Beauty Facts",
data_root => "/srv/obf",
www_root => "/srv/obf/html",
mongodb => "obf",
domain => "openbeautyfacts.org",
},
off => {
name => "Open Food Facts",
data_root => "/srv/off",
www_root => "/srv/off/html",
mongodb => "off",
domain => "openfoodfacts.org",
},
opf => {
name => "Open Products Facts",
data_root => "/srv/opf",
www_root => "/srv/opf/html",
mongodb => "opf",
domain => "openproductsfacts.org",
},
opff => {
prefix => "opff",
name => "Open Pet Food Facts",
data_root => "/srv/opff",
www_root => "/srv/opff/html",
mongodb => "opff",
domain => "openpetfoodfacts.org",
}
Expand Down
7 changes: 7 additions & 0 deletions lib/ProductOpener/Display.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7899,6 +7899,13 @@ JS
display_error_and_exit($request_ref, sprintf(lang("no_product_for_barcode"), $code), 404);
}

# If the product has a product_type and it is not the product_type of the server, redirect to the correct server

if ((defined $product_ref->{product_type}) and ($product_ref->{product_type} ne $options{product_type})) {
redirect_to_url($request_ref, 302,
format_subdomain($subdomain, $product_ref->{product_type}) . product_url($product_ref));
}

$title = product_name_brand_quantity($product_ref);
my $titleid = get_string_id_for_lang($lc, product_name_brand($product_ref));

Expand Down
Loading
Loading