Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add API resources form the manager app

Roland Geider74110b7d1468
diff --git a/wger/manager/api/__init__.py b/wger/manager/api/__init__.py
new file mode 100644
index 000000000..e69de29bb
diff --git a/wger/manager/api/resources.py b/wger/manager/api/resources.py
new file mode 100644
index 000000000..04e9576ba
--- /dev/null
+++ b/wger/manager/api/resources.py
@@ -0,0 +1,213 @@
+# -*- 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
+
+from tastypie import fields
+from tastypie.authentication import ApiKeyAuthentication
+from tastypie.resources import ModelResource
+
+from django.contrib.auth.models import User
+
+from wger.exercises.api.resources import ExerciseResource
+
+from wger.manager.models import UserProfile
+from wger.manager.models import Workout
+from wger.manager.models import Schedule
+from wger.manager.models import ScheduleStep
+from wger.manager.models import DaysOfWeek
+from wger.manager.models import Day
+from wger.manager.models import Set
+from wger.manager.models import Setting
+from wger.manager.models import WorkoutLog
+
+
+#
+# User related resources
+#
+class UserProfileResource(ModelResource):
+ '''
+ Resource for user profiles
+ '''
+ user = fields.ToOneField('wger.manager.api.resources.UserResource', 'user')
+
+ def authorized_read_list(self, object_list, bundle):
+ '''
+ Filter to own objects
+ '''
+ return object_list.filter(user=bundle.request.user)
+
+ class Meta:
+ queryset = UserProfile.objects.all()
+ authentication = ApiKeyAuthentication()
+
+
+class UserResource(ModelResource):
+ '''
+ Resource for users
+ '''
+
+ def authorized_read_list(self, object_list, bundle):
+ '''
+ Filter to own objects
+ '''
+ return object_list.filter(pk=bundle.request.user.pk)
+
+ class Meta:
+ queryset = User.objects.all()
+ excludes = ['is_active', 'is_staff', 'is_superuser', 'password', ]
+ authentication = ApiKeyAuthentication()
+
+
+#
+# Workout related resources
+#
+class WorkoutResource(ModelResource):
+ '''
+ Resource for workouts
+ '''
+
+ days = fields.ToManyField('wger.manager.api.resources.DaysOfWeekResource', 'day_set')
+
+ def authorized_read_list(self, object_list, bundle):
+ '''
+ Filter to own objects
+ '''
+ return object_list.filter(user=bundle.request.user)
+
+ class Meta:
+ queryset = Workout.objects.all()
+ authentication = ApiKeyAuthentication()
+
+
+class ScheduleStepResource(ModelResource):
+ '''
+ Resource for schedule steps
+ '''
+
+ workout = fields.ToOneField(WorkoutResource, 'workout')
+
+ def authorized_read_list(self, object_list, bundle):
+ '''
+ Filter to own objects
+ '''
+ return object_list.filter(schedule__user=bundle.request.user)
+
+ class Meta:
+ queryset = ScheduleStep.objects.all()
+ authentication = ApiKeyAuthentication()
+
+
+class ScheduleResource(ModelResource):
+ '''
+ Resource for schedules
+ '''
+
+ steps = fields.ToManyField(ScheduleStepResource, 'schedulestep_set')
+
+ def authorized_read_list(self, object_list, bundle):
+ '''
+ Filter to own objects
+ '''
+ return object_list.filter(user=bundle.request.user)
+
+ class Meta:
+ queryset = Schedule.objects.all()
+ authentication = ApiKeyAuthentication()
+
+
+class DaysOfWeekResource(ModelResource):
+ '''
+ Resource for days of the week
+ '''
+
+ class Meta:
+ queryset = DaysOfWeek.objects.all()
+
+
+class DayResource(ModelResource):
+ '''
+ Resource for training days
+ '''
+
+ workout = fields.ToOneField(WorkoutResource, 'training')
+ days = fields.ToManyField(DaysOfWeekResource, 'day')
+
+ def authorized_read_list(self, object_list, bundle):
+ '''
+ Filter to own objects
+ '''
+ return object_list.filter(training__user=bundle.request.user)
+
+ class Meta:
+ queryset = Day.objects.all()
+ authentication = ApiKeyAuthentication()
+
+
+class SetResource(ModelResource):
+ '''
+ Resource for training sets
+ '''
+
+ day = fields.ToOneField(DayResource, 'exerciseday')
+ exercises = fields.ToManyField(ExerciseResource, 'exercises')
+
+ def authorized_read_list(self, object_list, bundle):
+ '''
+ Filter to own objects
+ '''
+ return object_list.filter(exerciseday__training__user=bundle.request.user)
+
+ class Meta:
+ queryset = Set.objects.all()
+ authentication = ApiKeyAuthentication()
+
+
+class SettingResource(ModelResource):
+ '''
+ Resource for training settings
+ '''
+
+ set = fields.ToOneField(SetResource, 'set')
+ exercise = fields.ToOneField(ExerciseResource, 'exercise')
+
+ def authorized_read_list(self, object_list, bundle):
+ '''
+ Filter to own objects
+ '''
+ return object_list.filter(set__exerciseday__training__user=bundle.request.user)
+
+ class Meta:
+ queryset = Setting.objects.all()
+ authentication = ApiKeyAuthentication()
+
+
+class WorkoutLogResource(ModelResource):
+ '''
+ Resource for a workout log
+ '''
+
+ user = fields.ToOneField(UserResource, 'user')
+ exercise = fields.ToOneField(ExerciseResource, 'exercise')
+ workout = fields.ToOneField(WorkoutResource, 'workout')
+
+ def authorized_read_list(self, object_list, bundle):
+ '''
+ Filter to own objects
+ '''
+ return object_list.filter(user=bundle.request.user)
+
+ class Meta:
+ queryset = WorkoutLog.objects.all()
+ authentication = ApiKeyAuthentication()
diff --git a/wger/urls.py b/wger/urls.py
index 88e58dd11..ccea2ea8c 100644
--- a/wger/urls.py
+++ b/wger/urls.py
@@ -8,37 +8,51 @@ from wger.utils.generic_views import TextTemplateView
from wger.utils.generic_views import WebappManifestView
from tastypie.api import Api
+from wger.exercises.api import resources as exercises_api
+from wger.nutrition.api import resources as nutrition_api
+from wger.manager.api import resources as manager_api
-from wger.exercises.api.resources import ExerciseResource
-from wger.exercises.api.resources import ExerciseCategoryResource
-from wger.exercises.api.resources import ExerciseCommentResource
-from wger.exercises.api.resources import MuscleResource
-from wger.exercises.api.resources import LanguageResource
-
-from wger.nutrition.api.resources import IngredientResource
-from wger.nutrition.api.resources import WeightUnitResource
-
+#
# REST API
+#
v1_api = Api(api_name='v1')
# Exercises app
-v1_api.register(ExerciseCategoryResource())
-v1_api.register(ExerciseCommentResource())
-v1_api.register(ExerciseResource())
-v1_api.register(MuscleResource())
-v1_api.register(LanguageResource())
+v1_api.register(exercises_api.ExerciseCategoryResource())
+v1_api.register(exercises_api.ExerciseCommentResource())
+v1_api.register(exercises_api.ExerciseResource())
+v1_api.register(exercises_api.MuscleResource())
+v1_api.register(exercises_api.LanguageResource())
# Nutrition app
-v1_api.register(IngredientResource())
-v1_api.register(WeightUnitResource())
+v1_api.register(nutrition_api.IngredientResource())
+v1_api.register(nutrition_api.WeightUnitResource())
+# Manager app
+v1_api.register(manager_api.UserProfileResource())
+v1_api.register(manager_api.UserResource())
+v1_api.register(manager_api.WorkoutResource())
+v1_api.register(manager_api.ScheduleResource())
+v1_api.register(manager_api.ScheduleStepResource())
+v1_api.register(manager_api.DaysOfWeekResource())
+v1_api.register(manager_api.DayResource())
+v1_api.register(manager_api.SetResource())
+v1_api.register(manager_api.SettingResource())
+v1_api.register(manager_api.WorkoutLogResource())
from django.contrib import admin
admin.autodiscover()
+#
+# Sitemaps
+#
sitemaps = {'exercises': ExercisesSitemap,
'nutrition': NutritionSitemap}
+
+#
+# The actual URLs
+#
urlpatterns = i18n_patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('wger.manager.urls')),