Self hosted FLOSS fitness/workout, nutrition and weight tracker
Move functions to 'utils' package
Roland Geider5bed1ff1132d
diff --git a/exercises/views.py b/exercises/views.pyindex eca8ffd89..5b3d62d87 100644--- a/exercises/views.py+++ b/exercises/views.py@@ -29,6 +29,7 @@ from django.contrib.auth.decorators import permission_requiredfrom django.utils.translation import ugettext as _from django.utils import translation+from manager.utils import load_languagefrom exercises.models import Languagefrom exercises.models import Exercise@@ -37,50 +38,6 @@ from exercises.models import ExerciseCategorylogger = logging.getLogger('workout_manager.custom')-# ************************-# Language functions-# ************************--def load_language():- """Returns the currently used language, e.g. to load appropriate exercises- """- # TODO: perhaps store a language preference in the user's profile?-- # Read the first part of a composite language, e.g. 'de-at'- used_language = translation.get_language().split('-')[0]-- try:- language = Language.objects.get(short_name=used_language)-- # No luck, load english as our fall-back language- except ObjectDoesNotExist:- language = Language.objects.get(short_name="en")-- 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 functionsdiff --git a/manager/utils.py b/manager/utils.pynew file mode 100644index 000000000..e3258b2d7--- /dev/null+++ b/manager/utils.py@@ -0,0 +1,64 @@+# This file is part of Workout Manager.+#+# Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License++from django.utils import translation++from exercises.models import Language++# ************************+# Language functions+# ************************++def load_language():+ """Returns the currently used language, e.g. to load appropriate exercises+ """+ # TODO: perhaps store a language preference in the user's profile?++ # Read the first part of a composite language, e.g. 'de-at'+ used_language = translation.get_language().split('-')[0]++ try:+ language = Language.objects.get(short_name=used_language)++ # No luck, load english as our fall-back language+ except ObjectDoesNotExist:+ language = Language.objects.get(short_name="en")++ 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+diff --git a/nutrition/views.py b/nutrition/views.pyindex f2480ffb3..76c4fd8d3 100644--- a/nutrition/views.py+++ b/nutrition/views.py@@ -37,8 +37,8 @@ from reportlab.lib.pagesizes import A4, cmfrom reportlab.platypus import Paragraph, SimpleDocTemplate, Tablefrom reportlab.lib import colors-from exercises.views import load_language-from exercises.views import load_ingredient_languages+from manager.utils import load_language+from manager.utils import load_ingredient_languageslogger = logging.getLogger('workout_manager.custom')