From 121e4c25dee8808f0491914f745f1fa7922898fa Mon Sep 17 00:00:00 2001 From: Leander Furumo Date: Wed, 4 Nov 2026 22:32:25 +0100 Subject: [PATCH] fix: show right user in statistics overview --- bbs/models.py | 2 +- bbs/tests/test_models.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/bbs/models.py b/bbs/models.py index 1362d23..21c23eb 100755 --- a/bbs/models.py +++ b/bbs/models.py @@ -247,7 +247,7 @@ class UserProfile(models.Model): ) def product_order_stats(self): - return OrderLine.objects.filter(order__customer__pk=self.pk).product_order_stats() + return OrderLine.objects.filter(order__customer_id=self.user_id).product_order_stats() def order_sum_total(self) -> Decimal: return Order.objects.filter(customer_id=self.user_id).aggregate(sum=Sum("order_sum"))["sum"] or 0 diff --git a/bbs/tests/test_models.py b/bbs/tests/test_models.py index fb2b216..959a3e6 100644 --- a/bbs/tests/test_models.py +++ b/bbs/tests/test_models.py @@ -289,6 +289,21 @@ def test_userprofile_product_order_stats(user, product): assert result[0]["product_name"] == "borg" +def test_userprofile_product_order_stats_uses_the_profiles_user(db, product): # noqa: ARG001 + # Create the profiles in the opposite order of the users, so profile and user ids differ + alice = DjangoUser.objects.create_user(username="alice") + bob = DjangoUser.objects.create_user(username="bob") + bob_profile = UserProfile.objects.create(user=bob) + alice_profile = UserProfile.objects.create(user=alice) + assert alice_profile.pk != alice.pk + + o = Order.objects.create(customer=alice, order_sum=30) + OrderLine.objects.create(order=o, product=product, amount=1, unit_price=30) + + assert [row["product_name"] for row in alice_profile.product_order_stats()] == ["borg"] + assert list(bob_profile.product_order_stats()) == [] + + def test_image_url_with_email(user): expected_hash = hashlib.sha256(user.email.lower().strip().encode()).hexdigest() assert expected_hash in user.profile.image_url()