Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Show exercise history in API

Tom Bowyer7b0ca99a4f4c
diff --git a/wger/core/tests/test_sitemap.py b/wger/core/tests/test_sitemap.py
index 49a7d22ce..16902631f 100644
--- a/wger/core/tests/test_sitemap.py
+++ b/wger/core/tests/test_sitemap.py
@@ -36,7 +36,7 @@ class SitemapTestCase(WgerTestCase):
reverse('django.contrib.sitemaps.views.sitemap', kwargs={'section': 'exercises'})
)
self.assertEqual(response.status_code, 200)
- self.assertEqual(len(response.context['urlset']), 9)
+ self.assertEqual(len(response.context['urlset']), 12)
def test_sitemap_ingredients(self):
diff --git a/wger/exercises/management/commands/change-author.py b/wger/exercises/management/commands/change-author.py
new file mode 100644
index 000000000..f8d9a1c1f
--- /dev/null
+++ b/wger/exercises/management/commands/change-author.py
@@ -0,0 +1,79 @@
+# -*- 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
+
+# Django
+from django.core.management.base import BaseCommand
+
+# wger
+from wger.exercises.models.exercise import Exercise
+
+
+class Command(BaseCommand):
+ """
+ Alter the author name in the exercise models.
+ """
+
+ help = (
+ 'Used to alter exercise license authors\n'
+ 'Must provide at least the exercise id using --exercise-id\n'
+ 'Must also provide the new author name using --author-name\n'
+ )
+
+ def add_arguments(self, parser):
+ parser.add_argument(
+ '--author-name',
+ action='store',
+ dest='author_name',
+ help='The name of the new author'
+ )
+ parser.add_argument(
+ '--exercise-id',
+ action='store',
+ dest='exercise_id',
+ help='The ID of the exercise'
+ )
+
+ def handle(self, **options):
+ author_name = options['author_name']
+ exercise_id = options['exercise_id']
+
+ if author_name is None:
+ self.print_error('Please enter an author name')
+ return
+
+ if exercise_id is None:
+ self.print_error('Please enter an exercise ID')
+ return
+
+ try:
+ exercise = Exercise.objects.get(id=exercise_id)
+ except Exercise.DoesNotExist:
+ self.print_error('Failed to find exercise')
+ return
+
+ exercise.license_author = author_name
+ exercise.save()
+
+ self.stdout.write(
+ self.style.SUCCESS(f"Exercise has been updated")
+ )
+
+ def print_error(self, error_message):
+ self.stdout.write(
+ self.style.WARNING(
+ f"{error_message}"
+ )
+ )
diff --git a/wger/exercises/tests/test_change_author.py b/wger/exercises/tests/test_change_author.py
new file mode 100644
index 000000000..960d60e14
--- /dev/null
+++ b/wger/exercises/tests/test_change_author.py
@@ -0,0 +1,69 @@
+# 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 io import StringIO
+
+from django.core.management import call_command
+
+# wger
+from wger.core.tests.base_testcase import WgerTestCase
+from wger.exercises.models.exercise import Exercise
+
+
+class ChangeAuthorTestCase(WgerTestCase):
+ """
+ Tests the change author command
+ """
+
+ def setUp(self):
+ super(ChangeAuthorTestCase, self).setUp()
+ self.out = StringIO()
+
+
+ def test_missing_author(self):
+ """
+ Test to ensure command handles a missing author parameter
+ """
+ call_command('change-author', stdout=self.out, no_color=True)
+ self.assertIn('Please enter an author name', self.out.getvalue())
+
+ def test_missing_exercise(self):
+ """
+ Test to ensure command handles a missing exercise parameters
+ """
+ args = [
+ "--author-name",
+ "tom"
+ ]
+ call_command('change-author', *args, stdout=self.out, no_color=True)
+ self.assertIn('Please enter an exercise ID', self.out.getvalue())
+
+ def test_can_update_exercise(self):
+ """
+ Test to ensure command can handle an exercise id passed
+ """
+ exercise = Exercise.objects.get(id=1)
+ self.assertNotEquals(exercise.license_author, "tom")
+
+ args = [
+ "--author-name",
+ "tom",
+ "--exercise-id",
+ "1"
+ ]
+ call_command('change-author', *args, stdout=self.out, no_color=True)
+ self.assertIn('Exercise has been updated', self.out.getvalue())
+
+ exercise = Exercise.objects.get(id=1)
+ self.assertEquals(exercise.license_author, "tom")
diff --git a/wger/exercises/tests/test_exercise.py b/wger/exercises/tests/test_exercise.py
index e22e124fa..626d8f823 100644
--- a/wger/exercises/tests/test_exercise.py
+++ b/wger/exercises/tests/test_exercise.py
@@ -257,6 +257,21 @@ class ExercisesTestCase(WgerTestCase):
self.user_login('test')
self.search_exercise()
+ def test_exercise_records_historical_data(self):
+ """
+ Test that changing exercise details generates a historical record
+ """
+ exercise = Exercise.objects.get(pk=2)
+ self.assertEqual(len(exercise.history.all()), 0)
+
+ exercise.name = 'Very cool exercise 2'
+ exercise.description = 'New description'
+ exercise.exercise_base.muscles_secondary.add(Muscle.objects.get(pk=2))
+ exercise.save()
+
+ exercise = Exercise.objects.get(pk=2)
+ self.assertEqual(len(exercise.history.all()), 1)
+
class DeleteExercisesTestCase(WgerDeleteTestCase):
"""