Skip to content

Commit

Permalink
feat(products): New Product.proof_count field (#491)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn authored Oct 2, 2024
1 parent 459c6fc commit f77ec80
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
1 change: 1 addition & 0 deletions open_prices/products/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ProductAdmin(admin.ModelAdmin):
"price_count",
"location_count",
"user_count",
"proof_count",
"created",
)
list_filter = ("source",)
Expand Down
1 change: 1 addition & 0 deletions open_prices/products/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ class Meta:
# price_count = factory.LazyAttribute(lambda x: random.randrange(0, 100))
# location_count = factory.LazyAttribute(lambda x: random.randrange(0, 100)) # noqa
# user_count = factory.LazyAttribute(lambda x: random.randrange(0, 100))
# proof_count = factory.LazyAttribute(lambda x: random.randrange(0, 100))
17 changes: 17 additions & 0 deletions open_prices/products/migrations/0004_product_proof_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.1 on 2024-10-02 18:45

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("products", "0003_product_user_count"),
]

operations = [
migrations.AddField(
model_name="product",
name="proof_count",
field=models.PositiveIntegerField(blank=True, default=0, null=True),
),
]
16 changes: 14 additions & 2 deletions open_prices/products/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Product(models.Model):
price_count = models.PositiveIntegerField(default=0, blank=True, null=True)
location_count = models.PositiveIntegerField(default=0, blank=True, null=True)
user_count = models.PositiveIntegerField(default=0, blank=True, null=True)
proof_count = models.PositiveIntegerField(default=0, blank=True, null=True)

created = models.DateTimeField(default=timezone.now)
updated = models.DateTimeField(auto_now=True)
Expand Down Expand Up @@ -101,7 +102,7 @@ def update_location_count(self):
from open_prices.prices.models import Price

self.location_count = (
Price.objects.filter(product=self)
Price.objects.filter(product=self, location_id__isnull=False)
.values_list("location_id", flat=True)
.distinct()
.count()
Expand All @@ -112,13 +113,24 @@ def update_user_count(self):
from open_prices.prices.models import Price

self.user_count = (
Price.objects.filter(product=self)
Price.objects.filter(product=self, owner__isnull=False)
.values_list("owner", flat=True)
.distinct()
.count()
)
self.save(update_fields=["user_count"])

def update_proof_count(self):
from open_prices.prices.models import Price

self.proof_count = (
Price.objects.filter(product=self, proof_id__isnull=False)
.values_list("proof_id", flat=True)
.distinct()
.count()
)
self.save(update_fields=["proof_count"])


@receiver(signals.post_save, sender=Product)
def product_post_create_fetch_and_save_data_from_openfoodfacts(
Expand Down
20 changes: 17 additions & 3 deletions open_prices/products/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from open_prices.products import constants as product_constants
from open_prices.products.factories import ProductFactory
from open_prices.products.models import Product
from open_prices.proofs.factories import ProofFactory
from open_prices.users.factories import UserFactory

PRODUCT_OFF = {
Expand Down Expand Up @@ -103,20 +104,26 @@ def setUpTestData(cls):
cls.product = ProductFactory(code="0123456789100", product_quantity=1000)
cls.user = UserFactory()
cls.location = LocationFactory(**LOCATION_NODE_652825274)
cls.proof = ProofFactory(
location_osm_id=cls.location.osm_id,
location_osm_type=cls.location.osm_type,
owner=cls.user.user_id,
)
PriceFactory(
product_code=cls.product.code,
location_osm_id=cls.location.osm_id,
location_osm_type=cls.location.osm_type,
proof_id=cls.proof.id,
price=1.0,
price_is_discounted=True,
price_without_discount=1.5,
location_osm_id=cls.location.osm_id,
location_osm_type=cls.location.osm_type,
owner=cls.user.user_id,
)
PriceFactory(
product_code=cls.product.code,
price=2.0,
location_osm_id=cls.location.osm_id,
location_osm_type=cls.location.osm_type,
price=2.0,
owner=cls.user.user_id,
)

Expand Down Expand Up @@ -175,3 +182,10 @@ def test_update_user_count(self):
# update_user_count() should fix user_count
self.product.update_user_count()
self.assertEqual(self.product.user_count, 1)

def test_update_proof_count(self):
self.product.refresh_from_db()
self.assertEqual(self.product.proof_count, 0)
# update_proof_count() should fix proof_count
self.product.update_proof_count()
self.assertEqual(self.product.proof_count, 1)

0 comments on commit f77ec80

Please sign in to comment.