Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Filter the ingredients the user will see by its language.

Roland Geideree06526f8e8c
diff --git a/exercises/views.py b/exercises/views.py
index af34d1610..56f368e56 100644
--- a/exercises/views.py
+++ b/exercises/views.py
@@ -47,7 +47,6 @@ def load_language():
# Read the first part of a composite language, e.g. 'de-at'
used_language = translation.get_language().split('-')[0]
- #logger.debug("Used language: %s" % used_language)
try:
language = Language.objects.get(short_name=used_language)
@@ -58,6 +57,29 @@ def load_language():
return language
+def load_ingredient_languages(request):
+ """ Filter the ingredients the user will see by its language.
+
+ Additionally, if the user has selected on his preference page that he wishes to also see the
+ ingredients in English (from the US Department of Agriculture), show those too. This only makes
+ sense if the user's language isn't English, as he will be presented those in that case anyway,
+ so also do a check for this.
+ """
+
+ language = load_language()
+ languages = (language.id,)
+
+ # Only registered users have a profile
+ if request.user.is_authenticated():
+ profile = request.user.get_profile()
+ show_english = profile.show_english_ingredients
+
+ # If the user's language is not english and has the preference, add english to the list
+ if show_english and language.short_name != 'en':
+ languages = (language.id, 2)
+
+ return languages
+
# ************************
# Exercise comment functions
diff --git a/manager/models.py b/manager/models.py
index ae584aa4b..75e1b7ea6 100644
--- a/manager/models.py
+++ b/manager/models.py
@@ -114,9 +114,16 @@ class UserProfile(models.Model):
# User preferences
#
- # Show exercise comments on workout view
+ # Show exercise comments on workout view
show_comments = models.BooleanField(verbose_name = _('Show exercise comments'),
help_text=_('Check to show exercise comments on the workout view'))
+
+ # Also show english ingredients while composing a nutritional plan
+ # (obviously this is only meaningful if the user has a language other than english)
+ show_english_ingredients = models.BooleanField(verbose_name = _('Show english ingredients'),
+ help_text=_('''Check to also show english ingredients. These ingredients
+are extracted from a list provided by the US Department of Agriculture. It is extremely complete,
+with around 7000 entries, but can be somewhat overwhelming and make the search difficult.'''))
def create_user_profile(sender, instance, created, **kwargs):
if created:
diff --git a/nutrition/views.py b/nutrition/views.py
index eb7a3e5be..b4b35c000 100644
--- a/nutrition/views.py
+++ b/nutrition/views.py
@@ -37,6 +37,8 @@ from reportlab.platypus import Paragraph, SimpleDocTemplate, Table
from reportlab.lib import colors
from exercises.views import load_language
+from exercises.views import load_ingredient_languages
+
logger = logging.getLogger('workout_manager.custom')
@@ -423,14 +425,16 @@ def edit_meal_item(request, id, meal_id, item_id=None):
def ingredient_overview(request):
"""Show an overview of all ingredients
"""
- language = load_language()
template_data = {}
template_data['active_tab'] = 'nutrition'
- #ingredients = Ingredient.objects.filter(language = language.id)
- ingredients = Ingredient.objects.all()
-
+ # Filter the ingredients the user will see by its language
+ # (the user can also want to see ingredients in English, see load_ingredient_languages)
+ languages = load_ingredient_languages(request)
+
+ # Load the ingredients
+ ingredients = Ingredient.objects.filter(language__in = languages)
template_data['ingredients'] = ingredients
return render_to_response('ingredient_overview.html',
@@ -507,10 +511,13 @@ 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
"""
+ # Filter the ingredients the user will see by its language
+ # (the user can also want to see ingredients in English, see load_ingredient_languages)
+ languages = load_ingredient_languages(request)
# Perform the search
q = request.GET.get('term', '')
- ingredients = Ingredient.objects.filter(name__icontains = q )
+ ingredients = Ingredient.objects.filter(name__icontains = q, language__in = languages)
# AJAX-request, this comes from the autocompleter. Create a list and send it back as JSON
if request.is_ajax():