Skip to content

Commit

Permalink
refactor(Prices): Stats: new stat with_discount count (#716)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn authored Feb 22, 2025
1 parent d4b8688 commit d05320f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions open_prices/prices/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions open_prices/prices/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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),
),
]
7 changes: 7 additions & 0 deletions open_prices/stats/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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()
)
Expand Down

0 comments on commit d05320f

Please sign in to comment.