Skip to content

Commit

Permalink
feat(locations): New Location.user_count field (#471)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn authored Sep 27, 2024
1 parent cebc523 commit be0c796
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions open_prices/locations/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class LocationAdmin(admin.ModelAdmin):
"osm_tag_key",
"osm_tag_value",
"price_count",
"user_count",
"product_count",
"proof_count",
"created",
Expand Down
1 change: 1 addition & 0 deletions open_prices/locations/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ class Meta:
osm_name = factory.Faker("name")
osm_address_country = factory.Faker("country")
# price_count = factory.LazyAttribute(lambda x: random.randrange(0, 100))
# user_count = factory.LazyAttribute(lambda x: random.randrange(0, 100))
# product_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/locations/migrations/0004_location_user_count.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.1 on 2024-09-27 15:42

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("locations", "0003_location_product_count"),
]

operations = [
migrations.AddField(
model_name="location",
name="user_count",
field=models.PositiveIntegerField(blank=True, default=0, null=True),
),
]
12 changes: 12 additions & 0 deletions open_prices/locations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Location(models.Model):
)

price_count = models.PositiveIntegerField(default=0, blank=True, null=True)
user_count = models.PositiveIntegerField(default=0, blank=True, null=True)
product_count = models.PositiveIntegerField(default=0, blank=True, null=True)
proof_count = models.PositiveIntegerField(default=0, blank=True, null=True)

Expand Down Expand Up @@ -103,6 +104,17 @@ def update_price_count(self):
self.price_count = self.prices.count()
self.save(update_fields=["price_count"])

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

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

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

Expand Down
9 changes: 9 additions & 0 deletions open_prices/locations/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ class LocationPropertyTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.user = UserFactory()
cls.user_2 = UserFactory()
cls.location = LocationFactory(**LOCATION_NODE_652825274)
cls.proof = ProofFactory(
location_osm_id=cls.location.osm_id,
Expand All @@ -113,6 +114,7 @@ def setUpTestData(cls):
location_osm_id=cls.location.osm_id,
location_osm_type=cls.location.osm_type,
price=2.0,
owner=cls.user_2.user_id,
)

def test_update_price_count(self):
Expand All @@ -125,6 +127,13 @@ def test_update_price_count(self):
self.location.update_price_count()
self.assertEqual(self.location.price_count, 0)

def test_update_user_count(self):
self.location.refresh_from_db()
self.assertEqual(self.location.user_count, 0)
# update_user_count() should fix user_count
self.location.update_user_count()
self.assertEqual(self.location.user_count, 2)

def test_update_product_count(self):
self.location.refresh_from_db()
self.assertEqual(self.location.product_count, 0)
Expand Down

0 comments on commit be0c796

Please sign in to comment.