diff --git a/open_prices/locations/admin.py b/open_prices/locations/admin.py index 8bc278ce..7fb9e878 100644 --- a/open_prices/locations/admin.py +++ b/open_prices/locations/admin.py @@ -12,6 +12,7 @@ class LocationAdmin(admin.ModelAdmin): "osm_tag_key", "osm_tag_value", "price_count", + "proof_count", "created", ) list_filter = ("osm_type",) diff --git a/open_prices/locations/factories.py b/open_prices/locations/factories.py index 6ed5cb7e..1f2578eb 100644 --- a/open_prices/locations/factories.py +++ b/open_prices/locations/factories.py @@ -19,3 +19,4 @@ class Meta: osm_name = factory.Faker("name") osm_address_country = factory.Faker("country") # price_count = factory.LazyAttribute(lambda x: random.randrange(0, 100)) + # proof_count = factory.LazyAttribute(lambda x: random.randrange(0, 100)) diff --git a/open_prices/locations/migrations/0002_location_proof_count.py b/open_prices/locations/migrations/0002_location_proof_count.py new file mode 100644 index 00000000..ce81b603 --- /dev/null +++ b/open_prices/locations/migrations/0002_location_proof_count.py @@ -0,0 +1,17 @@ +# Generated by Django 5.1 on 2024-09-27 15:16 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("locations", "0001_initial"), + ] + + operations = [ + migrations.AddField( + model_name="location", + name="proof_count", + field=models.PositiveIntegerField(blank=True, default=0, null=True), + ), + ] diff --git a/open_prices/locations/models.py b/open_prices/locations/models.py index 73657910..74436567 100644 --- a/open_prices/locations/models.py +++ b/open_prices/locations/models.py @@ -22,6 +22,7 @@ def with_stats(self): class Location(models.Model): CREATE_FIELDS = ["osm_id", "osm_type"] LAT_LON_DECIMAL_FIELDS = ["osm_lat", "osm_lon"] + COUNT_FIELDS = ["price_count", "proof_count"] osm_id = models.PositiveBigIntegerField() osm_type = models.CharField( @@ -44,7 +45,7 @@ class Location(models.Model): ) price_count = models.PositiveIntegerField(default=0, blank=True, null=True) - # proof_count = models.PositiveIntegerField(default=0, blank=True, null=True) # noqa + proof_count = models.PositiveIntegerField(default=0, blank=True, null=True) created = models.DateTimeField(default=timezone.now) updated = models.DateTimeField(auto_now=True) @@ -101,6 +102,10 @@ def update_price_count(self): self.price_count = self.prices.count() self.save(update_fields=["price_count"]) + def update_proof_count(self): + self.proof_count = self.proofs.count() + self.save(update_fields=["proof_count"]) + @receiver(signals.post_save, sender=Location) def location_post_create_fetch_and_save_data_from_openstreetmap( diff --git a/open_prices/locations/tests.py b/open_prices/locations/tests.py index 316d292b..1b1471fa 100644 --- a/open_prices/locations/tests.py +++ b/open_prices/locations/tests.py @@ -7,6 +7,8 @@ from open_prices.locations.factories import LocationFactory from open_prices.locations.models import Location from open_prices.prices.factories import PriceFactory +from open_prices.proofs.factories import ProofFactory +from open_prices.users.factories import UserFactory LOCATION_NODE_652825274 = { "osm_id": 652825274, @@ -91,11 +93,19 @@ def test_with_stats(self): class LocationPropertyTest(TestCase): @classmethod def setUpTestData(cls): + 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( location_osm_id=cls.location.osm_id, location_osm_type=cls.location.osm_type, + proof_id=cls.proof.id, price=1.0, + owner=cls.user.user_id, ) PriceFactory( location_osm_id=cls.location.osm_id, @@ -112,3 +122,10 @@ def test_update_price_count(self): # update_price_count() should fix price_count self.location.update_price_count() self.assertEqual(self.location.price_count, 0) + + def test_update_proof_count(self): + self.location.refresh_from_db() + self.assertEqual(self.location.proof_count, 0) + # update_proof_count() should fix location_count + self.location.update_proof_count() + self.assertEqual(self.location.proof_count, 1)