Skip to content

Commit

Permalink
refactor(Price tags): refactor matching script, move rules to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Dec 30, 2024
1 parent 1ab55d4 commit 5ef2e67
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from collections import Counter
from decimal import Decimal

from django.core.management.base import BaseCommand

from open_prices.prices.constants import TYPE_CATEGORY, TYPE_PRODUCT
from open_prices.prices.models import Price
from open_prices.proofs.models import PriceTag, Proof
from open_prices.proofs.utils import (
match_category_price_tag_with_category_price,
match_product_price_tag_with_product_price,
)


def stats():
Expand All @@ -21,28 +22,6 @@ def stats():
)


def match_decimal_with_float(price_decimal: Decimal, price_float: float) -> bool:
return float(price_decimal) == price_float


def match_price_tag_with_product_price(price: Price, price_tag: PriceTag) -> bool:
price_tag_prediction_data = price_tag.predictions.first().data
return (
price.type == TYPE_PRODUCT
and (price.product_code == price_tag_prediction_data["barcode"])
and match_decimal_with_float(price.price, price_tag_prediction_data["price"])
)


def match_price_tag_with_category_price(price: Price, price_tag: PriceTag) -> bool:
price_tag_prediction_data = price_tag.predictions.first().data
return (
price.type == TYPE_CATEGORY
and (price.product_code == price_tag.predictions.first().data["product"])
and match_decimal_with_float(price.price, price_tag_prediction_data["price"])
)


class Command(BaseCommand):
"""
For each proof...
Expand Down Expand Up @@ -78,19 +57,21 @@ def handle(self, *args, **options) -> None: # type: ignore
if price.price_tags.count() > 0:
continue
# match product price
elif match_price_tag_with_product_price(price, price_tag):
elif match_product_price_tag_with_product_price(
price_tag, price
):
price_tag.price_id = price.id
price_tag.status = 1
price_tag.save()
break
# match category price
elif match_price_tag_with_category_price(price, price_tag):
elif match_category_price_tag_with_category_price(
price_tag, price
):
price_tag.price_id = price.id
price_tag.status = 1
price_tag.save()
break
else:
continue

self.stdout.write("=== Stats after...")
stats()
31 changes: 31 additions & 0 deletions open_prices/proofs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import random
import string
import time
from decimal import Decimal
from mimetypes import guess_extension
from pathlib import Path
from typing import Any
Expand All @@ -14,6 +15,10 @@
from openfoodfacts.utils import http_session
from PIL import Image, ImageOps

from open_prices.prices.constants import TYPE_CATEGORY, TYPE_PRODUCT
from open_prices.prices.models import Price
from open_prices.proofs.models import PriceTag

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -233,3 +238,29 @@ def fetch_and_save_ocr_data(image_path: Path | str, override: bool = False) -> b

logger.debug("OCR data saved to %s", ocr_json_path)
return True


def match_decimal_with_float(price_decimal: Decimal, price_float: float) -> bool:
return float(price_decimal) == price_float


def match_product_price_tag_with_product_price(
price_tag: PriceTag, price: Price
) -> bool:
price_tag_prediction_data = price_tag.predictions.first().data
return (
price.type == TYPE_PRODUCT
and (price.product_code == price_tag_prediction_data["barcode"])
and match_decimal_with_float(price.price, price_tag_prediction_data["price"])
)


def match_category_price_tag_with_category_price(
price_tag: PriceTag, price: Price
) -> bool:
price_tag_prediction_data = price_tag.predictions.first().data
return (
price.type == TYPE_CATEGORY
and (price.product_code == price_tag.predictions.first().data["product"])
and match_decimal_with_float(price.price, price_tag_prediction_data["price"])
)

0 comments on commit 5ef2e67

Please sign in to comment.