Self hosted FLOSS fitness/workout, nutrition and weight tracker
Exercise names are now capitalized
Roland Geidera43b013e84dd
diff --git a/docs/changelog.rst b/docs/changelog.rstindex 60dcf0c80..4907495d7 100644--- a/docs/changelog.rst+++ b/docs/changelog.rst@@ -9,7 +9,7 @@ Upgrade steps from 1.7:* Django update to 1.9: ``pip install -r requirements.txt``* Database upgrade: ``python manage.py migrate``-* Reset workout cache: ``python manage.py clear-cache --clear-workout-cache``+* Reset cache: ``python manage.py clear-cache --clear-all``* Due to changes in the JS package management, you have to deletewger/core/static/bower_components and do a ``python manage.py bower install``* Update static files (only production): ``python manage.py collectstatic``@@ -27,6 +27,7 @@ New features:* Add repetition (minutes, kilometer, etc.) and weight options (kg, lb, plates, until failure) to sets `#216`_ and `#217`_* Allow administrators to deactivate the guest user account `#330`_+* Exercise names are now capitalized, making them more consistent `#232`_* Much improved landing page (thanks `@DeveloperMal`_) `#307`_* Add extended PDF options to schedules as well (thanks `@alelevinas`_ ) `#272`_* Show trained secondary muscles in workout view (thanks `@alokhan`_ ) `#282`_@@ -56,6 +57,7 @@ Other improvements and bugfixes: `#25`_, `#243`_, `#279`_, `#275`_, `#270`_,.. _#201: https://github.com/wger-project/wger/issues/201.. _#216: https://github.com/wger-project/wger/issues/216.. _#217: https://github.com/wger-project/wger/issues/217+.. _#232: https://github.com/wger-project/wger/issues/232.. _#243: https://github.com/wger-project/wger/issues/243.. _#247: https://github.com/wger-project/wger/issues/247.. _#263: https://github.com/wger-project/wger/issues/263diff --git a/docs/commands.rst b/docs/commands.rstindex 879830c24..2f0958dae 100644--- a/docs/commands.rst+++ b/docs/commands.rst@@ -239,6 +239,14 @@ management functions that are sometimes needed. Call them withits help text as it could save the wrong image to the wrong exercise shoulddifferent IDs match.+**redo-capitalize-names**+ re-calculates the capitalized exercise names. This command can be called if the+ current "smart" capitalization algorithm is changed. This is a safe operation,+ since the original names (as entered by the user) are still available.++**submitted-exercises**+ simply prints a list of user submitted exercises+**extract-i18n**extract strings from the database that have to be inserted manually in the POfile when translating. These include e.g. exercise categories.@@ -248,9 +256,6 @@ management functions that are sometimes needed. Call them withjust useful while testing. Please note that you must select what caches toclear.-**submitted-exercises**- simply prints a list of user submitted exercises-**update-user-cache**update the user cache-table. This command is only needed when the python codeused to calculate any of the cached entries is changed and the ones in the@@ -277,5 +282,3 @@ similar**inactive-members**Sends email for gym members that have not been to the gym for a specifiedamount of weeks.--diff --git a/wger/exercises/management/commands/redo-capitalize-names.py b/wger/exercises/management/commands/redo-capitalize-names.pynew file mode 100644index 000000000..b737069fb--- /dev/null+++ b/wger/exercises/management/commands/redo-capitalize-names.py@@ -0,0 +1,42 @@+# -*- 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 django.core.management.base import BaseCommand++from wger.exercises.models import Exercise+from wger.utils.helpers import smart_capitalize+++class Command(BaseCommand):+ '''+ Re-calculates the capitalized exercise names++ This is a safe operation, since the original names (as entered by the user)+ are still available.+ '''++ help = 'Re-calculates the capitalized exercise names'++ def handle(self, **options):++ exercises = Exercise.objects.all()+ for exercise in exercises:+ if options['verbosity'] > 1:+ self.stdout.write('#{} {} -> {}'.format(exercise.id,+ exercise.name,+ smart_capitalize(exercise.name_original)))+ exercise.name = smart_capitalize(exercise.name_original)+ exercise.save()diff --git a/wger/exercises/migrations/0003_auto_20160921_2000.py b/wger/exercises/migrations/0003_auto_20160921_2000.pynew file mode 100644index 000000000..e7d6a28e6--- /dev/null+++ b/wger/exercises/migrations/0003_auto_20160921_2000.py@@ -0,0 +1,53 @@+# -*- coding: utf-8 -*-+# Generated by Django 1.9.9 on 2016-09-21 18:00+from __future__ import unicode_literals++from django.db import migrations, models+++def copy_name(apps, schema_editor):+ '''+ Copies the exercise name to the original name field+ '''+ Excercise = apps.get_model("exercises", "Exercise")+ for exercise in Excercise.objects.all():+ exercise.name_original = exercise.name+ exercise.save()+++def capitalize_name(apps, schema_editor):+ '''+ Capitalizes the name of the exercises++ The algorithm is copied here as it was implemented on the day the migration+ was written.+ '''+ def capitalize(input):+ out = []+ for word in input.split(' '):+ if len(word) > 2 and word[0] != u'ß':+ out.append(word[:1].upper() + word[1:])+ else:+ out.append(word)+ return ' '.join(out)++ Excercise = apps.get_model("exercises", "Exercise")+ for exercise in Excercise.objects.all():+ exercise.name = capitalize(exercise.name_original)+ exercise.save()++class Migration(migrations.Migration):++ dependencies = [+ ('exercises', '0002_auto_20150307_1841'),+ ]++ operations = [+ migrations.AddField(+ model_name='exercise',+ name='name_original',+ field=models.CharField(default='', max_length=200, verbose_name='Name'),+ ),+ migrations.RunPython(copy_name, reverse_code=migrations.RunPython.noop),+ migrations.RunPython(capitalize_name, reverse_code=migrations.RunPython.noop),+ ]diff --git a/wger/exercises/models.py b/wger/exercises/models.pyindex 7f207a376..36653d90f 100644--- a/wger/exercises/models.py+++ b/wger/exercises/models.py@@ -35,6 +35,7 @@ from django.core.validators import MinLengthValidatorfrom django.conf import settingsfrom wger.core.models import Language+from wger.utils.helpers import smart_capitalizefrom wger.utils.managers import SubmissionManagerfrom wger.utils.models import AbstractLicenseModel, AbstractSubmissionModelfrom wger.utils.cache import (@@ -171,6 +172,12 @@ class Exercise(AbstractSubmissionModel, AbstractLicenseModel, models.Model):name = models.CharField(max_length=200,verbose_name=_('Name'))+ '''The exercise's name, with correct upercase'''++ name_original = models.CharField(max_length=200,+ verbose_name=_('Name'),+ default='')+ '''The exercise's name, as entered by the user'''muscles = models.ManyToManyField(Muscle,blank=True,@@ -222,7 +229,7 @@ class Exercise(AbstractSubmissionModel, AbstractLicenseModel, models.Model):'''Reset all cached infos'''-+ self.name = smart_capitalize(self.name_original)super(Exercise, self).save(*args, **kwargs)# Cached objectsdiff --git a/wger/exercises/tests/test_corrected_exercise.py b/wger/exercises/tests/test_corrected_exercise.pyindex bc43c2e12..1232e7a53 100644--- a/wger/exercises/tests/test_corrected_exercise.py+++ b/wger/exercises/tests/test_corrected_exercise.py@@ -31,7 +31,7 @@ class ExercisesCorrectionTestCase(WorkoutManagerTestCase):description = 'a nice, long and accurate description for the exercise'response = self.client.post(reverse('exercise:exercise:correct', kwargs={'pk': 1}),{'category': 3,- 'name': 'my test exercise',+ 'name_original': 'my test exercise','license': 2,'description': description,'muscles': [3]})diff --git a/wger/exercises/tests/test_exercise.py b/wger/exercises/tests/test_exercise.pyindex 51279fb3e..909acd0c4 100644--- a/wger/exercises/tests/test_exercise.py+++ b/wger/exercises/tests/test_exercise.py@@ -233,7 +233,7 @@ class ExercisesTestCase(WorkoutManagerTestCase):count_before = Exercise.objects.count()response = self.client.post(reverse('exercise:exercise:add'),{'category': 2,- 'name': 'my test exercise',+ 'name_original': 'my test exercise','license': 1,'muscles': [1, 2]})count_after = Exercise.objects.count()@@ -269,7 +269,7 @@ class ExercisesTestCase(WorkoutManagerTestCase):description = 'a nice, long and accurate description for the exercise'response = self.client.post(reverse('exercise:exercise:add'),{'category': 2,- 'name': 'my test exercise',+ 'name_original': 'my test exercise','license': 1,'description': description,'muscles': [1, 2]})@@ -297,12 +297,12 @@ class ExercisesTestCase(WorkoutManagerTestCase):self.assertEqual(response.context['active_tab'], 'exercises')exercise_1 = Exercise.objects.get(pk=exercise_id)- self.assertEqual(exercise_1.name, 'my test exercise')+ self.assertEqual(exercise_1.name, 'my Test Exercise')# Wrong category - addingresponse = self.client.post(reverse('exercise:exercise:add'),{'category': 111,- 'name': 'my test exercise',+ 'name_original': 'my test exercise','license': 1,'muscles': [1, 2]})self.assertTrue(response.context['form'].errors['category'])@@ -310,7 +310,7 @@ class ExercisesTestCase(WorkoutManagerTestCase):# Wrong category - editingresponse = self.client.post(reverse('exercise:exercise:edit', kwargs={'pk': '1'}),{'category': 111,- 'name': 'my test exercise',+ 'name_original': 'my test exercise','license': 1,'muscles': [1, 2]})if admin:@@ -321,7 +321,7 @@ class ExercisesTestCase(WorkoutManagerTestCase):# No muscles - addingresponse = self.client.post(reverse('exercise:exercise:add'),{'category': 1,- 'name': 'my test exercise',+ 'name_original': 'my test exercise','license': 1,'muscles': []})self.assertFalse(response.context['form'].errors.get('muscles'))@@ -329,7 +329,7 @@ class ExercisesTestCase(WorkoutManagerTestCase):# No muscles - editingresponse = self.client.post(reverse('exercise:exercise:edit', kwargs={'pk': '1'}),{'category': 1,- 'name': 'my test exercise',+ 'name_original': 'my test exercise','license': 1,'muscles': []})if admin:diff --git a/wger/exercises/views/exercises.py b/wger/exercises/views/exercises.pyindex 0238ad0e2..4c5a7280d 100644--- a/wger/exercises/views/exercises.py+++ b/wger/exercises/views/exercises.py@@ -180,7 +180,7 @@ class ExercisesEditAddView(WgerFormMixin):class Meta:model = Exercisewidgets = {'equipment': TranslatedSelectMultiple()}- fields = ['name',+ fields = ['name_original','category','description','muscles',@@ -293,7 +293,7 @@ class ExerciseDeleteView(WgerDeleteMixin,model = Exercisefields = ('category','description',- 'name',+ 'name_original','muscles','muscles_secondary','equipment')diff --git a/wger/utils/helpers.py b/wger/utils/helpers.pyindex 4b1b5f8fe..ca65787f5 100644--- a/wger/utils/helpers.py+++ b/wger/utils/helpers.py@@ -208,3 +208,25 @@ def normalize_decimal(d):return decimal.Decimal((sign, digits + (0,) * exponent, 0))else:return normalized+++def smart_capitalize(input):+ '''+ A "smart" capitalizer++ This is used to capitalize e.g. exercise names. This is different than python's+ capitalize and the similar django template tag mainly because of side effects+ when applied to all caps words. E.g. the German "KH" (Kurzhantel) is capitalized+ to "Kh" or "ß" to "SS". Because of this, only words with more than 2 letters as+ well as the ones starting with "ß" are ignored.++ :param input: the input string+ :return: the capitalized string+ '''+ out = []+ for word in input.split(' '):+ if len(word) > 2 and word[0] != u'ß':+ out.append(word[:1].upper() + word[1:])+ else:+ out.append(word)+ return ' '.join(out)diff --git a/wger/utils/tests/test_capitalizer.py b/wger/utils/tests/test_capitalizer.pynew file mode 100644index 000000000..4a3d958a6--- /dev/null+++ b/wger/utils/tests/test_capitalizer.py@@ -0,0 +1,33 @@+# 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 wger.core.tests.base_testcase import WorkoutManagerTestCase+from wger.utils.helpers import smart_capitalize+++class CapitalizerTestCase(WorkoutManagerTestCase):+ '''+ Tests the "intelligent" capitalizer+ '''++ def test_capitalizer(self):+ '''+ Tests different combinations of input strings+ '''+ self.assertEqual(smart_capitalize("some long words"), "Some Long Words")+ self.assertEqual(smart_capitalize("Here a short one"), "Here a Short One")+ self.assertEqual(smart_capitalize("meine gym AG"), "Meine Gym AG")+ self.assertEqual(smart_capitalize("ßpecial case"), "ßpecial Case")+ self.assertEqual(smart_capitalize("fIRST lettER only"), "FIRST LettER Only")