Self hosted FLOSS fitness/workout, nutrition and weight tracker
Fetch the category along with the body weight entries
Roland Geiderb17fac684525
diff --git a/wger/measurements/models/measurement.py b/wger/measurements/models/measurement.pyindex 57cc43e37..469cc3ca1 100644--- a/wger/measurements/models/measurement.py+++ b/wger/measurements/models/measurement.py@@ -158,9 +158,13 @@ class Measurement(models.Model):def body_weight_for(cls, user: User) -> models.QuerySet:"""Returns the user's body weight entries: the measurements in the- official body-weight category+ official body-weight category.++ The category comes along: every consumer reads it back per entry, be it+ for the unit an entry without `extra_data` falls back to or for the+ owner the legacy weight serializer returns"""- return cls.objects.filter(+ return cls.objects.select_related('category').filter(category__user=user,category__metric_type=MetricType.BODY_WEIGHT,category__is_official=True,diff --git a/wger/weight/tests/test_entry.py b/wger/weight/tests/test_entry.pyindex 47f6932e7..c76bd7023 100644--- a/wger/weight/tests/test_entry.py+++ b/wger/weight/tests/test_entry.py@@ -17,6 +17,9 @@ from decimal import Decimal# Djangofrom django.contrib.auth.models import User+from django.db import connection+from django.test.utils import CaptureQueriesContext+from django.urls import reversefrom django.utils import timezone# wger@@ -267,3 +270,45 @@ class WeightEntryTokenAuthTestCase(WgerTestCase):Test that the endpoint is not open to anonymous callers"""self.assertEqual(self.client.get(self.url).status_code, 403)+++class WeightEntryQueryCountTestCase(api_base_test.ApiBaseTestCase, WgerTestCase):+ """+ The endpoint returns the owner and the unit of every entry, both of which+ live on the category. openScale lists the whole history in one call+ """++ def add_entries(self, count: int, offset: int):+ category = Category.objects.get(+ user__username='test',+ metric_type=MetricType.BODY_WEIGHT,+ is_official=True,+ )+ Measurement.objects.bulk_create(+ Measurement(+ category=category,+ value=70,+ date=timezone.now() - timezone.timedelta(days=offset + i),+ )+ for i in range(count)+ )++ def test_listing_does_not_query_per_entry(self):+ """+ Test that the query count of a listing does not grow with the entries+ """+ self.authenticate('test')+ url = reverse('weightentry-list')++ # Warms the caches a first request fills (auth, user profile)+ self.client.get(url)++ self.add_entries(10, offset=100)+ with CaptureQueriesContext(connection) as short_list:+ self.assertEqual(self.client.get(url, {'limit': 100}).status_code, 200)++ self.add_entries(10, offset=200)+ with CaptureQueriesContext(connection) as long_list:+ self.assertEqual(self.client.get(url, {'limit': 100}).status_code, 200)++ self.assertEqual(len(long_list.captured_queries), len(short_list.captured_queries))