Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Provide an alternative to the exercise search for browsers without JavaScript

Roland Geidere348c4f49664
diff --git a/exercises/templates/exercise_search.html b/exercises/templates/exercise_search.html
new file mode 100644
index 000000000..f64d02246
--- /dev/null
+++ b/exercises/templates/exercise_search.html
@@ -0,0 +1,36 @@
+{% extends "base.html" %}
+{% load i18n %}
+
+<!--
+ Title
+-->
+{% block title %}{% trans "Search results" %}{% endblock %}
+
+
+<!--
+ Main Content
+-->
+{% block content %}
+
+<h2>{% trans "Search results" %} - &quot;{{ search_term }}&quot;</h2>
+
+<form action="{% url exercises.views.exercise_search %}"
+ method="get"
+ id="exercise_search_form">
+ {% csrf_token %}
+ <input name="term" id="exercise-search">
+</form>
+
+<ul>
+{% for exercise in exercises %}
+ <li><a href="{% url exercises.views.exercise_view exercise.id %}">{{ exercise.name }}</a></li>
+{% endfor %}
+</ul>
+
+{% endblock %}
+
+
+<!--
+ Side bar
+-->
+{% block sidebar %}{% endblock %}
\ No newline at end of file
diff --git a/exercises/templates/overview.html b/exercises/templates/overview.html
index b8f186342..7849b26fc 100644
--- a/exercises/templates/overview.html
+++ b/exercises/templates/overview.html
@@ -16,6 +16,9 @@ $(document).ready(function() {
// Init the tabs for the exercises
$( "#tabs" ).tabs();
+
+ // Prevent the form from being submited if JavaScript is enabled
+ $("#exercise_search_form").submit(function() {return false;});
// Init the autocompleter
$("#exercise-search").autocomplete({
@@ -142,7 +145,7 @@ $(document).ready(function() {
alt="{% trans 'Add new exercise' %}"
title="{% trans 'Add new exercise' %}">
{% trans "Add new exercise" %}
- </a>
+ </a>
</p>
<p>
<a href="{% url exercises.views.exercise_category_edit None %}">
@@ -152,14 +155,19 @@ $(document).ready(function() {
alt="{% trans 'Add category' %}"
title="{% trans 'Add category' %}">
{% trans "Add category" %}
- </a>
+ </a>
</p>
{% endif %}
<h3>{% trans "Search" %}</h3>
<div class="ui-widget">
- <input id="exercise-search">
+ <form action="{% url exercises.views.exercise_search %}"
+ method="get"
+ id="exercise_search_form">
+ {% csrf_token %}
+ <input name="term" id="exercise-search">
+ </form>
</div>
<div>
diff --git a/exercises/views.py b/exercises/views.py
index 88c61d797..6ca290d3c 100644
--- a/exercises/views.py
+++ b/exercises/views.py
@@ -82,6 +82,8 @@ def exercise_overview(request):
language = get_object_or_404(Language, short_name = translation.get_language())
template_data = {}
+ template_data.update(csrf(request))
+
template_data['categories'] = ExerciseCategory.objects.filter(language = language.id)
template_data['active_tab'] = 'exercises'
@@ -233,11 +235,13 @@ def exercise_search(request):
"""Search an exercise, return the result as a JSON list
"""
+ # Perform the search
+ q = request.GET.get('term', '')
+ exercises = Exercise.objects.filter(name__icontains = q )[:20]
+
+ # AJAX-request, this comes from the autocompleter. Create a list and send it back as JSON
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 = {}
@@ -246,9 +250,19 @@ def exercise_search(request):
exercise_json['value'] = exercise.name
results.append(exercise_json)
data = json.dumps(results)
+
+ # Return the results to the server
+ mimetype = 'application/json'
+ return HttpResponse(data, mimetype)
+
+ # Usual search (perhaps JS disabled), present the results as normal HTML page
else:
- data = 'fail'
+ template_data = {}
+ template_data.update(csrf(request))
+ template_data['exercises'] = exercises
+ template_data['search_term'] = q
+ return render_to_response('exercise_search.html',
+ template_data,
+ context_instance=RequestContext(request))
- # Return the results to the server
- mimetype = 'application/json'
- return HttpResponse(data, mimetype)
+