Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Make the translated checkbox widgets work again

Roland Geiderb8b189d51c8c
diff --git a/wger/core/templatetags/wger_extras.py b/wger/core/templatetags/wger_extras.py
index 24eccd1ab..e1ad1bced 100644
--- a/wger/core/templatetags/wger_extras.py
+++ b/wger/core/templatetags/wger_extras.py
@@ -15,13 +15,24 @@
# You should have received a copy of the GNU Affero General Public License
from django import template
-from django.forms.widgets import CheckboxInput, ClearableFileInput
-from django.utils.translation import ugettext_lazy as _
-from django.utils.translation import pgettext
from django.conf import settings
-
-from wger.utils.constants import PAGINATION_MAX_TOTAL_PAGES, PAGINATION_PAGES_AROUND_CURRENT
-from wger.exercises.widgets import CheckboxBootstrapSelectMultiple
+from django.forms.widgets import (
+ CheckboxInput,
+ ClearableFileInput
+)
+from django.utils.translation import (
+ ugettext_lazy as _,
+ pgettext
+)
+
+from wger.utils.constants import (
+ PAGINATION_MAX_TOTAL_PAGES,
+ PAGINATION_PAGES_AROUND_CURRENT
+)
+from wger.utils.widgets import (
+ BootstrapSelectMultipleTranslatedOriginal,
+ BootstrapSelectMultiple
+)
register = template.Library()
@@ -222,7 +233,8 @@ def is_multiple(field):
:param field: a form field
:return: boolen
'''
- return isinstance(field.field.widget, CheckboxBootstrapSelectMultiple)
+ return isinstance(field.field.widget, BootstrapSelectMultiple) \
+ or isinstance(field.field.widget, BootstrapSelectMultipleTranslatedOriginal)
@register.filter(name='is_fileupload')
diff --git a/wger/exercises/views/exercises.py b/wger/exercises/views/exercises.py
index 035584e9a..0d91da7b6 100644
--- a/wger/exercises/views/exercises.py
+++ b/wger/exercises/views/exercises.py
@@ -53,7 +53,11 @@ from wger.utils.generic_views import (
)
from wger.utils.language import load_language, load_item_languages
from wger.utils.cache import cache_mapper
-from wger.utils.widgets import TranslatedSelect, TranslatedSelectMultiple
+from wger.utils.widgets import (
+ TranslatedSelect,
+ TranslatedSelectMultiple,
+ TranslatedOriginalSelectMultiple
+)
from wger.config.models import LanguageConfig
from wger.weight.helpers import process_log_entries
@@ -172,11 +176,11 @@ class ExercisesEditAddView(WgerFormMixin):
category = ModelChoiceField(queryset=ExerciseCategory.objects.all(),
widget=TranslatedSelect())
muscles = ModelMultipleChoiceField(queryset=Muscle.objects.all(),
- widget=MuscleTranslatedSelectMultiple(),
+ widget=TranslatedOriginalSelectMultiple(),
required=False)
muscles_secondary = ModelMultipleChoiceField(queryset=Muscle.objects.all(),
- widget=MuscleTranslatedSelectMultiple(),
+ widget=TranslatedOriginalSelectMultiple(),
required=False)
class Meta:
diff --git a/wger/exercises/widgets.py b/wger/exercises/widgets.py
index 2ffe1c090..7bd75f606 100644
--- a/wger/exercises/widgets.py
+++ b/wger/exercises/widgets.py
@@ -16,10 +16,10 @@
from django.utils.translation import ugettext as _
-from wger.utils.widgets import CheckboxBootstrapSelectMultiple
+from wger.utils.widgets import BootstrapSelectMultiple
-class MuscleTranslatedSelectMultiple(CheckboxBootstrapSelectMultiple):
+class MuscleTranslatedSelectMultiple(BootstrapSelectMultiple):
'''
A SelectMultiple widget that translates the options
'''
diff --git a/wger/gym/forms.py b/wger/gym/forms.py
index ecbefd2f5..0c0ebcb70 100644
--- a/wger/gym/forms.py
+++ b/wger/gym/forms.py
@@ -19,7 +19,7 @@ from django import forms
from django.utils.translation import ugettext as _
from wger.core.forms import UserPersonalInformationForm
-from wger.utils.widgets import CheckboxBootstrapSelectMultiple
+from wger.utils.widgets import BootstrapSelectMultiple
class GymUserPermisssionForm(forms.ModelForm):
@@ -57,7 +57,7 @@ class GymUserPermisssionForm(forms.ModelForm):
self.fields['role'] = forms.MultipleChoiceField(choices=field_choices,
initial=User,
- widget=CheckboxBootstrapSelectMultiple())
+ widget=BootstrapSelectMultiple())
class GymUserAddForm(GymUserPermisssionForm, UserPersonalInformationForm):
@@ -67,7 +67,7 @@ class GymUserAddForm(GymUserPermisssionForm, UserPersonalInformationForm):
class Meta:
model = User
- widgets = {'role': CheckboxBootstrapSelectMultiple()}
+ widgets = {'role': BootstrapSelectMultiple()}
fields = ('first_name', 'last_name', 'username', 'email', 'role',)
username = forms.RegexField(label=_("Username"),
diff --git a/wger/software/templates/changelog.html b/wger/software/templates/changelog.html
index 87c849018..da2680d64 100644
--- a/wger/software/templates/changelog.html
+++ b/wger/software/templates/changelog.html
@@ -14,6 +14,10 @@
Remove hard-coded CC licence from documentation and website
<a href="https://github.com/rolandgeider/wger/issues/247">#247</a>
</li>
+ <li>
+ Other improvements and bugfixes
+ <a href="https://github.com/rolandgeider/wger/issues/258">#258</a>
+ </li>
</ul>
diff --git a/wger/utils/widgets.py b/wger/utils/widgets.py
index 8912c895c..be8f62f12 100644
--- a/wger/utils/widgets.py
+++ b/wger/utils/widgets.py
@@ -18,13 +18,13 @@ import logging
from itertools import chain
from django.forms.widgets import (
- CheckboxFieldRenderer,
CheckboxSelectMultiple,
DateInput,
Select,
SelectMultiple,
TextInput,
-)
+ ChoiceFieldRenderer,
+ ChoiceInput)
from django.forms import fields
@@ -144,24 +144,82 @@ class ExerciseAjaxSelect(SelectMultiple):
return ''
-class CheckboxBootstrapSelectMultipleRenderer(CheckboxFieldRenderer):
- outer_html = '<div{id_attr}>{content}</div>'
- inner_html = '<div class="checkbox">{choice_value}{sub_widgets}</div>'
+class CheckboxChoiceInputTranslated(ChoiceInput):
+ '''
+ Overwritten ChoiceInput
+
+ This only translated the text for the select widgets
+ '''
+ input_type = 'checkbox'
+ def __init__(self, name, value, attrs, choice, index):
+ choice = (choice[0], _(choice[1]))
-class CheckboxBootstrapSelectMultiple(CheckboxSelectMultiple):
- renderer = CheckboxBootstrapSelectMultipleRenderer
+ super(CheckboxChoiceInputTranslated, self).__init__(name, value, attrs, choice, index)
-class TranslatedSelectMultiple(CheckboxBootstrapSelectMultiple):
+class CheckboxChoiceInputTranslatedOriginal(ChoiceInput):
+ '''
+ Overwritten ChoiceInput
+
+ This only translated the text for the select widgets, showing the original
+ string as well.
+ '''
+ input_type = 'checkbox'
+
+ def __init__(self, name, value, attrs, choice, index):
+ if _(choice[1]) != choice[1]:
+ choice = (choice[0], u"{0} ({1})".format(choice[1], _(choice[1])))
+ else:
+ choice = (choice[0], _(choice[1]))
+
+ super(CheckboxChoiceInputTranslatedOriginal, self).__init__(name,
+ value,
+ attrs,
+ choice,
+ index)
+
+
+class CheckboxFieldRendererTranslated(ChoiceFieldRenderer):
+ choice_input_class = CheckboxChoiceInputTranslated
+
+
+class CheckboxFieldRendererTranslatedOriginal(ChoiceFieldRenderer):
+ choice_input_class = CheckboxChoiceInputTranslatedOriginal
+
+
+class CheckboxBootstrapRenderer(CheckboxFieldRendererTranslated):
+ outer_html = u'<div{id_attr}>{content}</div>'
+ inner_html = u'<div class="checkbox">{choice_value}{sub_widgets}</div>'
+
+
+class CheckboxBootstrapRendererTranslatedOriginal(CheckboxFieldRendererTranslatedOriginal):
+ outer_html = u'<div{id_attr}>{content}</div>'
+ inner_html = u'<div class="checkbox">{choice_value}{sub_widgets}</div>'
+
+
+class BootstrapSelectMultiple(CheckboxSelectMultiple):
+ renderer = CheckboxBootstrapRenderer
+
+
+class BootstrapSelectMultipleTranslatedOriginal(CheckboxSelectMultiple):
+ renderer = CheckboxBootstrapRendererTranslatedOriginal
+
+
+class TranslatedSelectMultiple(BootstrapSelectMultiple):
'''
A SelectMultiple widget that translates the options
'''
+ pass
- def render_option(self, selected_choices, option_value, option_label):
- return super(TranslatedSelectMultiple, self).render_option(selected_choices,
- option_value,
- _(option_label))
+
+class TranslatedOriginalSelectMultiple(BootstrapSelectMultipleTranslatedOriginal):
+ '''
+ A SelectMultiple widget that translates the options, showing the original
+ string as well. This is currently only used in the muscle list, where the
+ translated muscles as well as the latin names are shown.
+ '''
+ pass
class TranslatedSelect(Select):