Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add an autocompleter to the meal item form (not 100% functional)

Roland Geider5ab2f0db8afd
diff --git a/exercises/templates/edit_exercise.html b/exercises/templates/edit_exercise.html
index 3ec4eeaef..42f14f320 100644
--- a/exercises/templates/edit_exercise.html
+++ b/exercises/templates/edit_exercise.html
@@ -7,7 +7,7 @@
<script src="{% static 'js/workout-manager.js' %}"></script>
<script type="text/javascript">
init_tinymce();
-</script>
+</script>
{% endblock %}
<!--
diff --git a/nutrition/templates/edit_meal_item.html b/nutrition/templates/edit_meal_item.html
index 9ecc7ba34..c66865239 100644
--- a/nutrition/templates/edit_meal_item.html
+++ b/nutrition/templates/edit_meal_item.html
@@ -5,6 +5,28 @@
{% block title %}{% trans "Edit meal" %}{% endblock %}
+{% block header %}
+<script src="{% static 'js/jquery-ui.js' %}"></script>
+<script src="{% static 'js/workout-manager.js' %}"></script>
+<script type="text/javascript">
+$(document).ready(function() {
+ // Init the autocompleter
+ $("#id_ingredient_freetext").autocomplete({
+ source: "{% url nutrition.views.ingredient_search %}",
+ minLength: 2,
+ select: function(event, ui) {
+
+ // TODO: do something with the result
+ // result ID can be accessed with ui.item.id
+ }
+ });
+});
+</script>
+{% endblock %}
+
+
+
+
{% block content %}
<h2>{% trans "Edit meal" %}</h2>
@@ -17,7 +39,9 @@
<table>
{% include "form_snippet.html" with field=form.amount_gramm %}
{% include "form_snippet.html" with field=form.amount_freetext %}
+ {% comment %}
{% include "form_snippet.html" with field=form.ingredient css_class="ym-fbox-select" %}
+ {% endcomment %}
{% include "form_snippet.html" with field=form.ingredient_freetext %}
<tr>
<td><input type="submit" value="{% trans "Save" %}" /></td>
diff --git a/nutrition/urls.py b/nutrition/urls.py
index 17531c2f9..bb6f4a7ac 100644
--- a/nutrition/urls.py
+++ b/nutrition/urls.py
@@ -22,7 +22,7 @@ urlpatterns = patterns('nutrition.views',
url(r'^nutrition/ingredient/view/(?P<id>\d+)$', 'ingredient_view'),
url(r'^nutrition/ingredient/edit/(?P<id>\w*)$', 'ingredient_edit'),
url(r'^nutrition/ingredient/delete/(?P<id>\w*)$', 'delete_ingredient'),
-
+ url(r'^nutrition/ingredient/search/$', 'ingredient_search'),
#url(r'^nutrition/export_pdf/$', 'export_pdf'),
)
diff --git a/nutrition/views.py b/nutrition/views.py
index 4f4cfd503..23be9d2c7 100644
--- a/nutrition/views.py
+++ b/nutrition/views.py
@@ -12,10 +12,12 @@
#
# You should have received a copy of the GNU Affero General Public License
import logging
+import json
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.shortcuts import get_object_or_404
+from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.http import HttpResponseForbidden
from django.core.context_processors import csrf
@@ -377,3 +379,39 @@ def ingredient_edit(request, id=None):
return render_to_response('edit_ingredient.html',
template_data,
context_instance=RequestContext(request))
+
+
+def ingredient_search(request):
+ """Search for an exercise, return the result as a JSON list or as HTML page, depending on how
+ the function was invoked
+ """
+
+ # Perform the search
+ q = request.GET.get('term', '')
+ ingredients = Ingredient.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():
+
+ results = []
+ for ingredient in ingredients:
+ ingredient_json = {}
+ ingredient_json['id'] = ingredient.id
+ ingredient_json['name'] = ingredient.name
+ ingredient_json['value'] = ingredient.name
+ results.append(ingredient_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:
+ template_data = {}
+ template_data.update(csrf(request))
+ template_data['ingredients'] = ingredients
+ template_data['search_term'] = q
+ return render_to_response('ingredient_search.html',
+ template_data,
+ context_instance=RequestContext(request))
\ No newline at end of file