Self hosted FLOSS fitness/workout, nutrition and weight tracker
Restore the legacy weight endpoint contract on top of measurements
Roland Geider6bfe4b7a5d6e
diff --git a/wger/measurements/fixtures/test-measurement-categories.json b/wger/measurements/fixtures/test-measurement-categories.jsonindex aaf5f909c..94d0968da 100644--- a/wger/measurements/fixtures/test-measurement-categories.json+++ b/wger/measurements/fixtures/test-measurement-categories.json@@ -34,5 +34,16 @@"name": "Admin's chest","unit": "cm"}+ },+ {+ "pk": "cccccccc-cccc-cccc-cccc-0000000000b0",+ "model": "measurements.category",+ "fields": {+ "user": 2,+ "name": "Body weight",+ "unit": "kg",+ "metric_type": "body_weight",+ "is_official": true+ }}]diff --git a/wger/measurements/fixtures/test-measurements.json b/wger/measurements/fixtures/test-measurements.jsonindex 8a7e6d6d8..b05106262 100644--- a/wger/measurements/fixtures/test-measurements.json+++ b/wger/measurements/fixtures/test-measurements.json@@ -26,5 +26,23 @@"value": 21,"notes": "some extra information"}+ },+ {+ "pk": "dddddddd-dddd-dddd-dddd-0000000000b1",+ "model": "measurements.measurement",+ "fields": {+ "category": "cccccccc-cccc-cccc-cccc-0000000000b0",+ "date": "2012-10-01T14:30:21.592Z",+ "value": 77+ }+ },+ {+ "pk": "dddddddd-dddd-dddd-dddd-0000000000b2",+ "model": "measurements.measurement",+ "fields": {+ "category": "cccccccc-cccc-cccc-cccc-0000000000b0",+ "date": "2012-11-01T14:30:21.592Z",+ "value": 80.6+ }}]diff --git a/wger/weight/api/filtersets.py b/wger/weight/api/filtersets.pyindex dc09732c7..5c4263dce 100644--- a/wger/weight/api/filtersets.py+++ b/wger/weight/api/filtersets.py@@ -22,7 +22,9 @@ from wger.measurements.models import Measurementclass WeightEntryFilterSet(filters.FilterSet):weight = filters.NumberFilter(field_name='value', lookup_expr='exact')+ weight__gt = filters.NumberFilter(field_name='value', lookup_expr='gt')weight__gte = filters.NumberFilter(field_name='value', lookup_expr='gte')+ weight__lt = filters.NumberFilter(field_name='value', lookup_expr='lt')weight__lte = filters.NumberFilter(field_name='value', lookup_expr='lte')class Meta:diff --git a/wger/weight/api/serializers.py b/wger/weight/api/serializers.pyindex 0363451e1..14ab6c231 100644--- a/wger/weight/api/serializers.py+++ b/wger/weight/api/serializers.py@@ -33,11 +33,10 @@ class WeightEntrySerializer(serializers.ModelSerializer):user = serializers.PrimaryKeyRelatedField(source='category.user', read_only=True)weight = serializers.DecimalField(source='value',- max_digits=6,+ max_digits=5,decimal_places=2,- min_value=Decimal(0.0),- max_value=Decimal(5000.0),- coerce_to_string=False,+ min_value=Decimal(30),+ max_value=Decimal(600),)class Meta:diff --git a/wger/weight/api/views.py b/wger/weight/api/views.pyindex acc4bc729..99646d8cb 100644--- a/wger/weight/api/views.py+++ b/wger/weight/api/views.py@@ -30,9 +30,7 @@ from wger.weight.api.serializers import WeightEntrySerializerclass WeightEntryViewSet(viewsets.ModelViewSet):"""- API endpoint for nutrition plan objects-- CHANGED (wger#2328): reads/writes measurements.Measurement rows+ API endpoint for weight entry objects"""serializer_class = WeightEntrySerializer@@ -49,17 +47,21 @@ class WeightEntryViewSet(viewsets.ModelViewSet):if getattr(self, 'swagger_fake_view', False):return Measurement.objects.none()+ # Measurement orders by -date, the historic weight endpoint by datereturn Measurement.objects.filter(category__user=self.request.user,category__metric_type=MetricType.BODY_WEIGHT,category__is_official=True,- )+ ).order_by('date')def perform_create(self, serializer):"""Route the new entry into the user's official body-weight category"""category = Category.get_or_create_official(- self.request.user, MetricType.BODY_WEIGHT, name='Body weight', unit='kg'+ self.request.user,+ MetricType.BODY_WEIGHT,+ name='Body weight',+ unit=self.request.user.userprofile.weight_unit,)serializer.save(category=category)diff --git a/wger/weight/tests/test_entry.py b/wger/weight/tests/test_entry.pyindex 1745a6632..a13ae3a23 100644--- a/wger/weight/tests/test_entry.py+++ b/wger/weight/tests/test_entry.py@@ -13,11 +13,17 @@# You should have received a copy of the GNU Affero General Public License# Django+from django.contrib.auth.models import Userfrom django.utils import timezone# wgerfrom wger.core.tests import api_base_testfrom wger.core.tests.base_testcase import WgerTestCase+from wger.measurements.models import (+ Category,+ Measurement,+)+from wger.measurements.models.category import MetricTypefrom wger.weight.models import WeightEntry@@ -40,8 +46,55 @@ class WeightEntryTestCase(api_base_test.ApiBaseResourceTestCase):Tests the weight entry overview resource"""- pk = 3- resource = WeightEntry+ pk = 'dddddddd-dddd-dddd-dddd-0000000000b1'+ resource = Measurementprivate_resource = Truedate = timezone.now() - timezone.timedelta(days=25)data = {'weight': 100, 'date': date}++ def get_resource_name(self):+ return 'weightentry'+++class WeightEntryOfficialCategoryTestCase(api_base_test.ApiBaseTestCase, WgerTestCase):+ """+ Tests the automatic handling of the official body weight category+ """++ url = '/api/v2/weightentry/'++ def test_post_creates_official_category(self):+ """+ Test that POSTing without an official category creates one with the+ user's preferred weight unit+ """+ self.authenticate('admin')+ user = User.objects.get(username='admin')+ user.userprofile.weight_unit = 'lb'+ user.userprofile.save()++ response = self.client.post(self.url, data={'weight': 180, 'date': timezone.now()})+ self.assertEqual(response.status_code, 201)++ category = Category.objects.get(+ user=user,+ metric_type=MetricType.BODY_WEIGHT,+ is_official=True,+ )+ self.assertEqual(category.unit, 'lb')++ def test_post_reuses_official_category(self):+ """+ Test that POSTing with an existing official category does not create+ a second one+ """+ self.authenticate('test')+ count_before = Category.objects.filter(user__username='test').count()++ response = self.client.post(self.url, data={'weight': 82.5, 'date': timezone.now()})+ self.assertEqual(response.status_code, 201)++ self.assertEqual(+ Category.objects.filter(user__username='test').count(),+ count_before,+ )