Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Use a custom widget to select exercises for sets

Roland Geider01db65da41bc
diff --git a/manager/static/js/workout-manager.js b/manager/static/js/workout-manager.js
index 66656deee..06621b0a3 100644
--- a/manager/static/js/workout-manager.js
+++ b/manager/static/js/workout-manager.js
@@ -390,9 +390,6 @@ function init_edit_set()
});
- // The multi-select exercise list is not needed if javascript is activated
- $('#form-exercises').remove();
-
// Initialise the autocompleter (our widget, defined above)
$("#exercise-search").catcomplete({
source: "/exercise/search/",
diff --git a/manager/templates/set/edit.html b/manager/templates/set/edit.html
index 6465bce4c..13bfe7b3f 100644
--- a/manager/templates/set/edit.html
+++ b/manager/templates/set/edit.html
@@ -11,14 +11,13 @@
Header
-->
{% block header %}
-<script src="{% static 'js/jquery.validate.js' %}"></script>
+<script src="{% static 'js/jquery.validate.js' %}"></script>
<script>
$(document).ready(function() {
init_edit_set();
});
</script>
-
{% endblock %}
<!--
@@ -32,36 +31,11 @@ $(document).ready(function() {
<form action="{% url manager.views.edit_set workout.id day.id set.id %}"
method="post"
class="ym-form">
+
{% csrf_token %}
<table>
-
-
- <tr id="form-ajax-{{set_form.exercises.name}}">
- <td style="text-align:right;">
- {% if set_form.exercises.errors %}<div class="ym-fbox-text ym-error"><span class="ym-message">{% endif %}
- {% trans set_form.exercises.label %}
- {% if set_form.exercises.errors %}</span></div>{% endif %}
- </td>
- <td>
- <div class="ym-fbox-text {% if set_form.exercises.errors %}ym-error{% endif %}">
- <input type="text" id="exercise-search">
- </div>
-
- {% if set_form.exercises.errors %}
- <div class="ym-fbox-text ym-error">
- {% for error in set_form.exercises.errors %}
- <p class="ym-message">{% trans error %}</p>
- {% endfor %}
- </div>
- {% endif %}
-
-
- <div id="exercise-search-log"></div>
- </td>
- </tr>
-
- {% include "form_snippet.html" with field=set_form.exercises css_class="ym-fbox-select" %}
+ {% include "form_snippet.html" with field=set_form.exercises %}
{% include "form_snippet.html" with field=set_form.sets %}
<tr>
diff --git a/manager/views.py b/manager/views.py
index 104e7a07a..e0b611884 100644
--- a/manager/views.py
+++ b/manager/views.py
@@ -72,6 +72,8 @@ from workout_manager.constants import WORKOUT_TAB
from workout_manager.constants import USER_TAB
from workout_manager.generic_views import YamlFormMixin
from workout_manager.generic_views import YamlDeleteMixin
+from workout_manager.widgets import ExerciseAjaxSelect
+
logger = logging.getLogger('workout_manager.custom')
@@ -566,10 +568,22 @@ def view_day(request, id):
# ************************
# Set functions
# ************************
+
class SetForm(ModelForm):
class Meta:
model = Set
exclude = ('exerciseday', 'order',)
+ widgets = {
+ 'exercises': ExerciseAjaxSelect(),
+ }
+
+ # We need to overwrite the init method here because otherwise Django
+ # will outut a default help text, regardless of the widget used
+ # https://code.djangoproject.com/ticket/9321
+ def __init__(self, *args, **kwargs):
+ super(SetForm, self).__init__(*args, **kwargs)
+ self.fields['exercises'].help_text = _('You can search for more than one exercise, '
+ 'they will be grouped together for a superset.')
@login_required
def edit_set(request, id, day_id, set_id=None):
diff --git a/workout_manager/widgets.py b/workout_manager/widgets.py
new file mode 100644
index 000000000..214a15a89
--- /dev/null
+++ b/workout_manager/widgets.py
@@ -0,0 +1,95 @@
+# -*- coding: utf-8 -*-
+
+# This file is part of Workout Manager.
+#
+# 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.
+#
+# 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 Widget
+from django.forms.util import flatatt
+from itertools import chain
+from django.utils.encoding import force_unicode
+from django.utils.html import escape
+from django.utils.html import conditional_escape
+from django.utils.safestring import mark_safe
+from django.utils.datastructures import MultiValueDict
+from django.utils.datastructures import MergeDict
+
+
+
+class ExerciseAjaxSelect(Widget):
+ '''
+ Custom widget that allows to select exercises from an autocompleter
+
+ This is basically modified MultipleSelect widget
+ '''
+
+ def __init__(self, attrs=None, choices=()):
+ super(ExerciseAjaxSelect, self).__init__(attrs)
+ # choices can be any iterable, but we may need to render this widget
+ # multiple times. Thus, collapse it into a list so it can be consumed
+ # more than once.
+ self.choices = list(choices)
+
+
+ def render(self, name, value, attrs=None, choices=()):
+ if value is None: value = []
+ final_attrs = self.build_attrs(attrs, name=name)
+
+ output = [u'<div class="ym-fbox-text">']
+ output.append(u'<input type="text" id="exercise-search">')
+ output.append(u'</div>')
+
+ options = self.render_options(choices, value)
+ if options:
+ output.append(options)
+
+ output.append('<div id="exercise-search-log"></div>')
+ return mark_safe(u'\n'.join(output))
+
+
+ def render_options(self, choices, selected_choices):
+ # Normalize to strings.
+ selected_choices = set(force_unicode(v) for v in selected_choices)
+ output = []
+ for option_value, option_label in chain(self.choices, choices):
+ output.append(self.render_option(selected_choices, option_value, option_label))
+ return u'\n'.join(output)
+
+
+ def render_option(self, selected_choices, option_value, option_label):
+ option_value = force_unicode(option_value)
+ if option_value in selected_choices:
+
+ return u'''
+ <div id="DIV-ID" class="ajax-exercise-select">
+ <a href="#">
+ <img src="/static/images/icons/status-off.svg"
+ width="14"
+ height="14"
+ alt="Delete">
+ </a> %(value)s
+ <input type="hidden" name="exercises" value="%(id)s">
+ </div>
+ ''' % { 'value': conditional_escape(force_unicode(option_label)),
+ 'id': escape(option_value)
+ }
+
+ else:
+ return ''
+
+ def value_from_datadict(self, data, files, name):
+ if isinstance(data, (MultiValueDict, MergeDict)):
+ return data.getlist(name)
+ return data.get(name, None)
+