Skip to content
This repository has been archived by the owner on Aug 20, 2022. It is now read-only.

Commit

Permalink
draft functions for #20
Browse files Browse the repository at this point in the history
  • Loading branch information
podusowski committed May 22, 2017
1 parent 1eba8ff commit df2af12
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
14 changes: 14 additions & 0 deletions activities/strength/strength_workout.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ def add_reps(user, excercise_id, reps):
return s.workout.id


def start_timer(user, excercise_id):
excercise = models.Excercise.objects.get(pk=excercise_id, workout__user=user)
excercise.timers_set.create(time_started=django.utils.timezone.now())
return excercise.workout.id


def stop_timer(user, excercise_id):
excercise = models.Excercise.objects.get(pk=excercise_id, workout__user=user)
timer = excercise.timers_set.latest('pk')
timer.time_finished = django.utils.timezone.now()
timer.save()
return excercise.workout.id


def undo(user, workout_id):
workout = models.Workout.objects.get(pk=workout_id, user=user)

Expand Down
2 changes: 2 additions & 0 deletions activities/strength/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@
url('^finish_workout/(?P<training_session_id>[0-9]+)$', views.finish_workout, name='finish_workout'),
url('^add_excercise/(?P<training_session_id>[0-9]+)/$', views.add_excercise, name='add_excercise'),
url('^add_reps/(?P<excercise_id>[0-9]+)/$', views.add_reps, name='add_reps'),
url('^start_timer/(?P<excercise_id>[0-9]+)$', views.start_timer, name='start_timer'),
url('^stop_timer/(?P<excercise_id>[0-9]+)$', views.stop_timer, name='stop_timer'),
url('^undo/(?P<workout_id>[0-9]+)$', views.undo, name='undo'),
]
12 changes: 12 additions & 0 deletions activities/strength/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ def add_reps(request, excercise_id):
return redirect('workout', id)


@login_required
def start_timer(request, excercise_id):
workout_id = strength_workout.start_timer(request.user, excercise_id)
return redirect('workout', workout_id)


@login_required
def stop_timer(request, excercise_id):
workout_id = strength_workout.stop_timer(request.user, excercise_id)
return redirect('workout', workout_id)


@login_required
def undo(request, workout_id):
strength_workout.undo(request.user, workout_id)
Expand Down
34 changes: 34 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,40 @@ def test_add_some_excercises_and_reps(self):
self.assertIsNotNone(workout.started)
self.assertIsNotNone(workout.finished)

def test_timer_based_excercises(self):
self._start_workout()

statistics = self._get_statistics_from_dashboard()
workout = statistics.previous_workouts()[0]

self.post('/strength/add_excercise/{}/'.format(workout.id), {'name': 'plank front'})
excercise = workout.excercise_set.latest('pk')

first_timer_start = time(2016, 7, 30, 6, 22, 5)

with patch('django.utils.timezone.now', autospec=True) as now:
now.return_value = first_timer_start
self.post('/strength/start_timer/{}'.format(excercise.id))

statistics = self._get_statistics_from_dashboard()
workout = statistics.previous_workouts()[0]
excercise = workout.excercise_set.get()

self.assertEqual(1, len(excercise.timers_set.all()))

first_timer = excercise.timers_set.all()[0]
self.assertEqual(first_timer_start, first_timer.time_started)

first_timer_stop = time(2016, 7, 30, 6, 22, 6)

with patch('django.utils.timezone.now', autospec=True) as now:
now.return_value = first_timer_stop
self.post('/strength/stop_timer/{}'.format(excercise.id))

first_timer = excercise.timers_set.all()[0]

self.assertEqual(first_timer_stop, first_timer.time_finished)

def test_undo_last_rep(self):
workout = self._start_workout()

Expand Down

0 comments on commit df2af12

Please sign in to comment.