Skip to content

Commit

Permalink
feat(users): New User.proof_count field (#442)
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Sep 12, 2024
1 parent c440ced commit 2cb5fb1
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 3 deletions.
8 changes: 7 additions & 1 deletion open_prices/api/users/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def setUpTestData(cls):
cls.url = reverse("api:users-list")
UserFactory(price_count=15)
UserFactory(price_count=0)
UserFactory(price_count=50, location_count=5, product_count=25)
UserFactory(price_count=50, location_count=5, product_count=25, proof_count=10)

def test_user_list_order_by_price_count(self):
url = self.url + "?order_by=-price_count"
Expand All @@ -50,6 +50,12 @@ def test_user_list_order_by_product_count(self):
self.assertEqual(response.data["total"], 2) # only users with prices
self.assertEqual(response.data["items"][0]["product_count"], 25)

def test_user_list_order_by_proof_count(self):
url = self.url + "?order_by=-proof_count"
response = self.client.get(url)
self.assertEqual(response.data["total"], 2) # only users with prices
self.assertEqual(response.data["items"][0]["proof_count"], 10)


class UserListFilterApiTest(TestCase):
@classmethod
Expand Down
8 changes: 7 additions & 1 deletion open_prices/api/users/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,11 @@ class UserViewSet(mixins.ListModelMixin, viewsets.GenericViewSet):
serializer_class = UserSerializer
filter_backends = [DjangoFilterBackend, filters.OrderingFilter]
filterset_class = UserFilter
ordering_fields = ["user_id", "price_count", "location_count", "product_count"]
ordering_fields = [
"user_id",
"price_count",
"location_count",
"product_count",
"proof_count",
]
ordering = ["user_id"]
2 changes: 2 additions & 0 deletions open_prices/users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class UserAdmin(admin.ModelAdmin):
"is_moderator",
"price_count",
"location_count",
"product_count",
"proof_count",
"created",
)
list_filter = ("is_moderator",)
Expand Down
17 changes: 17 additions & 0 deletions open_prices/users/migrations/0004_user_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-12 14:51

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("users", "0003_user_product_count"),
]

operations = [
migrations.AddField(
model_name="user",
name="proof_count",
field=models.PositiveIntegerField(blank=True, default=0, null=True),
),
]
20 changes: 19 additions & 1 deletion open_prices/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ def has_prices(self):


class User(models.Model):
SERIALIZED_FIELDS = ["user_id", "price_count", "location_count", "product_count"]
SERIALIZED_FIELDS = [
"user_id",
"price_count",
"location_count",
"product_count",
"proof_count",
]

user_id = models.CharField(primary_key=True)

Expand All @@ -17,6 +23,7 @@ class User(models.Model):
price_count = models.PositiveIntegerField(default=0, blank=True, null=True)
location_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)

created = models.DateTimeField(default=timezone.now)
# updated = models.DateTimeField(auto_now=True)
Expand Down Expand Up @@ -60,6 +67,17 @@ def update_product_count(self):
)
self.save(update_fields=["product_count"])

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

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


class Session(models.Model):
user = models.ForeignKey(
Expand Down
15 changes: 15 additions & 0 deletions open_prices/users/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from open_prices.locations.factories import LocationFactory
from open_prices.prices.factories import PriceFactory
from open_prices.prices.models import Price
from open_prices.proofs.factories import ProofFactory
from open_prices.users.factories import UserFactory
from open_prices.users.models import User

Expand All @@ -29,17 +30,24 @@ class UserPropertyTest(TestCase):
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(
product_code="0123456789100",
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(
product_code="0123456789101",
location_osm_id=cls.location.osm_id,
location_osm_type=cls.location.osm_type,
proof_id=cls.proof.id,
price=2.0,
owner=cls.user.user_id,
)
Expand Down Expand Up @@ -67,3 +75,10 @@ def test_update_product_count(self):
# update_product_count() should fix product_count
self.user.update_product_count()
self.assertEqual(self.user.product_count, 2)

def test_update_proof_count(self):
self.user.refresh_from_db()
self.assertEqual(self.user.proof_count, 0)
# update_proof_count() should fix proof_count
self.user.update_proof_count()
self.assertEqual(self.user.proof_count, 1)

0 comments on commit 2cb5fb1

Please sign in to comment.