Skip to content

Commit

Permalink
feat: allow users to submit origin tags in their language (#561)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael0202 authored Nov 13, 2024
1 parent fa54a36 commit 2b867db
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
37 changes: 24 additions & 13 deletions open_prices/prices/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@

# Taxonomy mapping generation takes ~200ms, so we cache it to avoid
# recomputing it for each request.
_cached_create_taxonomy_mapping = functools.lru_cache(maxsize=1)(
create_taxonomy_mapping
)
_cached_create_taxonomy_mapping = functools.lru_cache()(create_taxonomy_mapping)


class PriceQuerySet(models.QuerySet):
Expand Down Expand Up @@ -242,9 +240,13 @@ def clean(self, *args, **kwargs):
# creating it multiple times.
# If the entry does not exist in the taxonomy, category_tag will
# be set to the tag version of the value (ex: `fr:boissons`).
taxonomy_mapping = _cached_create_taxonomy_mapping(category_taxonomy)
category_taxonomy_mapping = _cached_create_taxonomy_mapping(
category_taxonomy
)
try:
mapped_tags = map_to_canonical_id(taxonomy_mapping, [self.category_tag])
category_mapped_tags = map_to_canonical_id(
category_taxonomy_mapping, [self.category_tag]
)
except ValueError as e:
# The value is not language-prefixed
validation_errors = utils.add_validation_error(
Expand All @@ -255,7 +257,7 @@ def clean(self, *args, **kwargs):
else:
# Set the canonical id (or taggified version) as the
# category_tag
self.category_tag = mapped_tags[self.category_tag]
self.category_tag = category_mapped_tags[self.category_tag]
if self.labels_tags:
if not isinstance(self.labels_tags, list):
validation_errors = utils.add_validation_error(
Expand All @@ -281,13 +283,22 @@ def clean(self, *args, **kwargs):
)
else:
origins_taxonomy = get_taxonomy("origin")
for origin_tag in self.origins_tags:
if origin_tag not in origins_taxonomy:
validation_errors = utils.add_validation_error(
validation_errors,
"origins_tags",
f"Invalid origin tag: origin '{origin_tag}' does not exist in the taxonomy",
)
origins_taxonomy_mapping = _cached_create_taxonomy_mapping(
origins_taxonomy
)
try:
origins_mapped_tags = map_to_canonical_id(
origins_taxonomy_mapping, self.origins_tags
)
except ValueError as e:
# The value is not language-prefixed
validation_errors = utils.add_validation_error(
validation_errors,
"origins_tags",
str(e),
)
else:
self.origins_tags = list(origins_mapped_tags.values())
else:
validation_errors = utils.add_validation_error(
validation_errors,
Expand Down
21 changes: 20 additions & 1 deletion open_prices/prices/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,10 @@ def test_price_without_product_validation(self):
PriceFactory,
product_code=None,
category_tag="en:tomatoes",
labels_tags=["en:organic", "test"], # not valid
labels_tags=[
"en:organic",
"test",
], # not valid, no lang prefix before 'test'
price=3,
price_per=price_constants.PRICE_PER_KILOGRAM,
)
Expand Down Expand Up @@ -197,6 +200,22 @@ def test_price_category_validation(self):
)
self.assertEqual(price.category_tag, expected_category)

def test_price_origin_validation(self):
for input_origin_tags, expected_origin_tags in [
(["en:France"], ["en:france"]),
(["fr:Allemagne"], ["en:germany"]),
(["de:Deutschland", "es: España"], ["en:germany", "en:spain"]),
(["fr: Fairyland"], ["fr:fairyland"]),
]:
price = PriceFactory(
product_code=None,
category_tag="en:tomatoes",
origins_tags=input_origin_tags,
price=3,
price_per=price_constants.PRICE_PER_KILOGRAM,
)
self.assertEqual(price.origins_tags, expected_origin_tags)

def test_price_price_validation(self):
for PRICE_OK in [5, 0]:
PriceFactory(price=PRICE_OK)
Expand Down

0 comments on commit 2b867db

Please sign in to comment.