Skip to content

Commit

Permalink
feat(users): New User.location_count field (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Sep 12, 2024
1 parent db749f9 commit ba02e6b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 4 deletions.
8 changes: 7 additions & 1 deletion open_prices/users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",)

Expand Down
1 change: 1 addition & 0 deletions open_prices/users/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
17 changes: 17 additions & 0 deletions open_prices/users/migrations/0002_user_location_count.py
Original file line number Diff line number Diff line change
@@ -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),
),
]
14 changes: 13 additions & 1 deletion open_prices/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand Down
29 changes: 27 additions & 2 deletions open_prices/users/tests.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
Expand All @@ -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)

0 comments on commit ba02e6b

Please sign in to comment.