Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(locations): New Location.proof_count field #468

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
"proof_count",
"created",
)
list_filter = ("osm_type",)
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,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))
17 changes: 17 additions & 0 deletions open_prices/locations/migrations/0002_location_proof_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: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),
),
]
7 changes: 6 additions & 1 deletion open_prices/locations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -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(
Expand Down
17 changes: 17 additions & 0 deletions open_prices/locations/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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)