Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add an AJAX search to the exercise page

Roland Geider8212406772a4
diff --git a/manager/templates/exercise/overview.html b/manager/templates/exercise/overview.html
index dbcccb162..6fcb956f8 100644
--- a/manager/templates/exercise/overview.html
+++ b/manager/templates/exercise/overview.html
@@ -10,12 +10,26 @@
{% block header %}
-jquery-ui-1.8.23.custom
<script src="{% static "js/jquery-ui.js" %}"></script>
<script>
$(document).ready(function() {
- $( "#tabs" ).tabs();
+
+ // Init the tabs for the exercises
+ $( "#tabs" ).tabs();
+
+ // Init the autocompleter
+ $("#exercise-search").autocomplete({
+ source: "/exercise/search/",
+ minLength: 2,
+ select: function(event, ui) {
+
+ // Redirect to exercise after clicking on a result
+ window.location.href = '/exercise/view/' + ui.item.id
+ }
+ });
+
});
+
</script>
{% endblock %}
@@ -27,11 +41,6 @@ jquery-ui-1.8.23.custom
<h2> {% trans "Exercises" %} </h2>
-<p>
- <a href="/exercise/edit">{% trans "Add new exercise" %}</a> |
- <a href="/exercise/category/edit">{% trans "Add category" %}</a>
-</p>
-
<div id="tabs">
<ul>
{% for category in categories %}
@@ -70,13 +79,30 @@ jquery-ui-1.8.23.custom
{% endfor %}
</div>
+{% endblock %}
+<!--
+ Side bar?
+-->
+{% block sidebar %}
+ <h3>{% trans "Options" %}</h3>
+ <p>
+ <a href="/exercise/edit">{% trans "Add new exercise" %}</a> |
+ <a href="/exercise/category/edit">{% trans "Add category" %}</a>
+ </p>
-{% endblock %}
+ <h3>{% trans "Search" %}</h3>
+ <div class="ui-widget">
+ <input id="exercise-search">
+ </div>
-<!--
- Side bar?
--->
\ No newline at end of file
+ <div>
+ <p class="dimmed">
+ {% trans "Use the search box to search the name of any exercise." %}
+ </p>
+ </div>
+
+{% endblock %}
\ No newline at end of file
diff --git a/manager/urls.py b/manager/urls.py
index 73397d4d8..d885533b9 100644
--- a/manager/urls.py
+++ b/manager/urls.py
@@ -18,6 +18,7 @@ urlpatterns = patterns('manager.views',
url(r'^exercise/overview/$', 'exercise_overview'),
+ url(r'^exercise/search/$', 'exercise_search'),
url(r'^exercise/view/(?P<id>\d+)$', 'exercise_view'),
url(r'^exercise/view/(?P<id>\d+)/edit/comment/(?P<comment_id>\d+)$', 'exercise_view'),
url(r'^exercise/edit/(?P<id>\d*)$', 'exercise_edit'),
diff --git a/manager/views.py b/manager/views.py
index 911623863..5dec3d874 100644
--- a/manager/views.py
+++ b/manager/views.py
@@ -13,6 +13,7 @@
# You should have received a copy of the GNU Affero General Public License
import logging
import calendar
+import json
from django.shortcuts import render_to_response
from django.shortcuts import get_object_or_404
@@ -485,6 +486,31 @@ def exercise_category_delete(request, id):
return HttpResponseRedirect('/exercise/overview/')
+def exercise_search(request):
+ """Search an exercise, return the result as a JSON list
+ """
+
+ if request.is_ajax():
+ q = request.GET.get('term', '')
+
+ # Perform the search
+ exercises = Exercise.objects.filter(name__icontains = q )[:20]
+ results = []
+ for exercise in exercises:
+ exercise_json = {}
+ exercise_json['id'] = exercise.id
+ exercise_json['name'] = exercise.name
+ exercise_json['value'] = exercise.name
+ results.append(exercise_json)
+ data = json.dumps(results)
+ else:
+ data = 'fail'
+
+ # Return the results to the server
+ mimetype = 'application/json'
+ return HttpResponse(data, mimetype)
+
+
# ************************
# Settings functions
# ************************