Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add REST endpoint for the diary log summary

Roland Geiderbb3c3cef03b4
diff --git a/wger/nutrition/api/views.py b/wger/nutrition/api/views.py
index 3d2edc030..a7f6a0c26 100644
--- a/wger/nutrition/api/views.py
+++ b/wger/nutrition/api/views.py
@@ -15,10 +15,15 @@
# 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/>.
+import datetime
+
+from django.shortcuts import get_object_or_404
from rest_framework import viewsets
-from rest_framework.decorators import detail_route
-from rest_framework.decorators import api_view
from rest_framework.response import Response
+from rest_framework.decorators import (
+ detail_route,
+ api_view
+)
from wger.nutrition.api.serializers import (
NutritionPlanSerializer,
@@ -197,6 +202,23 @@ class NutritionPlanViewSet(viewsets.ModelViewSet):
'''
return Response(NutritionPlan.objects.get(pk=pk).get_nutritional_values())
+ @detail_route()
+ def log_summary(self, request, pk):
+ '''
+ Return a summary of the nutrition diary for a given date
+ '''
+ today = datetime.date.today()
+ year = request.GET.get('year', today.year)
+ month = request.GET.get('month', today.month)
+ day = request.GET.get('day', today.day)
+ plan = get_object_or_404(NutritionPlan, pk=pk)
+
+ try:
+ date = datetime.date(year=int(year), month=int(month), day=int(day))
+ except ValueError:
+ date = today
+ return Response(plan.get_log_summary(date))
+
class MealViewSet(WgerOwnerObjectModelViewSet):
'''
diff --git a/wger/software/templates/api.html b/wger/software/templates/api.html
index e90554eaf..52a38ee26 100644
--- a/wger/software/templates/api.html
+++ b/wger/software/templates/api.html
@@ -232,6 +232,17 @@ it in the header, see the link on the sidebar for details.</p>
</div>
</div>
+<div style="margin-top: 1em;">
+ <code>api/v2/nutritionplan/&lt;id&gt;/log_summary/?year=&lt;year&gt;&month=&lt;month&gt;&day=&lt;day&gt;</code><br>
+</div>
+<div class="row">
+ <div class="col-md-offset-1 col-md-10">
+ Returns the nutritional values logged for the nutrition diary for a
+ plan for the given date. You can omit the GET parameters, then you
+ will get the values for today.
+ </div>
+</div>
+
<h3>Data documentation</h3>
<p>The data structures should be pretty forward and easy to understand: a workout