Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Expose processed chart data via the REST API

Roland Geider0fb898e6f765
diff --git a/wger/manager/api/views.py b/wger/manager/api/views.py
index 646a78f72..357546e7f 100644
--- a/wger/manager/api/views.py
+++ b/wger/manager/api/views.py
@@ -15,12 +15,19 @@
# 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 json
+
+# Django
+from django.shortcuts import get_object_or_404
+
# Third Party
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
# wger
+from wger.exercises.models import Exercise
from wger.manager.api.serializers import (
DaySerializer,
ScheduleSerializer,
@@ -43,6 +50,7 @@ from wger.manager.models import (
WorkoutSession
)
from wger.utils.viewsets import WgerOwnerObjectModelViewSet
+from wger.weight.helpers import process_log_entries
class WorkoutViewSet(viewsets.ModelViewSet):
@@ -78,6 +86,31 @@ class WorkoutViewSet(viewsets.ModelViewSet):
out = WorkoutCanonicalFormSerializer(self.get_object().canonical_representation).data
return Response(out)
+ @action(detail=True)
+ def log_data(self, request, pk):
+ """
+ Returns processed log data for graphing
+
+ Basically, these are the logs for the workout and for a specific exercise.
+
+ If on a day there are several entries with the same number of repetitions,
+ but different weights, only the entry with the higher weight is shown in the chart
+ """
+ execise_id = request.GET.get('id')
+ if not execise_id:
+ return Response("Please provide an exercise ID in the 'id' GET parameter")
+
+ exercise = get_object_or_404(Exercise, pk=execise_id)
+ logs = exercise.workoutlog_set.filter(user=self.request.user,
+ weight_unit__in=(1, 2),
+ repetition_unit=1,
+ workout=self.get_object())
+ entry_logs, chart_data = process_log_entries(logs)
+ serialized_logs = {}
+ for key, values in entry_logs.items():
+ serialized_logs[str(key)] = [WorkoutLogSerializer(entry).data for entry in values]
+ return Response({'chart_data': json.loads(chart_data), 'logs': serialized_logs})
+
class WorkoutSessionViewSet(WgerOwnerObjectModelViewSet):
"""