From d05320fde7a9444c2cc257bcbff8246594e1378f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Odini?= Date: Sat, 22 Feb 2025 11:54:11 +0100 Subject: [PATCH] refactor(Prices): Stats: new stat with_discount count (#716) --- open_prices/prices/models.py | 3 +++ open_prices/prices/tests.py | 4 ++++ ...0008_totalstats_price_with_discount_count.py | 17 +++++++++++++++++ open_prices/stats/models.py | 7 +++++++ 4 files changed, 31 insertions(+) create mode 100644 open_prices/stats/migrations/0008_totalstats_price_with_discount_count.py diff --git a/open_prices/prices/models.py b/open_prices/prices/models.py index 6cc521ab..a611d123 100644 --- a/open_prices/prices/models.py +++ b/open_prices/prices/models.py @@ -28,6 +28,9 @@ class PriceQuerySet(models.QuerySet): + def has_discount(self): + return self.filter(price_is_discounted=True) + def exclude_discounted(self): return self.filter(price_is_discounted=False) diff --git a/open_prices/prices/tests.py b/open_prices/prices/tests.py index 263df6a8..e63b5cd1 100644 --- a/open_prices/prices/tests.py +++ b/open_prices/prices/tests.py @@ -25,6 +25,10 @@ def setUpTestData(cls): PriceFactory(price=8) PriceFactory(price=10) + def test_has_discount(self): + self.assertEqual(Price.objects.count(), 3) + self.assertEqual(Price.objects.has_discount().count(), 1) + def test_exclude_discounted(self): self.assertEqual(Price.objects.count(), 3) self.assertEqual(Price.objects.exclude_discounted().count(), 2) diff --git a/open_prices/stats/migrations/0008_totalstats_price_with_discount_count.py b/open_prices/stats/migrations/0008_totalstats_price_with_discount_count.py new file mode 100644 index 00000000..fa2c891b --- /dev/null +++ b/open_prices/stats/migrations/0008_totalstats_price_with_discount_count.py @@ -0,0 +1,17 @@ +# Generated by Django 5.1.4 on 2025-02-22 10:44 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("stats", "0007_totalstats_product_count_per_source"), + ] + + operations = [ + migrations.AddField( + model_name="totalstats", + name="price_with_discount_count", + field=models.PositiveIntegerField(default=0), + ), + ] diff --git a/open_prices/stats/models.py b/open_prices/stats/models.py index df8f82ec..936a7cbe 100644 --- a/open_prices/stats/models.py +++ b/open_prices/stats/models.py @@ -8,6 +8,7 @@ class TotalStats(SingletonModel): "price_count", "price_type_product_code_count", "price_type_category_tag_count", + "price_with_discount_count", "price_currency_count", ] PRODUCT_COUNT_FIELDS = [ @@ -55,6 +56,7 @@ class TotalStats(SingletonModel): price_count = models.PositiveIntegerField(default=0) price_type_product_code_count = models.PositiveIntegerField(default=0) price_type_category_tag_count = models.PositiveIntegerField(default=0) + price_with_discount_count = models.PositiveIntegerField(default=0) price_currency_count = models.PositiveIntegerField(default=0) product_count = models.PositiveIntegerField(default=0) product_source_off_count = models.PositiveIntegerField(default=0) @@ -83,6 +85,10 @@ class TotalStats(SingletonModel): user_count = models.PositiveIntegerField(default=0) user_with_price_count = models.PositiveIntegerField(default=0) + # Ideas + # - price count per discount type + # - ? + created = models.DateTimeField(default=timezone.now) updated = models.DateTimeField(auto_now=True) @@ -99,6 +105,7 @@ def update_price_stats(self): self.price_type_category_tag_count = Price.objects.filter( category_tag__isnull=False ).count() + self.price_with_discount_count = Price.objects.has_discount().count() self.price_currency_count = ( Price.objects.values_list("currency", flat=True).distinct().count() )