Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add search field to ingredient overview
Roland Geider8fdc1edfb21c
diff --git a/wger/nutrition/templates/ingredient_overview.html b/wger/nutrition/templates/ingredient_overview.htmlindex 0274ddbc3..4987dcdc4 100644--- a/wger/nutrition/templates/ingredient_overview.html+++ b/wger/nutrition/templates/ingredient_overview.html@@ -5,6 +5,27 @@{% load manager_extras %}+{% block header %}+<script>+$(document).ready(function() {+ // Prevent the form from being submited if JavaScript is enabled+ $("#search_form").submit(function() {return false;});++ // Init the autocompleter+ $("#ingredient-search").autocomplete({+ source: "{% url 'ingredient-search' %}",+ minLength: 2,+ select: function(event, ui) {++ // Redirect to exercise after clicking on a result+ // Path is hard coded here, because we can't use the url-tag+ window.location.href = '/nutrition/ingredient/' + ui.item.id + '/view/'+ }+ });+});+</script>+{% endblock %}+{% block title %}{% trans "Ingredient overview" %}{% endblock %}@@ -31,6 +52,21 @@{% endblock %}{% block sidebar %}+<h3>{% trans "Search" %}</h3>+<div class="ui-widget">+ <form action="{% url 'ingredient-search' %}"+ method="get"+ id="search_form">+ {% csrf_token %}+ <input name="term"+ type="search"+ id="ingredient-search"+ class="ajax-form-element"+ placeholder="{% trans 'ingredient name' %}"+ style="width:100%;">+ </form>+</div>+{% if perms.exercises %}<h3>{% trans "Options" %}</h3><p>diff --git a/wger/nutrition/tests/ingredient.py b/wger/nutrition/tests/ingredient.pyindex 415ecaeb5..f1d45da03 100644--- a/wger/nutrition/tests/ingredient.py+++ b/wger/nutrition/tests/ingredient.py@@ -13,6 +13,8 @@# You should have received a copy of the GNU Affero General Public License# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.+import json+from django.core.urlresolvers import reversefrom wger.nutrition.models import Ingredient@@ -126,3 +128,46 @@ class IngredientDetailTestCase(WorkoutManagerTestCase):'''self.ingredient_detail(editor=False)+++class IngredientSearchTestCase(WorkoutManagerTestCase):+ '''+ Tests the ingredient search functions+ '''++ def search_ingredient(self, fail=True):+ '''+ Helper function+ '''++ # Perform the search+ response = self.client.get(reverse('ingredient-search'), {'term': 'test'})+ self.assertEqual(response.status_code, 200)+ self.assertEqual(len(response.context['ingredients']), 2)+ self.assertEqual(response.context['ingredients'][0].name,+ 'Ingredient, test, 2, organic, raw')+ self.assertEqual(response.context['ingredients'][1].name, 'Test ingredient 1')++ # AJAX-Search+ kwargs = {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'}+ response = self.client.get(reverse('ingredient-search'), {'term': 'test'}, **kwargs)+ self.assertEqual(response.status_code, 200)+ result = json.loads(response.content)+ self.assertEqual(len(result), 2)+ self.assertEqual(result[0]['value'], 'Ingredient, test, 2, organic, raw')+ self.assertEqual(result[1]['value'], 'Test ingredient 1')++ def test_search_ingredient_anonymous(self):+ '''+ Test searching for an ingredient by an anonymous user+ '''++ self.search_ingredient()++ def test_search_ingredient_logged_in(self):+ '''+ Test searching for an ingredient by a logged in user+ '''++ self.user_login('test')+ self.search_ingredient()diff --git a/wger/nutrition/urls.py b/wger/nutrition/urls.pyindex 7662089a8..c61300b27 100644--- a/wger/nutrition/urls.py+++ b/wger/nutrition/urls.py@@ -70,7 +70,9 @@ urlpatterns = patterns('wger.nutrition.views',url(r'^ingredient/overview/$', 'ingredient.ingredient_overview'),url(r'^ingredient/(?P<id>\d+)/view/$', 'ingredient.ingredient_view'),url(r'^ingredient/(?P<id>\d+)/view/(?P<slug>[-\w]+)/$', 'ingredient.ingredient_view'),- url(r'^ingredient/search/$', 'ingredient.ingredient_search'),+ url(r'^ingredient/search/$',+ 'ingredient.ingredient_search',+ name='ingredient-search'),# Ingredient unitsurl(r'^ingredient/unit/list/$',