Self hosted FLOSS fitness/workout, nutrition and weight tracker
Expose workout sessions through the REST API and add tests
Roland Geider63b9233caaa4
diff --git a/wger/manager/api/resources.py b/wger/manager/api/resources.pyindex 77e5daefc..daae1b8c1 100644--- a/wger/manager/api/resources.py+++ b/wger/manager/api/resources.py@@ -23,6 +23,7 @@ from wger.exercises.api.resources import ExerciseResourcefrom wger.utils.resources import UserObjectsOnlyAuthorizationfrom wger.manager.models import UserProfile+from wger.manager.models import WorkoutSessionfrom wger.manager.models import Workoutfrom wger.manager.models import Schedulefrom wger.manager.models import ScheduleStep@@ -79,6 +80,29 @@ class WorkoutResource(ModelResource):"creation_date": ALL}+class WorkoutSessionResource(ModelResource):+ '''+ Resource for workout sessions+ '''++ workout = fields.ToOneField('wger.manager.api.resources.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 = WorkoutSession.objects.all()+ authentication = ApiKeyAuthentication()+ authorization = UserObjectsOnlyAuthorization()+ filtering = {'id': ALL,+ "date": ALL,+ "time_start": ALL,+ "time_end": ALL}++class ScheduleStepResource(ModelResource):'''Resource for schedule stepsdiff --git a/wger/manager/fixtures/test-workout-session.json b/wger/manager/fixtures/test-workout-session.jsonnew file mode 100644index 000000000..f52a13438--- /dev/null+++ b/wger/manager/fixtures/test-workout-session.json@@ -0,0 +1,41 @@+[+ {+ "pk": 1,+ "model": "manager.workoutsession",+ "fields": {+ "impression": "2",+ "time_start": "17:00:00",+ "workout": 1,+ "time_end": "18:30:00",+ "user": 1,+ "date": "2014-01-15",+ "notes": "Notes come here"+ }+ },+ {+ "pk": 2,+ "model": "manager.workoutsession",+ "fields": {+ "impression": "3",+ "time_start": "17:00:00",+ "workout": 1,+ "time_end": "18:00:00",+ "user": 1,+ "date": "2014-01-20",+ "notes": "Even more notes and impressions"+ }+ },+ {+ "pk": 3,+ "model": "manager.workoutsession",+ "fields": {+ "impression": "3",+ "time_start": "10:00:00",+ "workout": 2,+ "time_end": "15:00:00",+ "user": 1,+ "date": "2014-02-01",+ "notes": "I spent the whole day in the gym"+ }+ }+]\ No newline at end of filediff --git a/wger/manager/models.py b/wger/manager/models.pyindex 01210283a..aca4d0ecc 100644--- a/wger/manager/models.py+++ b/wger/manager/models.py@@ -715,12 +715,24 @@ class WorkoutSession(models.Model):Time the workout session ended'''+ def __unicode__(self):+ '''+ Return a more human-readable representation+ '''+ return u"{0} - {1}".format(self.workout, self.date)+class Meta:'''Set other properties'''ordering = ["date", ]+ def get_owner_object(self):+ '''+ Returns the object that has owner information+ '''+ return self+class UserProfile(models.Model):GENDER_MALE = '1'diff --git a/wger/manager/tests/test_workout_session.py b/wger/manager/tests/test_workout_session.pynew file mode 100644index 000000000..fee65749c--- /dev/null+++ b/wger/manager/tests/test_workout_session.py@@ -0,0 +1,105 @@+# 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++import datetime++from django.core.urlresolvers import reverse, reverse_lazy++from wger.manager.models import Workout, WorkoutSession++from wger.manager.tests.testcase import WorkoutManagerTestCase+from wger.manager.tests.testcase import WorkoutManagerAddTestCase+from wger.manager.tests.testcase import WorkoutManagerDeleteTestCase+from wger.manager.tests.testcase import WorkoutManagerEditTestCase+from wger.manager.tests.testcase import ApiBaseResourceTestCase++'''+Tests for workout sessions+'''+++class AddWorkoutSessionTestCase(WorkoutManagerAddTestCase):+ '''+ Tests adding a workout session+ '''++ object_class = WorkoutSession+ url = reverse_lazy('workout-session-add', kwargs={'workout_pk': 1})+ pk = 4+ data = {+ 'user': 1,+ 'workout': 1,+ 'date': datetime.date.today(),+ 'notes': 'Some interesting and deep insights',+ 'impression': '3',+ 'time_start': datetime.time(10, 0),+ 'time_end': datetime.time(13, 0)+ }+ #user_success = 'test'+ #user_fail = 'admin'+++class EditWorkoutSessionTestCase(WorkoutManagerEditTestCase):+ '''+ Tests editing a workout session+ '''++ object_class = WorkoutSession+ url = 'workout-session-edit'+ pk = 3+ #user_success = 'test'+ #user_fail = 'admin'+ data = {+ 'user': 1,+ 'workout': 2,+ 'date': datetime.date.today(),+ 'notes': 'My new insights',+ 'impression': '3',+ 'time_start': datetime.time(10, 0),+ 'time_end': datetime.time(13, 0)+ }+++class WorkoutSessionModelTestCase(WorkoutManagerTestCase):+ '''+ Tests other functionality from the model+ '''++ def test_unicode(self):+ '''+ Test the unicode representation+ '''++ session = WorkoutSession()+ session.workout = Workout.objects.get(pk=1)+ session.date = datetime.date.today()+ self.assertEqual(session.__unicode__(),+ u'{0} - {1}'.format(Workout.objects.get(pk=1), datetime.date.today()))+++class WorkoutSessionApiTestCase(ApiBaseResourceTestCase):+ '''+ Tests the workout overview resource+ '''+ resource = 'workoutsession'+ resource_updatable = False+++class WorkoutSessionDetailApiTestCase(ApiBaseResourceTestCase):+ '''+ Tests accessing a specific workout+ '''+ resource = 'workoutsession/1'+ user_fail = 'test'+ user = 'admin'diff --git a/wger/manager/tests/testcase.py b/wger/manager/tests/testcase.pyindex de8e24a07..bacb83db8 100644--- a/wger/manager/tests/testcase.py+++ b/wger/manager/tests/testcase.py@@ -65,6 +65,7 @@ class BaseTestCase(object):'test-ingredients','test-nutrition-data','test-workout-data',+ 'test-workout-session','test-schedules')current_user = 'anonymous'diff --git a/wger/manager/urls.py b/wger/manager/urls.pyindex 0a161915b..4f1faae5c 100644--- a/wger/manager/urls.py+++ b/wger/manager/urls.py@@ -92,7 +92,7 @@ urlpatterns = patterns('wger.manager.views',workout_session.WorkoutSessionAddView.as_view(),name='workout-session-add'),url(r'^workout/session/(?P<pk>\d+)/edit$',- workout_session.WorkoutSessionUpdateView.as_view,+ workout_session.WorkoutSessionUpdateView.as_view(),name='workout-session-edit'),# Timerdiff --git a/wger/manager/views/workout_session.py b/wger/manager/views/workout_session.pyindex 81044d3fc..9a43d444b 100644--- a/wger/manager/views/workout_session.py+++ b/wger/manager/views/workout_session.py@@ -44,13 +44,12 @@ class WorkoutSessionUpdateView(WgerFormMixin, UpdateView, WgerPermissionMixin):Generic view to edit an existing workout session entry'''model = WorkoutSession- form_class = WorkoutLogFormlogin_required = Truedef get_context_data(self, **kwargs):context = super(WorkoutSessionUpdateView, self).get_context_data(**kwargs)context['form_action'] = reverse('workout-log-edit', kwargs={'pk': self.object.id})- context['title'] = _('Edit log entry for %s') % self.object.exercise.name+ context['title'] = _('Edit workout session for %s') % self.object.datereturn contextdiff --git a/wger/urls.py b/wger/urls.pyindex ad26ba279..72c252a47 100644--- a/wger/urls.py+++ b/wger/urls.py@@ -45,6 +45,7 @@ v1_api.register(manager_api.DayResource())v1_api.register(manager_api.SetResource())v1_api.register(manager_api.SettingResource())v1_api.register(manager_api.WorkoutLogResource())+v1_api.register(manager_api.WorkoutSessionResource())# Weight appv1_api.register(weight_api.WeightEntryResource())diff --git a/wger/utils/generic_views.py b/wger/utils/generic_views.pyindex c93f86caf..dd0049c3b 100644--- a/wger/utils/generic_views.py+++ b/wger/utils/generic_views.py@@ -192,7 +192,7 @@ class WgerFormMixin(ModelFormMixin, WgerPermissionMixin):# Nothing to see, please move alongif owner_object and owner_object.user != self.request.user:- return HttpResponseForbidden()+ return HttpResponseForbidden('You are not allowed to access this object')# Dispatch normallyreturn super(WgerFormMixin, self).dispatch(request, *args, **kwargs)