Skip to content

Commit f64dbf3

Browse files
committed
add product type to pain report + embed in no fresh egg exception
1 parent 51faaf6 commit f64dbf3

6 files changed

Lines changed: 26 additions & 10 deletions

File tree

‎backend/app/business/open_food_facts/knowledge_panel.py‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
Panel,
2525
PanelElement,
2626
ProductInfo,
27+
ProductType,
2728
TextElement,
2829
TitleElement,
2930
)
@@ -158,8 +159,7 @@ async def get_pain_reports(barcode: str, locale: str) -> List[PainReport]:
158159
calculator = PainReportCalculator(product_data)
159160

160161
except EggButNotFreshEgg as e:
161-
return [PainReport(product_name=e.product_name, product_image_url=e.image_url)]
162-
162+
return [e.pain_report]
163163
# Generate and return the pain report
164164
# For no fresh chicken eggs return empty list to display a specific knowledge panel
165165
pain_reports = calculator.get_pain_reports()
@@ -219,6 +219,12 @@ def get_response(self) -> KnowledgePanelResponse:
219219
"""
220220
# Defining which detailed panels are to be displayed
221221

222+
# No result if the product is no egg
223+
if len(self.pain_reports) > 0 and self.pain_reports[0].product_type != ProductType(
224+
is_mixed=False, animal_types={AnimalType.LAYING_HEN}
225+
):
226+
raise ResourceNotFoundException(f"Product {self.pain_reports[0].product_name} is not a laying hen")
227+
222228
detailed_panels = ["project_panel"]
223229

224230
# root panel depending on pain report data and detailed panels
@@ -255,6 +261,7 @@ def _create_root_panel(self, detailed_panels: list[str]) -> Panel:
255261
# Initialize the panel with generic information message about the project
256262
elements = []
257263

264+
#
258265
if len(self.pain_reports) == 1 and self.pain_reports[0].animal_pain_reports == []:
259266
elements += self._create_element_from_html("no_fresh_egg.html")
260267

‎backend/app/business/open_food_facts/pain_report_calculator.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ def get_pain_reports(self) -> List[PainReport]:
9090
animal_pain_reports=[],
9191
product_name=self.product_data.product_name,
9292
product_image_url=self.product_data.image_url,
93+
product_type=self.product_type,
9394
)
9495
]
9596

@@ -116,6 +117,7 @@ def get_pain_reports(self) -> List[PainReport]:
116117
animal_pain_reports=[animal_report],
117118
product_name=self.product_data.product_name,
118119
product_image_url=self.product_data.image_url,
120+
product_type=self.product_type,
119121
)
120122
pain_reports.append(pain_report)
121123

‎backend/app/business/open_food_facts/product_type_calculator.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from app.enums.open_food_facts.enums import AnimalType
55
from app.enums.open_food_facts.product_type_enums import ProductTypePatternRepository
66
from app.schemas.open_food_facts.external import ProductData
7-
from app.schemas.open_food_facts.internal import ProductType
7+
from app.schemas.open_food_facts.internal import PainReport, ProductType
88

99

1010
def get_product_type(product_data: ProductData) -> ProductType:
@@ -28,7 +28,13 @@ def get_product_type(product_data: ProductData) -> ProductType:
2828

2929
# temp fix to display specific information for this kind of product
3030
else:
31-
raise EggButNotFreshEgg(product_name=product_data.product_name, image_url=product_data.image_url)
31+
raise EggButNotFreshEgg(
32+
pain_report=PainReport(
33+
product_name=product_data.product_name,
34+
product_image_url=product_data.image_url,
35+
product_type=ProductType(is_mixed=False, animal_types={AnimalType.LAYING_HEN}),
36+
)
37+
)
3238
else:
3339
animal_types.add(animal_type)
3440

‎backend/app/config/exceptions.py‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import logging
77

8-
from pydantic import HttpUrl
8+
from app.schemas.open_food_facts.internal import PainReport
99

1010
logger = logging.getLogger("app")
1111

@@ -52,10 +52,7 @@ class MissingBreedingTypeOrQuantityError(Exception):
5252

5353

5454
class EggButNotFreshEgg(Exception):
55-
def __init__(
56-
self, product_name: str | None = None, image_url: HttpUrl | None = None, message: str = "Egg but not fresh egg"
57-
):
58-
self.product_name = product_name
59-
self.image_url = image_url
55+
def __init__(self, pain_report: PainReport, message: str = "Egg but not fresh egg"):
6056
self.message = message
57+
self.pain_report = pain_report
6158
super().__init__(message)

‎backend/app/schemas/open_food_facts/internal.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class PainReport(BaseModel):
4747
animal_pain_reports: List[AnimalPainReport] = []
4848
product_name: str | None
4949
product_image_url: HttpUrl | None = None
50+
product_type: ProductType
5051

5152

5253
# Knowledge panel response models

‎frontend/app/[locale]/off/page.tsx‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ export default function KnowledgePanel() {
8989
'78742330808',
9090
'5051140152901',
9191
'3560071227241',
92+
'0030561400018',
93+
9294
];
9395

9496
const barcodeNames: { [key: string]: string } = {
@@ -115,6 +117,7 @@ export default function KnowledgePanel() {
115117
'78742330808': '"1 egg" dans quantity',
116118
'5051140152901': '12 large - calibre trouvé',
117119
'3560071227241': '6 moyens - calibre trouvé',
120+
'0030561400018' : 'pickled eggs',
118121
};
119122

120123
useEffect(() => {

0 commit comments

Comments
 (0)