Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add measurement app
Roland Geider7646416e9e27
diff --git a/wger/core/tests/base_testcase.py b/wger/core/tests/base_testcase.pyindex 5bf2fa0dc..8528b5f7c 100644--- a/wger/core/tests/base_testcase.py+++ b/wger/core/tests/base_testcase.py@@ -126,6 +126,8 @@ class BaseTestCase(object):'test-workout-session','test-schedules','test-gallery-images',+ 'test-measurement-categories',+ 'test-measurements',)current_user = 'anonymous'current_password = ''diff --git a/wger/exercises/tests/test_exercise.py b/wger/exercises/tests/test_exercise.pyindex ac009cda1..6a125150d 100644--- a/wger/exercises/tests/test_exercise.py+++ b/wger/exercises/tests/test_exercise.py@@ -695,7 +695,9 @@ class ExerciseApiTestCase(class ExerciseInfoApiTestCase(- api_base_test.BaseTestCase, api_base_test.ApiBaseTestCase, api_base_test.ApiGetTestCase+ api_base_test.BaseTestCase,+ api_base_test.ApiBaseTestCase,+ api_base_test.ApiGetTestCase,):"""Tests the exercise info resourcediff --git a/wger/measurements/__init__.py b/wger/measurements/__init__.pynew file mode 100644index 000000000..d6331e676--- /dev/null+++ b/wger/measurements/__init__.py@@ -0,0 +1,6 @@+# wger+from wger import get_version+++VERSION = get_version()+default_app_config = 'wger.measurements.apps.MeasurementsConfig'diff --git a/wger/measurements/admin.py b/wger/measurements/admin.pynew file mode 100644index 000000000..846f6b406--- /dev/null+++ b/wger/measurements/admin.py@@ -0,0 +1 @@+# Register your models here.diff --git a/wger/measurements/api/__init__.py b/wger/measurements/api/__init__.pynew file mode 100644index 000000000..e69de29bbdiff --git a/wger/measurements/api/serializers.py b/wger/measurements/api/serializers.pynew file mode 100644index 000000000..4e8a7c3d2--- /dev/null+++ b/wger/measurements/api/serializers.py@@ -0,0 +1,37 @@+# Third Party+from rest_framework import serializers++# wger+from wger.measurements.models import (+ Category,+ Measurement,+)+++class UnitSerializer(serializers.ModelSerializer):+ """+ Measurement unit serializer+ """++ class Meta:+ model = Category+ fields = [+ 'id',+ 'name',+ ]+++class MeasurementSerializer(serializers.ModelSerializer):+ """+ Measurement serializer+ """++ class Meta:+ model = Measurement+ fields = [+ 'id',+ 'unit',+ 'date',+ 'value',+ 'notes',+ ]diff --git a/wger/measurements/api/views.py b/wger/measurements/api/views.pynew file mode 100644index 000000000..bca65cef9--- /dev/null+++ b/wger/measurements/api/views.py@@ -0,0 +1,92 @@+# -*- coding: utf-8 -*-++# This file is part of wger Workout Manager.+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License+# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.++# Standard Library+import logging++# Third Party+from rest_framework import viewsets+from rest_framework.permissions import IsAuthenticated++# wger+from wger.measurements.api.serializers import (+ MeasurementSerializer,+ UnitSerializer,+)+from wger.measurements.models import (+ Category,+ Measurement,+)+++logger = logging.getLogger(__name__)+++class CategoryViewSet(viewsets.ModelViewSet):+ """+ API endpoint for measurement units+ """++ permission_classes = [IsAuthenticated]+ serializer_class = UnitSerializer+ is_private = True+ ordering_fields = '__all__'+ filterset_fields = [+ 'id',+ 'name',+ ]++ def get_queryset(self):+ """+ Only allow access to appropriate objects+ """+ return Category.objects.filter(user=self.request.user)++ def perform_create(self, serializer):+ """+ Set the owner+ """+ serializer.save(user=self.request.user)+++class MeasurementViewSet(viewsets.ModelViewSet):+ """+ API endpoint for measurements+ """++ permission_classes = [IsAuthenticated]+ serializer_class = MeasurementSerializer+ is_private = True+ ordering_fields = '__all__'+ filterset_fields = [+ 'id',+ 'unit',+ 'date',+ 'value',+ ]++ def get_queryset(self):+ """+ Only allow access to appropriate objects+ """+ return Measurement.objects.filter(user=self.request.user)++ def perform_create(self, serializer):+ """+ Set the owner+ """+ serializer.save(user=self.request.user)diff --git a/wger/measurements/apps.py b/wger/measurements/apps.pynew file mode 100644index 000000000..04124fc04--- /dev/null+++ b/wger/measurements/apps.py@@ -0,0 +1,7 @@+from django.apps import AppConfig+++class MeasurementsConfig(AppConfig):+ default_auto_field = 'django.db.models.BigAutoField'+ name = 'wger.measurements'+ verbose_name = "Measurements"diff --git a/wger/measurements/fixtures/test-measurement-categories.json b/wger/measurements/fixtures/test-measurement-categories.jsonnew file mode 100644index 000000000..419e19e3b--- /dev/null+++ b/wger/measurements/fixtures/test-measurement-categories.json@@ -0,0 +1,29 @@+[+ {+ "pk": 1,+ "model": "measurements.category",+ "fields": {+ "user": 2,+ "name": "Biceps",+ "unit": "cm"+ }+ },+ {+ "pk": 2,+ "model": "measurements.category",+ "fields": {+ "user": 2,+ "name": "Body fat",+ "unit": "%"+ }+ },+ {+ "pk": 3,+ "model": "measurements.category",+ "fields": {+ "user": 2,+ "name": "The way I like it",+ "unit": "rods to the hogshead"+ }+ }+]diff --git a/wger/measurements/fixtures/test-measurements.json b/wger/measurements/fixtures/test-measurements.jsonnew file mode 100644index 000000000..6ec62ed2c--- /dev/null+++ b/wger/measurements/fixtures/test-measurements.json@@ -0,0 +1,33 @@+[+ {+ "pk": 1,+ "model": "measurements.measurement",+ "fields": {+ "user": 2,+ "unit": 1,+ "date": "2021-07-01",+ "value": 20+ }+ },+ {+ "pk": 2,+ "model": "measurements.measurement",+ "fields": {+ "user": 2,+ "unit": 2,+ "date": "2021-07-01",+ "value": 100.5+ }+ },+ {+ "pk": 3,+ "model": "measurements.measurement",+ "fields": {+ "user": 2,+ "unit": 1,+ "date": "2021-07-10",+ "value": 21,+ "notes": "some extra information"+ }+ }+]diff --git a/wger/measurements/migrations/0001_initial.py b/wger/measurements/migrations/0001_initial.pynew file mode 100644index 000000000..51d30aac2--- /dev/null+++ b/wger/measurements/migrations/0001_initial.py@@ -0,0 +1,87 @@+# Generated by Django 3.2.3 on 2021-07-08 13:20++import datetime+from django.conf import settings+import django.core.validators+from django.db import migrations, models+import django.db.models.deletion+++class Migration(migrations.Migration):++ initial = True++ dependencies = [+ migrations.swappable_dependency(settings.AUTH_USER_MODEL),+ ]++ operations = [+ migrations.CreateModel(+ name='Category',+ fields=[+ (+ 'id',+ models.BigAutoField(+ auto_created=True, primary_key=True, serialize=False, verbose_name='ID'+ )+ ),+ ('name', models.CharField(max_length=100, verbose_name='Name')),+ ('unit', models.CharField(max_length=30, verbose_name='Unit')),+ (+ 'user',+ models.ForeignKey(+ on_delete=django.db.models.deletion.CASCADE,+ to=settings.AUTH_USER_MODEL,+ verbose_name='User'+ )+ ),+ ],+ options={+ 'ordering': ['-name'],+ },+ ),+ migrations.CreateModel(+ name='Measurement',+ fields=[+ (+ 'id',+ models.BigAutoField(+ auto_created=True, primary_key=True, serialize=False, verbose_name='ID'+ )+ ),+ ('date', models.DateField(default=datetime.datetime.now, verbose_name='Date')),+ (+ 'value',+ models.DecimalField(+ decimal_places=2,+ max_digits=6,+ validators=[+ django.core.validators.MinValueValidator(0),+ django.core.validators.MaxValueValidator(5000)+ ],+ verbose_name='Value'+ )+ ),+ ('notes', models.CharField(blank=True, max_length=100, verbose_name='Description')),+ (+ 'unit',+ models.ForeignKey(+ on_delete=django.db.models.deletion.CASCADE,+ to='measurements.category',+ verbose_name='User'+ )+ ),+ (+ 'user',+ models.ForeignKey(+ on_delete=django.db.models.deletion.CASCADE,+ to=settings.AUTH_USER_MODEL,+ verbose_name='User'+ )+ ),+ ],+ options={+ 'ordering': ['-date'],+ },+ ),+ ]diff --git a/wger/measurements/migrations/__init__.py b/wger/measurements/migrations/__init__.pynew file mode 100644index 000000000..e69de29bbdiff --git a/wger/measurements/models/__init__.py b/wger/measurements/models/__init__.pynew file mode 100644index 000000000..6b3762283--- /dev/null+++ b/wger/measurements/models/__init__.py@@ -0,0 +1,19 @@+# This file is part of wger Workout Manager <https://github.com/wger-project>.+# Copyright (C) 2013 - 2021 wger Team+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU Affero General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License+# along with this program. If not, see <http://www.gnu.org/licenses/>.++# Local+from .category import Category+from .measurement import Measurementdiff --git a/wger/measurements/models/category.py b/wger/measurements/models/category.pynew file mode 100644index 000000000..3c2d21e31--- /dev/null+++ b/wger/measurements/models/category.py@@ -0,0 +1,66 @@+# This file is part of wger Workout Manager <https://github.com/wger-project>.+# Copyright (C) 2013 - 2021 wger Team+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU Affero General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License+# along with this program. If not, see <http://www.gnu.org/licenses/>.++# This file is part of wger Workout Manager.+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License+# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.+# Standard Library++# Django+from django.contrib.auth.models import User+from django.db import models+from django.utils.translation import gettext_lazy as _+++class Category(models.Model):++ class Meta:+ ordering = [+ "-name",+ ]++ user = models.ForeignKey(+ User,+ verbose_name=_('User'),+ on_delete=models.CASCADE,+ )++ name = models.CharField(+ verbose_name=_('Name'),+ max_length=100,+ )++ unit = models.CharField(+ verbose_name=_('Unit'),+ max_length=30,+ )++ def get_owner_object(self):+ """+ Returns the object that has owner information+ """+ return selfdiff --git a/wger/measurements/models/measurement.py b/wger/measurements/models/measurement.pynew file mode 100644index 000000000..9dc2cc53d--- /dev/null+++ b/wger/measurements/models/measurement.py@@ -0,0 +1,93 @@+# This file is part of wger Workout Manager <https://github.com/wger-project>.+# Copyright (C) 2013 - 2021 wger Team+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU Affero General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License+# along with this program. If not, see <http://www.gnu.org/licenses/>.++# This file is part of wger Workout Manager.+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License+# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.+# Standard Library++# Standard Library+import datetime++# Django+from django.contrib.auth.models import User+from django.core.validators import (+ MaxValueValidator,+ MinValueValidator,+)+from django.db import models+from django.utils.translation import gettext_lazy as _++# wger+from wger.measurements.models import Category+++class Measurement(models.Model):++ class Meta:+ ordering = [+ "-date",+ ]++ user = models.ForeignKey(+ User,+ verbose_name=_('User'),+ on_delete=models.CASCADE,+ )++ unit = models.ForeignKey(+ Category,+ verbose_name=_('User'),+ on_delete=models.CASCADE,+ )++ date = models.DateField(+ _('Date'),+ default=datetime.datetime.now,+ )++ value = models.DecimalField(+ verbose_name=_('Value'),+ max_digits=6,+ decimal_places=2,+ validators=[+ MinValueValidator(0),+ MaxValueValidator(5000),+ ]+ )++ notes = models.CharField(+ verbose_name=_('Description'),+ max_length=100,+ blank=True,+ )++ def get_owner_object(self):+ """+ Returns the object that has owner information+ """+ return selfdiff --git a/wger/measurements/tests/__init__.py b/wger/measurements/tests/__init__.pynew file mode 100644index 000000000..1292caeab--- /dev/null+++ b/wger/measurements/tests/__init__.py@@ -0,0 +1,15 @@+# This file is part of wger Workout Manager <https://github.com/wger-project>.+# Copyright (C) 2013 - 2021 wger Team+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU Affero General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License+# along with this program. If not, see <http://www.gnu.org/licenses/>.diff --git a/wger/measurements/tests/test_categories.py b/wger/measurements/tests/test_categories.pynew file mode 100644index 000000000..b979a8e9e--- /dev/null+++ b/wger/measurements/tests/test_categories.py@@ -0,0 +1,36 @@+# This file is part of wger Workout Manager.+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License+# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.+# Standard Library++# wger+from wger.core.tests import api_base_test+from wger.measurements.models import Category+++class UnitApiTestCase(api_base_test.ApiBaseResourceTestCase):+ """+ Tests the measurement units endpoint+ """++ pk = 1+ resource = Category+ private_resource = True+ data = {+ 'name': 'Legs',+ 'unit': 'cm',+ }++ def get_resource_name(self):+ return 'measurement-category'diff --git a/wger/measurements/tests/test_measurements.py b/wger/measurements/tests/test_measurements.pynew file mode 100644index 000000000..245fd773d--- /dev/null+++ b/wger/measurements/tests/test_measurements.py@@ -0,0 +1,30 @@+# This file is part of wger Workout Manager.+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License+# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.+# Standard Library++# wger+from wger.core.tests import api_base_test+from wger.measurements.models import Measurement+++class MeasurementsApiTestCase(api_base_test.ApiBaseResourceTestCase):+ """+ Tests the measurements endpoint+ """++ pk = 1+ resource = Measurement+ private_resource = True+ data = {"unit": 2, "date": "2021-08-12", "value": 99.99}diff --git a/wger/measurements/views.py b/wger/measurements/views.pynew file mode 100644index 000000000..60f00ef0e--- /dev/null+++ b/wger/measurements/views.py@@ -0,0 +1 @@+# Create your views here.diff --git a/wger/settings_global.py b/wger/settings_global.pyindex 961a19a0d..c6799ef0e 100644--- a/wger/settings_global.py+++ b/wger/settings_global.py@@ -61,6 +61,7 @@ INSTALLED_APPS = ('wger.utils','wger.weight','wger.gallery',+ 'wger.measurements',# reCaptcha support, see https://github.com/praekelt/django-recaptcha'captcha',diff --git a/wger/urls.py b/wger/urls.pyindex 389105c7e..4df94395a 100644--- a/wger/urls.py+++ b/wger/urls.py@@ -36,6 +36,7 @@ from wger.exercises.api import views as exercises_api_viewsfrom wger.exercises.sitemap import ExercisesSitemapfrom wger.gallery.api import views as gallery_api_viewsfrom wger.manager.api import views as manager_api_views+from wger.measurements.api import views as measurements_api_viewsfrom wger.nutrition.api import views as nutrition_api_viewsfrom wger.nutrition.sitemap import NutritionSitemapfrom wger.utils.generic_views import TextTemplateView@@ -123,6 +124,18 @@ router.register(r'weightentry', weight_api_views.WeightEntryViewSet, basename='w# Gallery approuter.register(r'gallery', gallery_api_views.ImageViewSet, basename='gallery')+# Measurements app+router.register(+ r'measurement',+ measurements_api_views.MeasurementViewSet,+ basename='measurement',+)+router.register(+ r'measurement-category',+ measurements_api_views.CategoryViewSet,+ basename='measurement-category',+)+## Sitemaps#