-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Moderation): script to remove Products with long barcodes (#700)
- Loading branch information
Showing
7 changed files
with
90 additions
and
2 deletions.
There are no files selected for viewing
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
Empty file.
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,46 @@ | ||
""" | ||
A list of rules, to clean up the data | ||
""" | ||
|
||
from django.db.models import Q | ||
from django.db.models.functions import Length | ||
|
||
from open_prices.prices import constants as price_constants | ||
from open_prices.products.models import Product | ||
|
||
|
||
def cleanup_products_with_long_barcodes(): | ||
""" | ||
Remove products (and their prices) with long barcodes | ||
- long barcode = more than 13 characters | ||
- only if the price come from the validation workflows | ||
- only from unknown source (aka not from OxF) | ||
""" | ||
# init | ||
price_deleted_count = 0 | ||
product_deleted_count = 0 | ||
|
||
# products with long barcodes | ||
product_queryset = Product.objects.annotate( | ||
code_length_annotated=Length("code") | ||
).filter(code_length_annotated__gt=13, source=None) | ||
print(f"Found {product_queryset.count()} products with long barcodes") | ||
|
||
# build the price source filter query | ||
source_query = Q() | ||
for source in price_constants.PRICE_CREATED_FROM_PRICE_TAG_VALIDATION_SOURCE_LIST: | ||
source_query |= Q(source__contains=source) | ||
|
||
# loop on each product | ||
for product in product_queryset: | ||
product_prices_from_source_queryset = product.prices.filter(source_query) | ||
if product_prices_from_source_queryset.exists(): | ||
for price in product_prices_from_source_queryset.all(): | ||
price.delete() # delete 1 by 1 to trigger signals | ||
price_deleted_count += 1 | ||
if product.prices.count() == 0: | ||
product.delete() | ||
product_deleted_count += 1 | ||
|
||
# recap | ||
print(f"Deleted {price_deleted_count} prices and {product_deleted_count} products") |
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,30 @@ | ||
from django.test import TestCase | ||
|
||
from open_prices.moderation.rules import cleanup_products_with_long_barcodes | ||
from open_prices.prices.factories import PriceFactory | ||
from open_prices.prices.models import Price | ||
from open_prices.products.factories import ProductFactory | ||
from open_prices.products.models import Product | ||
|
||
|
||
class ModerationRulesTest(TestCase): | ||
@classmethod | ||
def setUpTestData(cls): | ||
cls.product_ok = ProductFactory(code="0123456789100") # 13 characters | ||
PriceFactory(product_code=cls.product_ok.code, source="mobile") | ||
PriceFactory( | ||
product_code=cls.product_ok.code, | ||
source="web - /experiments/price-validation-assistant", | ||
) | ||
cls.product_not_ok = ProductFactory(code="01234567891000") # 14 characters | ||
PriceFactory( | ||
product_code=cls.product_not_ok.code, | ||
source="web - /experiments/price-validation-assistant", | ||
) | ||
|
||
def test_cleanup_products_with_long_barcodes(self): | ||
self.assertEqual(Product.objects.count(), 2) | ||
self.assertEqual(Price.objects.count(), 2 + 1) | ||
cleanup_products_with_long_barcodes() | ||
self.assertEqual(Product.objects.count(), 1) # 1 product deleted | ||
self.assertEqual(Price.objects.count(), 2) # 1 price deleted |
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
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
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