From ba02e6bfc8bf37b6e2190d24062ba8a17b704346 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Odini?= Date: Thu, 12 Sep 2024 15:55:57 +0200 Subject: [PATCH] feat(users): New User.location_count field (#440) --- open_prices/users/admin.py | 8 ++++- open_prices/users/factories.py | 1 + .../migrations/0002_user_location_count.py | 17 +++++++++++ open_prices/users/models.py | 14 ++++++++- open_prices/users/tests.py | 29 +++++++++++++++++-- 5 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 open_prices/users/migrations/0002_user_location_count.py diff --git a/open_prices/users/admin.py b/open_prices/users/admin.py index b5f41c1c..9e24cf82 100644 --- a/open_prices/users/admin.py +++ b/open_prices/users/admin.py @@ -5,7 +5,13 @@ @admin.register(User) class UserAdmin(admin.ModelAdmin): - list_display = ("user_id", "is_moderator", "price_count", "created") + list_display = ( + "user_id", + "is_moderator", + "price_count", + "location_count", + "created", + ) list_filter = ("is_moderator",) search_fields = ("user_id",) diff --git a/open_prices/users/factories.py b/open_prices/users/factories.py index ac25cb07..83ea4fa5 100644 --- a/open_prices/users/factories.py +++ b/open_prices/users/factories.py @@ -12,6 +12,7 @@ class Meta: user_id = factory.Faker("user_name") # price_count = factory.LazyAttribute(lambda x: random.randrange(0, 100)) + # location_count = factory.LazyAttribute(lambda x: random.randrange(0, 100)) # noqa class SessionFactory(DjangoModelFactory): diff --git a/open_prices/users/migrations/0002_user_location_count.py b/open_prices/users/migrations/0002_user_location_count.py new file mode 100644 index 00000000..61e361f0 --- /dev/null +++ b/open_prices/users/migrations/0002_user_location_count.py @@ -0,0 +1,17 @@ +# Generated by Django 5.1 on 2024-09-12 13:37 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("users", "0001_initial"), + ] + + operations = [ + migrations.AddField( + model_name="user", + name="location_count", + field=models.PositiveIntegerField(blank=True, default=0, null=True), + ), + ] diff --git a/open_prices/users/models.py b/open_prices/users/models.py index 82b0d04d..f86d6449 100644 --- a/open_prices/users/models.py +++ b/open_prices/users/models.py @@ -8,13 +8,14 @@ def has_prices(self): class User(models.Model): - SERIALIZED_FIELDS = ["user_id", "price_count"] + SERIALIZED_FIELDS = ["user_id", "price_count", "location_count"] user_id = models.CharField(primary_key=True) is_moderator = models.BooleanField(default=False) price_count = models.PositiveIntegerField(default=0, blank=True, null=True) + location_count = models.PositiveIntegerField(default=0, blank=True, null=True) created = models.DateTimeField(default=timezone.now) # updated = models.DateTimeField(auto_now=True) @@ -36,6 +37,17 @@ def update_price_count(self): self.price_count = Price.objects.filter(owner=self).count() self.save(update_fields=["price_count"]) + def update_location_count(self): + from open_prices.prices.models import Price + + self.location_count = ( + Price.objects.filter(owner=self.user_id) + .values_list("location_id", flat=True) + .distinct() + .count() + ) + self.save(update_fields=["location_count"]) + class Session(models.Model): user = models.ForeignKey( diff --git a/open_prices/users/tests.py b/open_prices/users/tests.py index d5658d96..8c5ba962 100644 --- a/open_prices/users/tests.py +++ b/open_prices/users/tests.py @@ -1,10 +1,17 @@ from django.test import TestCase +from open_prices.locations.factories import LocationFactory from open_prices.prices.factories import PriceFactory from open_prices.prices.models import Price from open_prices.users.factories import UserFactory from open_prices.users.models import User +LOCATION_NODE_652825274 = { + "osm_id": 652825274, + "osm_type": "NODE", + "osm_name": "Monoprix", +} + class UserQuerySetTest(TestCase): @classmethod @@ -21,8 +28,19 @@ class UserPropertyTest(TestCase): @classmethod def setUpTestData(cls): cls.user = UserFactory() - PriceFactory(owner=cls.user.user_id, price=1.0) - PriceFactory(owner=cls.user.user_id, price=2.0) + cls.location = LocationFactory(**LOCATION_NODE_652825274) + PriceFactory( + owner=cls.user.user_id, + location_osm_id=cls.location.osm_id, + location_osm_type=cls.location.osm_type, + price=1.0, + ) + PriceFactory( + owner=cls.user.user_id, + location_osm_id=cls.location.osm_id, + location_osm_type=cls.location.osm_type, + price=2.0, + ) def test_update_price_count(self): self.user.refresh_from_db() @@ -33,3 +51,10 @@ def test_update_price_count(self): # update_price_count() should fix price_count self.user.update_price_count() self.assertEqual(self.user.price_count, 0) + + def test_update_location_count(self): + self.user.refresh_from_db() + self.assertEqual(self.user.location_count, 0) + # update_location_count() should fix location_count + self.user.update_location_count() + self.assertEqual(self.user.location_count, 1)