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

feat: Adding Knowledge Panel for Last-edits #13

Closed
wants to merge 18 commits into from
Closed
47 changes: 46 additions & 1 deletion app/knowledge_panels.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import json
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,
Expand Down Expand Up @@ -39,3 +44,43 @@ def hunger_game_kp(
],
},
}


def last_edits_kp(facet: str, value: str):
if facet == "packaging":
pural_facet = facet
sumit-158 marked this conversation as resolved.
Show resolved Hide resolved
# That how the api read packaging
else:
pural_facet = p.plural(facet)
"""
Changing other facet to pural because that how the search api works
sumit-158 marked this conversation as resolved.
Show resolved Hide resolved
"""
query = {}
if value is not None:
query[f"{pural_facet}_tags_en"] = value
sumit-158 marked this conversation as resolved.
Show resolved Hide resolved
query["fields"] = "code,product_name,last_editor"
sumit-158 marked this conversation as resolved.
Show resolved Hide resolved
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)
sumit-158 marked this conversation as resolved.
Show resolved Hide resolved
data = response_API.text
parse_json = json.loads(data)
counts = parse_json["count"]
tags = parse_json["products"]
sumit-158 marked this conversation as resolved.
Show resolved Hide resolved
first_three = tags[0:3]
"""Parsing data """

return {
"last-edits": {
"elements": [
{
"element_type": "text",
"counts": counts,
"text_element": first_three,
sumit-158 marked this conversation as resolved.
Show resolved Hide resolved
"source_url": search_url,
},
],
},
}
8 changes: 7 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -30,5 +30,11 @@ def knowledge_panel(
hunger_game_filter=facet_name, value=facet_value, country=country
)
)
"""Appending Hunger-game-knowlege-panel
sumit-158 marked this conversation as resolved.
Show resolved Hide resolved
"""

if facet_value is not None:
panels.append(last_edits_kp(facet=facet_name, value=facet_value))
"""Appending Last-edits-knowlede-panel"""
sumit-158 marked this conversation as resolved.
Show resolved Hide resolved

return {"knowledge_panels": panels}
Binary file modified requirements.txt
Binary file not shown.
29 changes: 28 additions & 1 deletion tests/test_knowledge_panels.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down Expand Up @@ -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",
}
]
}
}
30 changes: 27 additions & 3 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_knowledge_panel_badendpoint():

def test_knowledge_panel_ctegory_with_value_and_country():
sumit-158 marked this conversation as resolved.
Show resolved Hide resolved
assert knowledge_panel(
facet_name="category", facet_value="chocolate", country="belgium"
facet_name="category", facet_value="chocolates", country="belgium"
) == {
"knowledge_panels": [
{
Expand All @@ -39,12 +39,36 @@ def test_knowledge_panel_ctegory_with_value_and_country():
{
"element_type": "text",
"text_element": {
"html": "<p><a href='https://hunger.openfoodfacts.org/questions?country=belgium&type=category&value_tag=chocolate'>Answer robotoff questions about chocolate category</a></p>\n"
"html": "<p><a href='https://hunger.openfoodfacts.org/questions?country=belgium&type=category&value_tag=chocolates'>Answer robotoff questions about chocolates category</a></p>\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",
}
]
}
},
]
}

Expand Down