Skip to content

Commit

Permalink
feat: new Product.nutriscore_grade field (#239)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn authored Mar 5, 2024
1 parent a3503b0 commit 2724893
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Add Product nutriscore_grade field
Revision ID: d98171064c5a
Revises: 770709475132
Create Date: 2024-03-03 16:26:02.690921
"""
from typing import Sequence, Union

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "d98171064c5a"
down_revision: Union[str, None] = "770709475132"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column("products", sa.Column("nutriscore_grade", sa.String(), nullable=True))
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column("products", "nutriscore_grade")
# ### end Alembic commands ###
2 changes: 2 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ class Product(Base):
brands_tags = Column(ARRAY(String), server_default="{}", index=True)
labels_tags = Column(ARRAY(String), server_default="{}", index=True)
image_url = Column(String)

nutriscore_grade = Column(String)
unique_scans_n = Column(Integer, nullable=False, server_default="0")

prices: Mapped[list["Price"]] = relationship(back_populates="product")
Expand Down
4 changes: 4 additions & 0 deletions app/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ class ProductFull(ProductCreate):
"https://images.openfoodfacts.org/images/products/800/150/500/5707/front_fr.161.400.jpg"
],
)
nutriscore_grade: str | None = Field(
description="Nutriscore grade.", examples=["a", "unknown"]
)
unique_scans_n: int = Field(
description="number of unique scans of the product on Open Food Facts.",
examples=[15],
Expand Down Expand Up @@ -449,6 +452,7 @@ class ProductFilter(Filter):
source: Optional[Flavor] | None = None
product_name__like: Optional[str] | None = None
brands__like: Optional[str] | None = None
nutriscore_grade: Optional[str] | None = None
unique_scans_n__gte: Optional[int] | None = None
price_count: Optional[int] | None = None
price_count__gte: Optional[int] | None = None
Expand Down
1 change: 1 addition & 0 deletions app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def init_sentry(sentry_dsn: str | None, integrations: list[Integration] | None =
"brands_tags",
"labels_tags",
"image_url",
"nutriscore_grade",
"unique_scans_n",
]

Expand Down
9 changes: 6 additions & 3 deletions tests/integration/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,38 +46,41 @@ def override_get_db():
PRODUCT = ProductCreate(code="8001505005592")
PRODUCT_1 = ProductCreate(
code="0022314010025",
source="off",
product_name="Chestnut spread 500 g",
product_quantity=500,
product_quantity_unit="g",
categories_tags=["en:spreads", "en:nuts-and-their-products"],
brands="Clément Faugier",
brands_tags=["clement-faugier"],
labels_tags=[],
source="off",
nutriscore_grade="d",
unique_scans_n=20,
)
PRODUCT_2 = ProductCreate(
code="0022314010100",
source="off",
product_name="Chestnut spread 100 g",
product_quantity=100,
product_quantity_unit="g",
categories_tags=["en:spreads", "en:nuts-and-their-products"],
brands="Clément Faugier",
brands_tags=["clement-faugier"],
labels_tags=[],
source="off",
nutriscore_grade="d",
unique_scans_n=10,
)
PRODUCT_3 = ProductCreate(
code="3760091721969",
source="off",
product_name="Crème bio de châtaignes 320 g",
product_quantity=320,
product_quantity_unit="g",
categories_tags=["en:spreads", "en:nuts-and-their-products"],
brands="Ethiquable",
brands_tags=["paysans-d-ici", "ethiquable"],
labels_tags=["en:fair-trade", "en:organic", "en:made-in-france"],
source="off",
nutriscore_grade="c",
unique_scans_n=0,
)
LOCATION = LocationFull(
Expand Down

0 comments on commit 2724893

Please sign in to comment.