-
-
Notifications
You must be signed in to change notification settings - Fork 13
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(Price tags): script to match historical PriceTag with their prices #650
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
open_prices/proofs/management/commands/match_price_tags_with_existing_prices.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
from collections import Counter | ||
|
||
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 | ||
|
||
|
||
def stats(): | ||
print("PriceTag:", PriceTag.objects.count()) | ||
print("Proof PRICE_TAG:", Proof.objects.has_type_price_tag().count()) | ||
print( | ||
"PriceTag per status:", | ||
Counter(PriceTag.objects.all().values_list("status", flat=True)), | ||
) | ||
print( | ||
"PriceTag without a price_id:", | ||
PriceTag.objects.filter(price_id__isnull=True).count(), | ||
) | ||
|
||
|
||
def match_price_tag_with_product_price(price: Price, price_tag: PriceTag) -> bool: | ||
return ( | ||
price.type == TYPE_PRODUCT | ||
and (price.product_code == price_tag.predictions.first().data["barcode"]) | ||
and (str(price.price) == str(price_tag.predictions.first().data["price"])) | ||
) | ||
|
||
|
||
def match_price_tag_with_category_price(price: Price, price_tag: PriceTag) -> bool: | ||
return ( | ||
price.type == TYPE_CATEGORY | ||
and (price.product_code == price_tag.predictions.first().data["product"]) | ||
and (str(price.price) == str(price_tag.predictions.first().data["price"])) | ||
) | ||
|
||
|
||
class Command(BaseCommand): | ||
""" | ||
for each proof | ||
try to match generated price_tags with existing prices | ||
- skip proofs without price_tags or without prices | ||
- skip price_tags that already have a price_id or that have no predictions | ||
- finally loop on each price and try to match with the price_tag prediction data # noqa | ||
""" | ||
|
||
help = "Match price tags with existing prices." | ||
|
||
def handle(self, *args, **options) -> None: # type: ignore | ||
self.stdout.write("Stats before...") | ||
stats() | ||
|
||
self.stdout.write("Running matching script...") | ||
for proof in Proof.objects.has_type_price_tag().prefetch_related( | ||
"prices", "price_tags", "price_tags__predictions" | ||
): | ||
if proof.price_tags.count() == 0: | ||
continue | ||
elif proof.prices.count() == 0: | ||
continue | ||
else: | ||
for price_tag in proof.price_tags.all(): | ||
if price_tag.price_id is not None: | ||
continue | ||
elif price_tag.predictions.count() == 0: | ||
continue | ||
else: | ||
for price in proof.prices.all(): | ||
if price.price_tags.count() > 0: | ||
continue | ||
# match product price | ||
elif match_price_tag_with_product_price(price, price_tag): | ||
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): | ||
price_tag.price_id = price.id | ||
price_tag.status = 1 | ||
price_tag.save() | ||
break | ||
else: | ||
continue | ||
|
||
self.stdout.write("Stats after...") | ||
stats() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is probably an alternate way to loop on PriceTags instead of Proofs 🤔