From 49f48fa5fa8adb0b5874272aac2b4c93592192d2 Mon Sep 17 00:00:00 2001 From: Sumit Kashyap Date: Tue, 19 Jul 2022 17:03:19 +0530 Subject: [PATCH 01/18] Adding Knowledge Panel for Last-edits --- app/knowledge_panels.py | 47 +++++++++++++++++++++++++++++++++- app/main.py | 8 +++++- tests/test_knowledge_panels.py | 29 ++++++++++++++++++++- tests/test_main.py | 30 +++++++++++++++++++--- 4 files changed, 108 insertions(+), 6 deletions(-) diff --git a/app/knowledge_panels.py b/app/knowledge_panels.py index d4a2a8c..e0eab8e 100644 --- a/app/knowledge_panels.py +++ b/app/knowledge_panels.py @@ -1,7 +1,9 @@ +import json from typing import Union - from urllib.parse import urlencode +import requests + from .models import HungerGameFilter @@ -39,3 +41,46 @@ def hunger_game_kp( ], }, } + + +def last_edits_kp(facet: str, value: str): + list = ["country", "category"] + if facet in list: + word = facet[:-1] + pural_facet = word + "ies" + elif facet == "packaging": + pural_facet = facet + else: + pural_facet = facet + "s" + """ + Changing facet to pural because that how the search api works + """ + query = {} + if value is not None: + query[f"{pural_facet}_tags_en"] = value + query["fields"] = "code,product_name,last_editor" + query["sort_by"] = "last_modified_t" + search_url = "https://world.openfoodfacts.org/api/v2/search" + if query: + search_url += f"?{urlencode(query)}" + + response_API = requests.get(search_url) + data = response_API.text + parse_json = json.loads(data) + counts = parse_json["count"] + tags = parse_json["products"] + first_three = tags[0:3] + """Parsing data """ + + return { + "last-edits": { + "elements": [ + { + "element_type": "text", + "total_issues": counts, + "text_element": first_three, + "source_url": search_url, + }, + ], + }, + } diff --git a/app/main.py b/app/main.py index 1ee40ef..b72e3a9 100644 --- a/app/main.py +++ b/app/main.py @@ -2,7 +2,7 @@ from fastapi import FastAPI -from .knowledge_panels import hunger_game_kp +from .knowledge_panels import hunger_game_kp, last_edits_kp from .models import FacetName, HungerGameFilter app = FastAPI() @@ -30,5 +30,11 @@ def knowledge_panel( hunger_game_filter=facet_name, value=facet_value, country=country ) ) + """Appending Hunger-game-knowlege-panel + """ + + if facet_value is not None: + panels.append(last_edits_kp(facet=facet_name, value=facet_value)) + """Appending Last-edits-knowlede-panel""" return {"knowledge_panels": panels} diff --git a/tests/test_knowledge_panels.py b/tests/test_knowledge_panels.py index e4c6d9a..4fad964 100644 --- a/tests/test_knowledge_panels.py +++ b/tests/test_knowledge_panels.py @@ -1,4 +1,4 @@ -from app.main import hunger_game_kp +from app.main import hunger_game_kp, last_edits_kp def test_hunger_game_kp_with_filter_value_and_country(): @@ -91,3 +91,30 @@ def test_hunger_game_kp_label_with_value(): ] } } + + +def test_last_edits_kp_with_country(): + assert last_edits_kp(facet="country", value="uganda") == { + "last-edits": { + "elements": [ + { + "element_type": "text", + "total_issues": 10, + "text_element": [ + {"code": "0212842620692", "last_editor": "smoothie-app"}, + { + "code": "90370755", + "last_editor": "kiliweb", + "product_name": "Fanta", + }, + { + "code": "8901086260918", + "last_editor": "packbot", + "product_name": "Herbal Cough Remedy", + }, + ], + "source_url": "https://world.openfoodfacts.org/api/v2/search?countries_tags_en=uganda&fields=code%2Cproduct_name%2Clast_editor&sort_by=last_modified_t", + } + ] + } + } diff --git a/tests/test_main.py b/tests/test_main.py index e9e0723..5163302 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -30,7 +30,7 @@ def test_knowledge_panel_badendpoint(): def test_knowledge_panel_ctegory_with_value_and_country(): assert knowledge_panel( - facet_name="category", facet_value="chocolate", country="belgium" + facet_name="category", facet_value="chocolates", country="belgium" ) == { "knowledge_panels": [ { @@ -39,12 +39,36 @@ def test_knowledge_panel_ctegory_with_value_and_country(): { "element_type": "text", "text_element": { - "html": "

Answer robotoff questions about chocolate category

\n" + "html": "

Answer robotoff questions about chocolates category

\n" }, } ] } - } + }, + { + "last-edits": { + "elements": [ + { + "element_type": "text", + "total_issues": 17028, + "text_element": [ + {"code": "3664346305860", "last_editor": "jul45"}, + { + "code": "5201127034724", + "last_editor": "ayyyvocado", + "product_name": "Ион Дарк Шок. 72 % Какао и Цели Бадеми", + }, + { + "code": "3560071265564", + "last_editor": "kiliweb", + "product_name": "Chocolat noir noisettes entières", + }, + ], + "source_url": "https://world.openfoodfacts.org/api/v2/search?categories_tags_en=chocolates&fields=code%2Cproduct_name%2Clast_editor&sort_by=last_modified_t", + } + ] + } + }, ] } From 2c0a8bed9afef3ee24fa9d23e542caad3c1d9954 Mon Sep 17 00:00:00 2001 From: Sumit Kashyap Date: Tue, 19 Jul 2022 17:19:08 +0530 Subject: [PATCH 02/18] Update knowledge_panels.py --- app/knowledge_panels.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/knowledge_panels.py b/app/knowledge_panels.py index e0eab8e..ef4ac23 100644 --- a/app/knowledge_panels.py +++ b/app/knowledge_panels.py @@ -77,7 +77,7 @@ def last_edits_kp(facet: str, value: str): "elements": [ { "element_type": "text", - "total_issues": counts, + "counts": counts, "text_element": first_three, "source_url": search_url, }, From e1f54f6da054db93811b8149633eaf0717c2f5f8 Mon Sep 17 00:00:00 2001 From: Sumit Kashyap Date: Tue, 19 Jul 2022 18:31:20 +0530 Subject: [PATCH 03/18] update knowlege_panel.py --- app/knowledge_panels.py | 14 +++++++------- requirements.txt | Bin 1002 -> 1076 bytes 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/knowledge_panels.py b/app/knowledge_panels.py index ef4ac23..53230c3 100644 --- a/app/knowledge_panels.py +++ b/app/knowledge_panels.py @@ -2,10 +2,13 @@ from typing import Union from urllib.parse import urlencode +import inflect import requests from .models import HungerGameFilter +p = inflect.engine() + def hunger_game_kp( hunger_game_filter: HungerGameFilter, @@ -44,16 +47,13 @@ def hunger_game_kp( def last_edits_kp(facet: str, value: str): - list = ["country", "category"] - if facet in list: - word = facet[:-1] - pural_facet = word + "ies" - elif facet == "packaging": + if facet == "packaging": pural_facet = facet + # That how the api read packaging else: - pural_facet = facet + "s" + pural_facet = p.plural(facet) """ - Changing facet to pural because that how the search api works + Changing other facet to pural because that how the search api works """ query = {} if value is not None: diff --git a/requirements.txt b/requirements.txt index 0cbb721d8645b5e6361e411a131c67fcbc9c6698..95fc8e626262e345462511a66a5bdfce11a4db4f 100644 GIT binary patch delta 79 zcmaFGzJ+5$4WoD(Lk>eKLo!1NgDnu6GUzdw0kP5MV#abNt#XD;hCGH8hJ1!{hGGU? Xpt4eiA|NXjh(XGYfJ%*l*nj~5!l4g! delta 12 TcmdnO@rr#z4ddoHjCo7|A@Ky8 From 2750afe2510ffd4f16d9affde8d7159bca78e8b2 Mon Sep 17 00:00:00 2001 From: Sumit Kashyap Date: Fri, 22 Jul 2022 18:51:16 +0530 Subject: [PATCH 04/18] Update knowledge_panels.py --- app/knowledge_panels.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/knowledge_panels.py b/app/knowledge_panels.py index 53230c3..e99eef2 100644 --- a/app/knowledge_panels.py +++ b/app/knowledge_panels.py @@ -48,16 +48,16 @@ def hunger_game_kp( def last_edits_kp(facet: str, value: str): if facet == "packaging": - pural_facet = facet + plural_facet = facet # That how the api read packaging else: - pural_facet = p.plural(facet) + plural_facet = p.plural(facet) """ - Changing other facet to pural because that how the search api works + Changing other facet to plural because that how the search api works """ query = {} if value is not None: - query[f"{pural_facet}_tags_en"] = value + query[f"{plural_facet}_tags_en"] = value query["fields"] = "code,product_name,last_editor" query["sort_by"] = "last_modified_t" search_url = "https://world.openfoodfacts.org/api/v2/search" From fd7fa8abe767b60590050ecfac8ef4a71271b0ef Mon Sep 17 00:00:00 2001 From: Sumit Kashyap Date: Fri, 22 Jul 2022 18:52:06 +0530 Subject: [PATCH 05/18] Update app/main.py Co-authored-by: Pierre Slamich --- app/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index b72e3a9..d139fc7 100644 --- a/app/main.py +++ b/app/main.py @@ -35,6 +35,6 @@ def knowledge_panel( if facet_value is not None: panels.append(last_edits_kp(facet=facet_name, value=facet_value)) - """Appending Last-edits-knowlede-panel""" + """Appending last-edits-knowledge-panel""" return {"knowledge_panels": panels} From 9e6c17ce70275c03534454fe4312f4f9006c02aa Mon Sep 17 00:00:00 2001 From: Sumit Kashyap Date: Fri, 22 Jul 2022 18:52:43 +0530 Subject: [PATCH 06/18] Update app/main.py Co-authored-by: Pierre Slamich --- app/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index d139fc7..7444ce4 100644 --- a/app/main.py +++ b/app/main.py @@ -30,7 +30,7 @@ def knowledge_panel( hunger_game_filter=facet_name, value=facet_value, country=country ) ) - """Appending Hunger-game-knowlege-panel + """Appending hunger-game-knowledge-panel """ if facet_value is not None: From 1dba1b2ef14d3adcbcdfe7cd24837687d5849f03 Mon Sep 17 00:00:00 2001 From: Sumit Kashyap Date: Mon, 25 Jul 2022 14:02:27 +0530 Subject: [PATCH 07/18] refractor code --- app/knowledge_panels.py | 64 +++++++++++++++++++++++------------------ app/models.py | 15 +++++++++- 2 files changed, 50 insertions(+), 29 deletions(-) diff --git a/app/knowledge_panels.py b/app/knowledge_panels.py index e99eef2..e903b90 100644 --- a/app/knowledge_panels.py +++ b/app/knowledge_panels.py @@ -2,12 +2,9 @@ from typing import Union from urllib.parse import urlencode -import inflect import requests -from .models import HungerGameFilter - -p = inflect.engine() +from .models import HungerGameFilter, country_to_ISO_code, facet_plural def hunger_game_kp( @@ -46,31 +43,41 @@ def hunger_game_kp( } -def last_edits_kp(facet: str, value: str): +def last_edits_kp( + facet: str, + value: Union[str, None] = None, + country: Union[str, None] = None, +): if facet == "packaging": - plural_facet = facet - # That how the api read packaging + plural = facet else: - plural_facet = p.plural(facet) - """ - Changing other facet to plural because that how the search api works - """ - query = {} - if value is not None: - query[f"{plural_facet}_tags_en"] = value - query["fields"] = "code,product_name,last_editor" - query["sort_by"] = "last_modified_t" - search_url = "https://world.openfoodfacts.org/api/v2/search" - if query: - search_url += f"?{urlencode(query)}" + plural = facet_plural(facet=facet) + query = { + "fields": "code,last_editor,last_modified_t", + "sort_by": "last_modified_t", + } + if facet == "country": + country_code = country_to_ISO_code(value=value) + country = value + url = f"https://{country_code}-en.openfoodfacts.org" + else: + if value is not None: + url = "https://world.openfoodfacts.org" + query[f"{plural}_tags_en"] = value + if country is not None: + country_code = country_to_ISO_code(value=country) + url = f"https://{country_code}-en.openfoodfacts.org" - response_API = requests.get(search_url) - data = response_API.text - parse_json = json.loads(data) - counts = parse_json["count"] - tags = parse_json["products"] - first_three = tags[0:3] - """Parsing data """ + search_url = f"{url}/api/v2/search" + response_API = requests.get(search_url, params=query) + data = response_API.json() + counts = data["count"] + tags = data["products"] + html = "\n".join( + f'
  • Product with this code {tag["code"]} was last edited by {tag["last_editor"]} has last_modified_tag {tag["last_modified_t"]}
  • ' + for tag in tags[0:3] + ) + html = f"
      {html}
    " return { "last-edits": { @@ -78,8 +85,9 @@ def last_edits_kp(facet: str, value: str): { "element_type": "text", "counts": counts, - "text_element": first_three, - "source_url": search_url, + "text_element": html, + "total_edits": counts, + "source_url": f"{url}/{facet}/{value}?sort_by=last_modified_t", }, ], }, diff --git a/app/models.py b/app/models.py index 7263152..50532b4 100644 --- a/app/models.py +++ b/app/models.py @@ -1,5 +1,6 @@ from enum import Enum - +import inflect +import pycountry from pydantic import BaseModel @@ -38,3 +39,15 @@ class HungerGameFilter(str, Enum): @staticmethod def list(): return [c.value for c in HungerGameFilter] + + +def facet_plural(facet): + p = inflect.engine() + facet_plural = p.plural(facet) + return facet_plural + + +def country_to_ISO_code(value: str): + country_data = pycountry.countries.get(name=value) + country_iso_code = country_data.alpha_2 + return country_iso_code.lower() From 43003638baf52d85536ef69e1886f97a8169fa19 Mon Sep 17 00:00:00 2001 From: Sumit Kashyap Date: Mon, 25 Jul 2022 15:53:28 +0530 Subject: [PATCH 08/18] refactored code --- app/knowledge_panels.py | 5 +++++ app/main.py | 31 ++++++++++++++++++------------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/app/knowledge_panels.py b/app/knowledge_panels.py index e903b90..3632b93 100644 --- a/app/knowledge_panels.py +++ b/app/knowledge_panels.py @@ -60,15 +60,19 @@ def last_edits_kp( country_code = country_to_ISO_code(value=value) country = value url = f"https://{country_code}-en.openfoodfacts.org" + description = f"{value}" else: if value is not None: url = "https://world.openfoodfacts.org" query[f"{plural}_tags_en"] = value + description = f"{value} based for {facet}" if country is not None: country_code = country_to_ISO_code(value=country) url = f"https://{country_code}-en.openfoodfacts.org" + description = f"{facet} based for {country}" search_url = f"{url}/api/v2/search" + description = f"last-edits issues related to {description}" response_API = requests.get(search_url, params=query) data = response_API.json() counts = data["count"] @@ -88,6 +92,7 @@ def last_edits_kp( "text_element": html, "total_edits": counts, "source_url": f"{url}/{facet}/{value}?sort_by=last_modified_t", + "description": f"This is {description}", }, ], }, diff --git a/app/main.py b/app/main.py index 7444ce4..7829ec1 100644 --- a/app/main.py +++ b/app/main.py @@ -1,6 +1,6 @@ from typing import Union -from fastapi import FastAPI +from fastapi import FastAPI, HTTPException from .knowledge_panels import hunger_game_kp, last_edits_kp from .models import FacetName, HungerGameFilter @@ -23,18 +23,23 @@ def knowledge_panel( ): # FacetName is the model that have list of values # facet_value are the list of values connecting to FacetName eg:- category/beer, here beer is the value - panels = [] - if facet_name in HungerGameFilter.list(): - panels.append( - hunger_game_kp( - hunger_game_filter=facet_name, value=facet_value, country=country + try: + panels = [] + if facet_name in HungerGameFilter.list(): + panels.append( + hunger_game_kp( + hunger_game_filter=facet_name, value=facet_value, country=country + ) ) - ) - """Appending hunger-game-knowledge-panel - """ + """Appending hunger-game-knowledge-panel + """ - if facet_value is not None: - panels.append(last_edits_kp(facet=facet_name, value=facet_value)) - """Appending last-edits-knowledge-panel""" + if facet_value is not None: + panels.append( + last_edits_kp(facet=facet_name, value=facet_value, country=country) + ) + """Appending last-edits-knowledge-panel""" - return {"knowledge_panels": panels} + return {"knowledge_panels": panels} + except: + raise HTTPException(status_code=404, detail="Item not found") From 869dcf31b852698ab51b6cf4d42d7b652a834a19 Mon Sep 17 00:00:00 2001 From: Sumit Kashyap Date: Mon, 25 Jul 2022 17:59:01 +0530 Subject: [PATCH 09/18] Update knowledge_panels.py --- app/knowledge_panels.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/knowledge_panels.py b/app/knowledge_panels.py index 3632b93..9313dba 100644 --- a/app/knowledge_panels.py +++ b/app/knowledge_panels.py @@ -79,7 +79,7 @@ def last_edits_kp( tags = data["products"] html = "\n".join( f'
  • Product with this code {tag["code"]} was last edited by {tag["last_editor"]} has last_modified_tag {tag["last_modified_t"]}
  • ' - for tag in tags[0:3] + for tag in tags[0:10] ) html = f"
      {html}
    " From 9e76e4974555922ccff4519d562defdb49daecfa Mon Sep 17 00:00:00 2001 From: Sumit Kashyap Date: Wed, 27 Jul 2022 12:12:17 +0530 Subject: [PATCH 10/18] Added off.py --- app/knowledge_panels.py | 59 ++++++++++++++++++----------------------- app/models.py | 8 ++++-- app/off.py | 21 +++++++++++++++ 3 files changed, 53 insertions(+), 35 deletions(-) create mode 100644 app/off.py diff --git a/app/knowledge_panels.py b/app/knowledge_panels.py index 9313dba..59f048f 100644 --- a/app/knowledge_panels.py +++ b/app/knowledge_panels.py @@ -1,3 +1,4 @@ +import imp import json from typing import Union from urllib.parse import urlencode @@ -5,6 +6,7 @@ import requests from .models import HungerGameFilter, country_to_ISO_code, facet_plural +from .off import lastEdit def hunger_game_kp( @@ -48,51 +50,42 @@ def last_edits_kp( value: Union[str, None] = None, country: Union[str, None] = None, ): - if facet == "packaging": - plural = facet - else: - plural = facet_plural(facet=facet) + """ + Return knowledge panel for last-edits corresponding to differnet facet + """ query = { - "fields": "code,last_editor,last_modified_t", + "fields": "product_name,code,last_editor,last_edit_dates_tags", "sort_by": "last_modified_t", } + description = "" if facet == "country": - country_code = country_to_ISO_code(value=value) country = value + country_code = country_to_ISO_code(value=value) url = f"https://{country_code}-en.openfoodfacts.org" - description = f"{value}" - else: - if value is not None: - url = "https://world.openfoodfacts.org" - query[f"{plural}_tags_en"] = value - description = f"{value} based for {facet}" - if country is not None: - country_code = country_to_ISO_code(value=country) - url = f"https://{country_code}-en.openfoodfacts.org" - description = f"{facet} based for {country}" - - search_url = f"{url}/api/v2/search" + facet = value = None + if country is not None: + country_code = country_to_ISO_code(value=country) + url = f"https://{country_code}-en.openfoodfacts.org" + description += country + if country is None: + url = "https://world.openfoodfacts.org" + if facet is not None: + description += f" {facet}" + if value is not None: + query[f"{facet_plural(facet=facet)}_tags_en"] = value + description += f" {value}" description = f"last-edits issues related to {description}" - response_API = requests.get(search_url, params=query) - data = response_API.json() - counts = data["count"] - tags = data["products"] - html = "\n".join( - f'
  • Product with this code {tag["code"]} was last edited by {tag["last_editor"]} has last_modified_tag {tag["last_modified_t"]}
  • ' - for tag in tags[0:10] - ) - html = f"
      {html}
    " + expected_html = lastEdit(url=url, query=query) return { - "last-edits": { + "LastEdits": { + "title": "Last-edites", + "subtitle": f"{description}", + "source_url": f"{url}/{facet}/{value}?sort_by=last_modified_t", "elements": [ { "element_type": "text", - "counts": counts, - "text_element": html, - "total_edits": counts, - "source_url": f"{url}/{facet}/{value}?sort_by=last_modified_t", - "description": f"This is {description}", + "text_element": expected_html, }, ], }, diff --git a/app/models.py b/app/models.py index 50532b4..1c2a461 100644 --- a/app/models.py +++ b/app/models.py @@ -41,9 +41,13 @@ def list(): return [c.value for c in HungerGameFilter] -def facet_plural(facet): +def facet_plural(facet: str): p = inflect.engine() - facet_plural = p.plural(facet) + plural = p.plural(facet) + if facet == "packaging": + facet_plural = facet + else: + facet_plural = plural return facet_plural diff --git a/app/off.py b/app/off.py new file mode 100644 index 0000000..bf51550 --- /dev/null +++ b/app/off.py @@ -0,0 +1,21 @@ +import requests + + +def lastEdit(url, query): + """ + Helper function to return data for last-edits + """ + search_url = f"{url}/api/v2/search" + response_API = requests.get(search_url, params=query) + data = response_API.json() + counts = data["count"] + tags = data["products"] + + html = "\n".join( + f'
  • {tag["product_name"]} ({tag["code"]}) edited by {tag["last_editor"]} on {tag["last_edit_dates_tags"][0]}
  • ' + for tag in tags[0:10] + ) + + html = f"

      Total number of edits {counts}

      \n {html}
    " + + return html From 816a45691ca90860e4264e889107c8f22b1b8951 Mon Sep 17 00:00:00 2001 From: Sumit Kashyap Date: Wed, 27 Jul 2022 12:35:27 +0530 Subject: [PATCH 11/18] Update off.py --- app/off.py | 1 + 1 file changed, 1 insertion(+) diff --git a/app/off.py b/app/off.py index bf51550..5ff12b9 100644 --- a/app/off.py +++ b/app/off.py @@ -14,6 +14,7 @@ def lastEdit(url, query): html = "\n".join( f'
  • {tag["product_name"]} ({tag["code"]}) edited by {tag["last_editor"]} on {tag["last_edit_dates_tags"][0]}
  • ' for tag in tags[0:10] + if "product_name" in tag ) html = f"

      Total number of edits {counts}

      \n {html}
    " From 31cac9a5eec19b3bb6d969a0523596fde2e3de90 Mon Sep 17 00:00:00 2001 From: Sumit Kashyap Date: Thu, 28 Jul 2022 17:01:04 +0530 Subject: [PATCH 12/18] small fix(comments , unused imports) --- app/knowledge_panels.py | 7 +------ app/models.py | 11 +++++++++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/app/knowledge_panels.py b/app/knowledge_panels.py index 59f048f..1a346cf 100644 --- a/app/knowledge_panels.py +++ b/app/knowledge_panels.py @@ -1,10 +1,5 @@ -import imp -import json from typing import Union from urllib.parse import urlencode - -import requests - from .models import HungerGameFilter, country_to_ISO_code, facet_plural from .off import lastEdit @@ -51,7 +46,7 @@ def last_edits_kp( country: Union[str, None] = None, ): """ - Return knowledge panel for last-edits corresponding to differnet facet + Return knowledge panel for last-edits corresponding to different facet """ query = { "fields": "product_name,code,last_editor,last_edit_dates_tags", diff --git a/app/models.py b/app/models.py index 1c2a461..5f1bf00 100644 --- a/app/models.py +++ b/app/models.py @@ -1,5 +1,9 @@ from enum import Enum import inflect + +"""Library to correctly generate plurals, singular nouns, ordinals, indefinite articles convert numbers to words. +https://pypi.org/project/inflect/ """ + import pycountry from pydantic import BaseModel @@ -42,12 +46,15 @@ def list(): def facet_plural(facet: str): + """ + Return plural of facet + """ p = inflect.engine() plural = p.plural(facet) + facet_plural = plural if facet == "packaging": facet_plural = facet - else: - facet_plural = plural + return facet_plural From 89415672e637f7442fa1b2942c395bd247a50c57 Mon Sep 17 00:00:00 2001 From: Sumit Kashyap Date: Thu, 28 Jul 2022 17:47:00 +0530 Subject: [PATCH 13/18] update tests --- tests/test_knowledge_panels.py | 65 ++++++++++++++++++++++++---------- tests/test_utils.py | 21 +++++++++++ 2 files changed, 67 insertions(+), 19 deletions(-) create mode 100644 tests/test_utils.py diff --git a/tests/test_knowledge_panels.py b/tests/test_knowledge_panels.py index 4fad964..50cdbec 100644 --- a/tests/test_knowledge_panels.py +++ b/tests/test_knowledge_panels.py @@ -1,4 +1,8 @@ +from ast import Param from app.main import hunger_game_kp, last_edits_kp +import requests +import app.main +from .test_utils import mock_get_factory def test_hunger_game_kp_with_filter_value_and_country(): @@ -93,28 +97,51 @@ def test_hunger_game_kp_label_with_value(): } -def test_last_edits_kp_with_country(): - assert last_edits_kp(facet="country", value="uganda") == { - "last-edits": { +def test_last_edits_kp_with_all_three_values(monkeypatch): + expected_url = "https://fr-en.openfoodfacts.org/api/v2/search" + expected_json = { + "count": 13552, + "page": 1, + "page_count": 24, + "page_size": 24, + "products": [ + { + "code": "7624841856058", + "last_edit_dates_tags": ["2022-07-27", "2022-07", "2022"], + "last_editor": "gluten-scan", + "product_name": "Red thai curry soup", + }, + { + "code": "8712566429271", + "last_edit_dates_tags": ["2022-07-27", "2022-07", "2022"], + "last_editor": "org-unilever-france-gms", + "product_name": "Knorr Soupe Liquide Mouliné de Légumes d'Autrefois 1L", + }, + { + "code": "4023900542803", + "last_edit_dates_tags": ["2022-07-26", "2022-07", "2022"], + "last_editor": "prepperapp", + "product_name": "Bio Soja Souce", + }, + ], + } + monkeypatch.setattr( + requests, + "get", + mock_get_factory(expected_url, expected_json), + ) + result = app.main.last_edits_kp(facet="label", value="vegan", country="france") + + assert result == { + "LastEdits": { + "title": "Last-edites", + "subtitle": "last-edits issues related to france label vegan", + "source_url": "https://fr-en.openfoodfacts.org/label/vegan?sort_by=last_modified_t", "elements": [ { "element_type": "text", - "total_issues": 10, - "text_element": [ - {"code": "0212842620692", "last_editor": "smoothie-app"}, - { - "code": "90370755", - "last_editor": "kiliweb", - "product_name": "Fanta", - }, - { - "code": "8901086260918", - "last_editor": "packbot", - "product_name": "Herbal Cough Remedy", - }, - ], - "source_url": "https://world.openfoodfacts.org/api/v2/search?countries_tags_en=uganda&fields=code%2Cproduct_name%2Clast_editor&sort_by=last_modified_t", + "text_element": "

      Total number of edits 13552

      \n
    • Red thai curry soup (7624841856058) edited by gluten-scan on 2022-07-27
    • \n
    • Knorr Soupe Liquide Mouliné de Légumes d'Autrefois 1L (8712566429271) edited by org-unilever-france-gms on 2022-07-27
    • \n
    • Bio Soja Souce (4023900542803) edited by prepperapp on 2022-07-26
    • \n
    • Berry Revolutionary (8711327539303) edited by kiliweb on 2022-07-26
    • \n
    • Gelées con succhi Di frutta (8016042021011) edited by kiliweb on 2022-07-26
    • \n
    • Minifrühlingsrollen mit Gemüse (4316268371421) edited by blaerf on 2022-07-26
    • \n
    • Chicorée 0% Caffeine (5411788047166) edited by kiliweb on 2022-07-26
    • \n
    • Soy sauce (8715035110502) edited by frazerclews on 2022-07-26
    • \n
    • Sables pépites de chocolat (3760154260619) edited by kiliweb on 2022-07-26
    • \n
    • Raw pasta Fettuccine (8718868683878) edited by kiliweb on 2022-07-26
    ", } - ] + ], } } diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 0000000..31450e6 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,21 @@ +class MockResponse: + def __init__(self, json_content): + self.json_content = json_content + + def json(self): + return self.json_content + + +def mock_get_factory(target_url, json_content): + """generate a mock to patch request.get with a json response""" + + def mock_get(url, **kwargs): + assert url == target_url + assert kwargs == { + "fields": "product_name,code,last_editor,last_edit_dates_tags", + "sort_by": "last_modified_t", + "labels_tags_en": "vegan", + } + return MockResponse(json_content) + + return mock_get From 436412e6d7c24d4e806e9b17f0f33b14c53f6103 Mon Sep 17 00:00:00 2001 From: Sumit Kashyap Date: Thu, 28 Jul 2022 20:01:00 +0530 Subject: [PATCH 14/18] updated tests --- tests/test_knowledge_panels.py | 49 +++++++++++++++++----------------- tests/test_utils.py | 8 ++---- 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/tests/test_knowledge_panels.py b/tests/test_knowledge_panels.py index 50cdbec..7ec36df 100644 --- a/tests/test_knowledge_panels.py +++ b/tests/test_knowledge_panels.py @@ -98,49 +98,48 @@ def test_hunger_game_kp_label_with_value(): def test_last_edits_kp_with_all_three_values(monkeypatch): - expected_url = "https://fr-en.openfoodfacts.org/api/v2/search" + expected_url = "https://hu-en.openfoodfacts.org/api/v2/search" + expected_kwargs = { + "fields": "product_name,code,last_editor,last_edit_dates_tags", + "sort_by": "last_modified_t", + "vitamins_tags_en": "vitamin-k", + } expected_json = { - "count": 13552, + "count": 1, "page": 1, - "page_count": 24, + "page_count": 1, "page_size": 24, "products": [ { - "code": "7624841856058", - "last_edit_dates_tags": ["2022-07-27", "2022-07", "2022"], - "last_editor": "gluten-scan", - "product_name": "Red thai curry soup", - }, - { - "code": "8712566429271", - "last_edit_dates_tags": ["2022-07-27", "2022-07", "2022"], - "last_editor": "org-unilever-france-gms", - "product_name": "Knorr Soupe Liquide Mouliné de Légumes d'Autrefois 1L", - }, - { - "code": "4023900542803", - "last_edit_dates_tags": ["2022-07-26", "2022-07", "2022"], - "last_editor": "prepperapp", - "product_name": "Bio Soja Souce", - }, + "code": "0715235567418", + "last_edit_dates_tags": ["2022-02-10", "2022-02", "2022"], + "last_editor": "packbot", + "product_name": "Tiqle Sticks Strawberry taste", + } ], } monkeypatch.setattr( requests, "get", - mock_get_factory(expected_url, expected_json), + mock_get_factory( + expected_url, + expected_kwargs, + expected_json, + ), + ) + result = app.main.last_edits_kp( + facet="vitamin", value="vitamin-k", country="hungary" ) - result = app.main.last_edits_kp(facet="label", value="vegan", country="france") assert result == { "LastEdits": { "title": "Last-edites", - "subtitle": "last-edits issues related to france label vegan", - "source_url": "https://fr-en.openfoodfacts.org/label/vegan?sort_by=last_modified_t", + "subtitle": "last-edits issues related to Hungary vitamin vitamin-k", + "source_url": "https://hu-en.openfoodfacts.org/vitamin/vitamin-k?sort_by=last_modified_t", "elements": [ { "element_type": "text", - "text_element": "

      Total number of edits 13552

      \n
    • Red thai curry soup (7624841856058) edited by gluten-scan on 2022-07-27
    • \n
    • Knorr Soupe Liquide Mouliné de Légumes d'Autrefois 1L (8712566429271) edited by org-unilever-france-gms on 2022-07-27
    • \n
    • Bio Soja Souce (4023900542803) edited by prepperapp on 2022-07-26
    • \n
    • Berry Revolutionary (8711327539303) edited by kiliweb on 2022-07-26
    • \n
    • Gelées con succhi Di frutta (8016042021011) edited by kiliweb on 2022-07-26
    • \n
    • Minifrühlingsrollen mit Gemüse (4316268371421) edited by blaerf on 2022-07-26
    • \n
    • Chicorée 0% Caffeine (5411788047166) edited by kiliweb on 2022-07-26
    • \n
    • Soy sauce (8715035110502) edited by frazerclews on 2022-07-26
    • \n
    • Sables pépites de chocolat (3760154260619) edited by kiliweb on 2022-07-26
    • \n
    • Raw pasta Fettuccine (8718868683878) edited by kiliweb on 2022-07-26
    ", + "text_element": "

      Total number of edits 1

      \n
    • Tiqle Sticks Strawberry taste (0715235567418) edited by packbot on 2022-02-10
    ", } ], } diff --git a/tests/test_utils.py b/tests/test_utils.py index 31450e6..0ee7e9c 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -6,16 +6,12 @@ def json(self): return self.json_content -def mock_get_factory(target_url, json_content): +def mock_get_factory(target_url, expected_kwargs={}, json_content=None): """generate a mock to patch request.get with a json response""" def mock_get(url, **kwargs): assert url == target_url - assert kwargs == { - "fields": "product_name,code,last_editor,last_edit_dates_tags", - "sort_by": "last_modified_t", - "labels_tags_en": "vegan", - } + assert kwargs == expected_kwargs return MockResponse(json_content) return mock_get From 02d1279586b163b560b390db6641797774605343 Mon Sep 17 00:00:00 2001 From: Sumit Kashyap Date: Thu, 28 Jul 2022 20:08:00 +0530 Subject: [PATCH 15/18] Updated test --- app/main.py | 36 +++++++++--------- tests/test_knowledge_panels.py | 10 +++-- tests/test_main.py | 68 ---------------------------------- 3 files changed, 24 insertions(+), 90 deletions(-) diff --git a/app/main.py b/app/main.py index 7829ec1..a25ae23 100644 --- a/app/main.py +++ b/app/main.py @@ -1,5 +1,5 @@ from typing import Union - +import logging from fastapi import FastAPI, HTTPException from .knowledge_panels import hunger_game_kp, last_edits_kp @@ -23,23 +23,23 @@ def knowledge_panel( ): # FacetName is the model that have list of values # facet_value are the list of values connecting to FacetName eg:- category/beer, here beer is the value - try: - panels = [] - if facet_name in HungerGameFilter.list(): - panels.append( - hunger_game_kp( - hunger_game_filter=facet_name, value=facet_value, country=country - ) + panels = [] + """ + Appending hunger-game-knowledge-panel + """ + if facet_name in HungerGameFilter.list(): + panels.append( + hunger_game_kp( + hunger_game_filter=facet_name, value=facet_value, country=country ) - """Appending hunger-game-knowledge-panel - """ + ) - if facet_value is not None: - panels.append( - last_edits_kp(facet=facet_name, value=facet_value, country=country) - ) - """Appending last-edits-knowledge-panel""" + """Appending last-edits-knowledge-panel""" + try: + panels.append( + last_edits_kp(facet=facet_name, value=facet_value, country=country) + ) + except Exception as Argument: + logging.exception("error occued while appending last-edits-kp") - return {"knowledge_panels": panels} - except: - raise HTTPException(status_code=404, detail="Item not found") + return {"knowledge_panels": panels} diff --git a/tests/test_knowledge_panels.py b/tests/test_knowledge_panels.py index 7ec36df..d181568 100644 --- a/tests/test_knowledge_panels.py +++ b/tests/test_knowledge_panels.py @@ -100,9 +100,11 @@ def test_hunger_game_kp_label_with_value(): def test_last_edits_kp_with_all_three_values(monkeypatch): expected_url = "https://hu-en.openfoodfacts.org/api/v2/search" expected_kwargs = { - "fields": "product_name,code,last_editor,last_edit_dates_tags", - "sort_by": "last_modified_t", - "vitamins_tags_en": "vitamin-k", + "params": { + "fields": "product_name,code,last_editor,last_edit_dates_tags", + "sort_by": "last_modified_t", + "vitamins_tags_en": "vitamin-k", + } } expected_json = { "count": 1, @@ -134,7 +136,7 @@ def test_last_edits_kp_with_all_three_values(monkeypatch): assert result == { "LastEdits": { "title": "Last-edites", - "subtitle": "last-edits issues related to Hungary vitamin vitamin-k", + "subtitle": "last-edits issues related to hungary vitamin vitamin-k", "source_url": "https://hu-en.openfoodfacts.org/vitamin/vitamin-k?sort_by=last_modified_t", "elements": [ { diff --git a/tests/test_main.py b/tests/test_main.py index 5163302..f86e666 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -26,71 +26,3 @@ def test_knowledge_panel(): def test_knowledge_panel_badendpoint(): response = client.get("/knowledge_panel_bad") assert response.status_code == 404 - - -def test_knowledge_panel_ctegory_with_value_and_country(): - assert knowledge_panel( - facet_name="category", facet_value="chocolates", country="belgium" - ) == { - "knowledge_panels": [ - { - "hunger-game": { - "elements": [ - { - "element_type": "text", - "text_element": { - "html": "

    Answer robotoff questions about chocolates category

    \n" - }, - } - ] - } - }, - { - "last-edits": { - "elements": [ - { - "element_type": "text", - "total_issues": 17028, - "text_element": [ - {"code": "3664346305860", "last_editor": "jul45"}, - { - "code": "5201127034724", - "last_editor": "ayyyvocado", - "product_name": "Ион Дарк Шок. 72 % Какао и Цели Бадеми", - }, - { - "code": "3560071265564", - "last_editor": "kiliweb", - "product_name": "Chocolat noir noisettes entières", - }, - ], - "source_url": "https://world.openfoodfacts.org/api/v2/search?categories_tags_en=chocolates&fields=code%2Cproduct_name%2Clast_editor&sort_by=last_modified_t", - } - ] - } - }, - ] - } - - -def test_knowledge_panel_ctegory_with_country(): - assert knowledge_panel(facet_name="category", country="india") == { - "knowledge_panels": [ - { - "hunger-game": { - "elements": [ - { - "element_type": "text", - "text_element": { - "html": "

    Answer robotoff questions about category

    \n" - }, - } - ] - } - } - ] - } - - -def test_knowledge_panel_with_allergen(): - assert knowledge_panel(facet_name="allergen") == {"knowledge_panels": []} From 51b7efb91dd0f06702980e554f0572f7a0eb2e4d Mon Sep 17 00:00:00 2001 From: Sumit Kashyap Date: Fri, 29 Jul 2022 15:19:13 +0530 Subject: [PATCH 16/18] Update test_main.py --- tests/test_main.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/test_main.py b/tests/test_main.py index f86e666..cfedafa 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -1,7 +1,6 @@ from curses import panel from urllib import response import json - from app.main import app, knowledge_panel from fastapi.testclient import TestClient @@ -19,8 +18,7 @@ def test_hello(): def test_knowledge_panel(): response = client.get("/knowledge_panel?facet_name=origin") assert response.status_code == 200 - response_body = response.json() - assert response_body["knowledge_panels"] == [] + assert response.json() def test_knowledge_panel_badendpoint(): From 8b4563dd40e89f8269b2d4c2970ac904c8ebda4a Mon Sep 17 00:00:00 2001 From: Sumit Kashyap Date: Fri, 29 Jul 2022 20:25:48 +0530 Subject: [PATCH 17/18] update tests --- tests/test_knowledge_panels.py | 19 ++++++++++++++++--- tests/test_utils.py | 11 +++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/tests/test_knowledge_panels.py b/tests/test_knowledge_panels.py index d181568..03e270b 100644 --- a/tests/test_knowledge_panels.py +++ b/tests/test_knowledge_panels.py @@ -2,7 +2,7 @@ from app.main import hunger_game_kp, last_edits_kp import requests import app.main -from .test_utils import mock_get_factory +from .test_utils import mock_get_factory, tidy_html def test_hunger_game_kp_with_filter_value_and_country(): @@ -132,7 +132,20 @@ def test_last_edits_kp_with_all_three_values(monkeypatch): result = app.main.last_edits_kp( facet="vitamin", value="vitamin-k", country="hungary" ) - + first_element = result["LastEdits"]["elements"][0] + first_element["text_element"] = tidy_html(first_element["text_element"]) + expected_text = """ +
      +

      Total number of edits 1

      +
    • + Tiqle Sticks Strawberry taste (0715235567418) edited by packbot on 2022-02-10 +
    • +
    + """ + # assert html separately to have better output in case of error + assert first_element["text_element"] == tidy_html(expected_text) + # now replace it for concision of output + first_element["text_element"] = "ok" assert result == { "LastEdits": { "title": "Last-edites", @@ -141,7 +154,7 @@ def test_last_edits_kp_with_all_three_values(monkeypatch): "elements": [ { "element_type": "text", - "text_element": "

      Total number of edits 1

      \n
    • Tiqle Sticks Strawberry taste (0715235567418) edited by packbot on 2022-02-10
    ", + "text_element": "ok", } ], } diff --git a/tests/test_utils.py b/tests/test_utils.py index 0ee7e9c..6ad3056 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,3 +1,6 @@ +from bs4 import BeautifulSoup + + class MockResponse: def __init__(self, json_content): self.json_content = json_content @@ -15,3 +18,11 @@ def mock_get(url, **kwargs): return MockResponse(json_content) return mock_get + + +def tidy_html(html): + """ + Helper function that return pretiffy html + """ + html = BeautifulSoup(html, "html.parser").prettify() + return html.strip() From 9cb585355e36750626aba8863c8622f11ee3b520 Mon Sep 17 00:00:00 2001 From: Sumit Kashyap Date: Fri, 29 Jul 2022 20:29:06 +0530 Subject: [PATCH 18/18] Update test_knowledge_panels.py --- tests/test_knowledge_panels.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_knowledge_panels.py b/tests/test_knowledge_panels.py index 03e270b..57530f2 100644 --- a/tests/test_knowledge_panels.py +++ b/tests/test_knowledge_panels.py @@ -134,7 +134,7 @@ def test_last_edits_kp_with_all_three_values(monkeypatch): ) first_element = result["LastEdits"]["elements"][0] first_element["text_element"] = tidy_html(first_element["text_element"]) - expected_text = """ + last_edits_text = """

      Total number of edits 1

    • @@ -143,7 +143,7 @@ def test_last_edits_kp_with_all_three_values(monkeypatch):
    """ # assert html separately to have better output in case of error - assert first_element["text_element"] == tidy_html(expected_text) + assert first_element["text_element"] == tidy_html(last_edits_text) # now replace it for concision of output first_element["text_element"] = "ok" assert result == {