Skip to content

Commit

Permalink
refactor(Price tags): matching script: try to match on price only
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Dec 31, 2024
1 parent 9f6e9b8 commit 0273b92
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from open_prices.proofs.models import PriceTag, Proof
from open_prices.proofs.utils import (
match_category_price_tag_with_category_price,
match_price_tag_with_price,
match_product_price_tag_with_product_price,
)

Expand Down Expand Up @@ -72,6 +73,11 @@ def handle(self, *args, **options) -> None: # type: ignore
price_tag.status = 1
price_tag.save()
break
elif match_price_tag_with_price(price_tag, price):
price_tag.price_id = price.id
price_tag.status = 1
price_tag.save()
break

self.stdout.write("=== Stats after...")
stats()
27 changes: 27 additions & 0 deletions open_prices/proofs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,9 @@ def cleanup_price_tag_prediction_barcode(barcode: str) -> str:
def match_product_price_tag_with_product_price(
price_tag: PriceTag, price: Price
) -> bool:
"""
Match on barcode and price.
"""
price_tag_prediction_data = price_tag.predictions.first().data
price_tag_prediction_barcode = price_tag_prediction_data.get("barcode")
price_tag_prediction_barcode = cleanup_price_tag_prediction_barcode(
Expand All @@ -270,6 +273,9 @@ def match_product_price_tag_with_product_price(
def match_category_price_tag_with_category_price(
price_tag: PriceTag, price: Price
) -> bool:
"""
Match on product (category_tag) and price.
"""
price_tag_prediction_data = price_tag.predictions.first().data
price_tag_prediction_product = price_tag_prediction_data.get("product")
price_tag_prediction_price = price_tag_prediction_data.get("price")
Expand All @@ -278,3 +284,24 @@ def match_category_price_tag_with_category_price(
and (price.product_code == price_tag_prediction_product)
and match_decimal_with_float(price.price, price_tag_prediction_price)
)


def match_price_tag_with_price(price_tag: PriceTag, price: Price) -> bool:
"""
Match only on price.
We make sure this price is unique.
"""
price_tag_prediction_data = price_tag.predictions.first().data
price_tag_prediction_price = price_tag_prediction_data.get("price")
proof = price_tag.proof
proof_prices = list(proof.prices.values_list("price", flat=True))
proof_price_tag_prices = [
price_tag.predictions.first().data.get("price")
for price_tag in proof.price_tags.all()
]
return (
# (price_tag_prediction_data["product"] == "other")
match_decimal_with_float(price.price, price_tag_prediction_price)
and proof_prices.count(price.price) == 1
and proof_price_tag_prices.count(price_tag_prediction_price) == 1
)

0 comments on commit 0273b92

Please sign in to comment.