Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Rendering multiple choices with bootstrap checkboxes using a custom widget and a custom renderer.

Alexandre Murphy-Gonthierf50a884ab390
diff --git a/wger/core/templates/tags/render_form_element.html b/wger/core/templates/tags/render_form_element.html
index 6b1e458fb..a3a41e244 100644
--- a/wger/core/templates/tags/render_form_element.html
+++ b/wger/core/templates/tags/render_form_element.html
@@ -12,9 +12,9 @@
</label>
<div class="col-md-9">
- {% if not field|is_checkbox and not field|is_fileupload %}
+ {% if not field|is_checkbox and not field|is_fileupload and not field|is_multiple %}
{{ field|form_field_add_css:'form-control' }}
- {% elif field|is_checkbox %}
+ {% elif field|is_checkbox and not field|is_multiple %}
{{ field|form_field_add_css:'checkbox' }}
{% else %}
{{ field }}
@@ -37,4 +37,4 @@
</div>
{% else %}
{{field}}
-{% endif %}
\ No newline at end of file
+{% endif %}
diff --git a/wger/core/templatetags/wger_extras.py b/wger/core/templatetags/wger_extras.py
index b2c6128c6..24eccd1ab 100644
--- a/wger/core/templatetags/wger_extras.py
+++ b/wger/core/templatetags/wger_extras.py
@@ -21,6 +21,7 @@ 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
register = template.Library()
@@ -213,6 +214,17 @@ def is_checkbox(field):
return field.field.widget.__class__.__name__ == CheckboxInput().__class__.__name__
[email protected](name='is_multiple')
+def is_multiple(field):
+ '''
+ Tests if a field element is a checkbox, as it needs to be handled slightly different
+
+ :param field: a form field
+ :return: boolen
+ '''
+ return isinstance(field.field.widget, CheckboxBootstrapSelectMultiple)
+
+
@register.filter(name='is_fileupload')
def is_fileupload(field):
'''
diff --git a/wger/exercises/widgets.py b/wger/exercises/widgets.py
index 85550f7ac..2ffe1c090 100644
--- a/wger/exercises/widgets.py
+++ b/wger/exercises/widgets.py
@@ -14,12 +14,12 @@
#
# 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 _
+from wger.utils.widgets import CheckboxBootstrapSelectMultiple
+
-class MuscleTranslatedSelectMultiple(SelectMultiple):
+class MuscleTranslatedSelectMultiple(CheckboxBootstrapSelectMultiple):
'''
A SelectMultiple widget that translates the options
'''
diff --git a/wger/gym/forms.py b/wger/gym/forms.py
index 54411f2bf..ecbefd2f5 100644
--- a/wger/gym/forms.py
+++ b/wger/gym/forms.py
@@ -17,7 +17,9 @@
from django.contrib.auth.models import User
from django import forms
from django.utils.translation import ugettext as _
+
from wger.core.forms import UserPersonalInformationForm
+from wger.utils.widgets import CheckboxBootstrapSelectMultiple
class GymUserPermisssionForm(forms.ModelForm):
@@ -54,7 +56,8 @@ class GymUserPermisssionForm(forms.ModelForm):
field_choices.append((self.MANAGER, _('General manager')))
self.fields['role'] = forms.MultipleChoiceField(choices=field_choices,
- initial=User)
+ initial=User,
+ widget=CheckboxBootstrapSelectMultiple())
class GymUserAddForm(GymUserPermisssionForm, UserPersonalInformationForm):
@@ -64,6 +67,7 @@ class GymUserAddForm(GymUserPermisssionForm, UserPersonalInformationForm):
class Meta:
model = User
+ widgets = {'role': CheckboxBootstrapSelectMultiple()}
fields = ('first_name', 'last_name', 'username', 'email', 'role',)
username = forms.RegexField(label=_("Username"),
diff --git a/wger/utils/widgets.py b/wger/utils/widgets.py
index 73a81a352..8912c895c 100644
--- a/wger/utils/widgets.py
+++ b/wger/utils/widgets.py
@@ -18,10 +18,12 @@ import logging
from itertools import chain
from django.forms.widgets import (
- SelectMultiple,
- Select,
+ CheckboxFieldRenderer,
+ CheckboxSelectMultiple,
DateInput,
- TextInput
+ Select,
+ SelectMultiple,
+ TextInput,
)
from django.forms import fields
@@ -142,7 +144,16 @@ class ExerciseAjaxSelect(SelectMultiple):
return ''
-class TranslatedSelectMultiple(SelectMultiple):
+class CheckboxBootstrapSelectMultipleRenderer(CheckboxFieldRenderer):
+ outer_html = '<div{id_attr}>{content}</div>'
+ inner_html = '<div class="checkbox">{choice_value}{sub_widgets}</div>'
+
+
+class CheckboxBootstrapSelectMultiple(CheckboxSelectMultiple):
+ renderer = CheckboxBootstrapSelectMultipleRenderer
+
+
+class TranslatedSelectMultiple(CheckboxBootstrapSelectMultiple):
'''
A SelectMultiple widget that translates the options
'''