Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Make adding new exercise more accessible to users

Roland Geider87230a08780f
diff --git a/wger/config/management/commands/extract-categories-i18n.py b/wger/config/management/commands/extract-categories-i18n.py
index fad498122..f4fbd90eb 100644
--- a/wger/config/management/commands/extract-categories-i18n.py
+++ b/wger/config/management/commands/extract-categories-i18n.py
@@ -16,7 +16,7 @@
from django.core.management.base import BaseCommand
-from wger.exercises.models import ExerciseCategory
+from wger.exercises.models import ExerciseCategory, Muscle
from wger.exercises.models import Equipment
@@ -38,3 +38,8 @@ class Command(BaseCommand):
for equipment in Equipment.objects.all():
self.stdout.write('msgid "{0}"\n'
'msgstr ""\n\n'.format(equipment))
+
+ # Muscles
+ for muscle in Muscle.objects.all():
+ self.stdout.write('msgid "{0}"\n'
+ 'msgstr ""\n\n'.format(muscle))
diff --git a/wger/exercises/models.py b/wger/exercises/models.py
index b8d0a5e8c..b7fe50962 100644
--- a/wger/exercises/models.py
+++ b/wger/exercises/models.py
@@ -200,15 +200,15 @@ class Exercise(models.Model):
muscles = models.ManyToManyField(Muscle,
verbose_name=_('Primary muscles'),
- )
+ null=True,
+ blank=True)
'''Main muscles trained by the exercise'''
muscles_secondary = models.ManyToManyField(Muscle,
verbose_name=_('Secondary muscles'),
related_name='secondary_muscles',
null=True,
- blank=True
- )
+ blank=True)
'''Secondary muscles trained by the exercise'''
equipment = models.ManyToManyField(Equipment,
diff --git a/wger/exercises/tests/test_exercise.py b/wger/exercises/tests/test_exercise.py
index 6d7e3b603..bdcd966fa 100644
--- a/wger/exercises/tests/test_exercise.py
+++ b/wger/exercises/tests/test_exercise.py
@@ -292,7 +292,7 @@ class ExercisesTestCase(WorkoutManagerTestCase):
{'category': 1,
'name': 'my test exercise',
'muscles': []})
- self.assertTrue(response.context['form'].errors['muscles'])
+ self.assertFalse(response.context['form'].errors.get('muscles'))
# No muscles - editing
response = self.client.post(reverse('exercise-edit', kwargs={'pk': '1'}),
@@ -300,7 +300,7 @@ class ExercisesTestCase(WorkoutManagerTestCase):
'name': 'my test exercise',
'muscles': []})
if admin:
- self.assertTrue(response.context['form'].errors['muscles'])
+ self.assertFalse(response.context['form'].errors.get('muscles'))
else:
self.assertIn(response.status_code, STATUS_CODES_FAIL)
diff --git a/wger/exercises/views/exercises.py b/wger/exercises/views/exercises.py
index 4410f6651..bce276167 100644
--- a/wger/exercises/views/exercises.py
+++ b/wger/exercises/views/exercises.py
@@ -25,6 +25,7 @@ from django.http import HttpResponseRedirect
from django.http import HttpResponseForbidden
from django.forms import ModelForm
from django.forms import ModelChoiceField
+from django.forms import ModelMultipleChoiceField
from django.core import mail
from django.core.cache import cache
from django.core.urlresolvers import reverse
@@ -37,12 +38,14 @@ from django.views.generic import ListView
from django.views.generic import DeleteView
from django.views.generic import CreateView
from django.views.generic import UpdateView
+
from easy_thumbnails.files import get_thumbnailer
from easy_thumbnails.alias import aliases
from wger.manager.models import WorkoutLog
-from wger.exercises.models import Exercise
+from wger.exercises.models import Exercise, Muscle
from wger.exercises.models import ExerciseCategory
+from wger.exercises.widgets import MuscleTranslatedSelectMultiple
from wger.utils.generic_views import WgerFormMixin
from wger.utils.generic_views import WgerDeleteMixin
from wger.utils.generic_views import WgerPermissionMixin
@@ -153,9 +156,9 @@ class ExercisesEditAddView(WgerFormMixin):
form_fields = ['name',
'category',
+ 'description',
'muscles',
'muscles_secondary',
- 'description',
'equipment']
title = ugettext_lazy('Add exercise')
@@ -168,9 +171,15 @@ class ExercisesEditAddView(WgerFormMixin):
# have we access to the currently used language. In other places Django defaults
# to 'en-us'.
class ExerciseForm(ModelForm):
- #language = load_language()
category = ModelChoiceField(queryset=ExerciseCategory.objects.all(),
widget=TranslatedSelect())
+ muscles = ModelMultipleChoiceField(queryset=Muscle.objects.all(),
+ widget=MuscleTranslatedSelectMultiple(),
+ required=False)
+
+ muscles_secondary = ModelMultipleChoiceField(queryset=Muscle.objects.all(),
+ widget=MuscleTranslatedSelectMultiple(),
+ required=False)
class Meta:
model = Exercise
@@ -191,9 +200,8 @@ class ExerciseUpdateView(ExercisesEditAddView, UpdateView, WgerPermissionMixin):
def form_valid(self, form):
'''
- Set the user field, otherwise it will get reset to Null
+ Set the user
'''
-
form.instance.user = Exercise.objects.get(pk=self.object.id).user
return super(ExerciseUpdateView, self).form_valid(form)
@@ -216,9 +224,9 @@ class ExerciseAddView(ExercisesEditAddView, CreateView, WgerPermissionMixin):
def form_valid(self, form):
'''
Set the user that submitted the exercise
- '''
- # set the submitter, if admin, set approrpiate status
+ If admin, set appropriate status
+ '''
form.instance.user = self.request.user.username
form.instance.language = load_language()
if self.request.user.has_perm('exercises.add_exercise'):
@@ -254,10 +262,11 @@ class ExerciseDeleteView(WgerDeleteMixin, DeleteView):
messages = ugettext_lazy('Exercise successfully deleted')
permission_required = 'exercises.delete_exercise'
- # Send some additional data to the template
def get_context_data(self, **kwargs):
+ '''
+ Send some additional data to the template
+ '''
context = super(ExerciseDeleteView, self).get_context_data(**kwargs)
-
context['title'] = _('Delete exercise %s?') % self.object.name
context['form_action'] = reverse('exercise-delete', kwargs={'pk': self.kwargs['pk']})
diff --git a/wger/exercises/widgets.py b/wger/exercises/widgets.py
new file mode 100644
index 000000000..1fa760330
--- /dev/null
+++ b/wger/exercises/widgets.py
@@ -0,0 +1,39 @@
+# -*- 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.forms.widgets import SelectMultiple
+
+from django.utils.translation import ugettext as _
+
+
+class MuscleTranslatedSelectMultiple(SelectMultiple):
+ '''
+ A SelectMultiple widget that translates the options
+ '''
+
+ def render_option(self, selected_choices, option_value, option_label):
+
+ # No translation, output only the original
+ if option_label == _(option_label):
+ out_string = option_label
+
+ # There is a translation, show both
+ else:
+ out_string = '{0} - {1}'.format(option_label, _(option_label))
+
+ return super(MuscleTranslatedSelectMultiple, self).render_option(selected_choices,
+ option_value,
+ out_string)