Self hosted FLOSS fitness/workout, nutrition and weight tracker
Set active tab with a context processor, not on each view
Roland Geider70a4e25e5b8f
diff --git a/exercises/views.py b/exercises/views.pyindex 40d458f2b..e5cdfa0e5 100644--- a/exercises/views.py+++ b/exercises/views.py@@ -30,7 +30,6 @@ from django.core.urlresolvers import reverse_lazyfrom django.contrib.auth.decorators import permission_requiredfrom django.utils.translation import ugettext as _from django.utils.translation import ugettext_lazy-from django.utils.datastructures import SortedDictfrom django.views.generic import DeleteViewfrom django.views.generic import CreateView@@ -47,7 +46,6 @@ from exercises.models import Musclefrom workout_manager.generic_views import YamlFormMixinfrom workout_manager.generic_views import YamlDeleteMixin-from workout_manager.constants import EXERCISE_TABlogger = logging.getLogger('workout_manager.custom')@@ -67,7 +65,6 @@ class ExerciseCommentEditView(YamlFormMixin, UpdateView):Generic view to update an existing exercise comment"""- active_tab = EXERCISE_TABmodel = ExerciseCommentform_class = ExerciseCommentFormtitle = ugettext_lazy('Edit exercise comment')@@ -89,7 +86,6 @@ class ExerciseCommentAddView(YamlFormMixin, CreateView):Generic view to add a new exercise comment"""- active_tab = EXERCISE_TABmodel = ExerciseCommentform_class = ExerciseCommentFormtitle = ugettext_lazy('Add exercise comment')@@ -135,7 +131,6 @@ def exercise_overview(request):template_data.update(csrf(request))template_data['categories'] = ExerciseCategory.objects.filter(language=language.id)- template_data['active_tab'] = EXERCISE_TABreturn render_to_response('overview.html',template_data,@@ -159,7 +154,6 @@ class MuscleListView(ListView):'''context = super(MuscleListView, self).get_context_data(**kwargs)context['language'] = load_language()- context['active_tab'] = EXERCISE_TABreturn context@@ -175,7 +169,6 @@ def exercise_view(request, id, slug=None):# Load the exercise itselfexercise = get_object_or_404(Exercise, pk=id)template_data['exercise'] = exercise- template_data['active_tab'] = EXERCISE_TAB# Create the backgrounds that show what muscles the exercise works onbackgrounds_back = []@@ -225,7 +218,6 @@ class ExercisesEditAddView(YamlFormMixin):Generic view to subclass from for exercise adding and editing, since theyshare all this settings"""- active_tab = EXERCISE_TABmodel = Exerciseform_fields = ['name',@@ -282,7 +274,6 @@ class ExerciseDeleteView(YamlDeleteMixin, DeleteView):"""model = Exercise- active_tab = EXERCISE_TABsuccess_url = reverse_lazy('exercises.views.exercise_overview')delete_message = ugettext_lazy('This will delete the exercise from all workouts.')@@ -350,7 +341,6 @@ class ExerciseCategoryAddView(YamlFormMixin, CreateView):Generic view to add a new exercise category"""- active_tab = EXERCISE_TABmodel = ExerciseCategoryform_class = ExerciseCategoryFormsuccess_url = reverse_lazy('exercises.views.exercise_overview')@@ -368,7 +358,6 @@ class ExerciseCategoryUpdateView(YamlFormMixin, UpdateView):Generic view to update an existing exercise category"""- active_tab = EXERCISE_TABmodel = ExerciseCategoryform_class = ExerciseCategoryFormsuccess_url = reverse_lazy('exercises.views.exercise_overview')diff --git a/manager/context_processor.py b/manager/context_processor.pyindex aef21605a..f0b56c4e0 100644--- a/manager/context_processor.py+++ b/manager/context_processor.py@@ -1,15 +1,15 @@# 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# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.@@ -18,17 +18,42 @@ from workout_manager import get_versionfrom manager.utils import load_language+from workout_manager import constants++def processor(request):- return {++ context = {# Application version- 'version' : get_version(),-+ 'version': get_version(),+# Do not track header'DNT': request.META.get('HTTP_DNT', False),-+# User language'language': load_language(),-+# The current request'request': request}++ # Pseudo-intelligent navigation here+ if '/software/' in request.get_full_path():+ context['active_tab'] = constants.SOFTWARE_TAB++ elif '/exercise/' in request.get_full_path():+ context['active_tab'] = constants.EXERCISE_TAB++ elif '/nutrition/' in request.get_full_path():+ context['active_tab'] = constants.NUTRITION_TAB++ elif '/weight/' in request.get_full_path():+ context['active_tab'] = constants.WEIGHT_TAB++ elif '/workout/' in request.get_full_path():+ context['active_tab'] = constants.WORKOUT_TAB++ else:+ context['active_tab'] = constants.USER_TAB++ return contextdiff --git a/manager/views.py b/manager/views.pyindex eb59e9c4e..800ef336c 100644--- a/manager/views.py+++ b/manager/views.py@@ -76,8 +76,6 @@ from manager.forms import DemoUserFormfrom manager.utils import load_language-from workout_manager.constants import WORKOUT_TAB-from workout_manager.constants import USER_TABfrom workout_manager.generic_views import YamlFormMixinfrom workout_manager.generic_views import YamlDeleteMixin@@ -96,7 +94,6 @@ def index(request):"""template_data = {}- template_data['active_tab'] = USER_TAB# Load the last workout, if one existstry:@@ -156,8 +153,7 @@ def login(request):"""return django_loginview(request,- template_name='user/login.html',- extra_context={'active_tab': USER_TAB})+ template_name='user/login.html')def logout(request):@@ -172,7 +168,6 @@ def registration(request):"""template_data = {}template_data.update(csrf(request))- template_data['active_tab'] = USER_TABif request.method == 'POST':form = RegistrationForm(data=request.POST)@@ -210,7 +205,6 @@ def create_demo_user(request):template_data = {}template_data.update(csrf(request))- template_data['active_tab'] = USER_TABif request.method == 'POST':form = DemoUserForm(data=request.POST)@@ -297,7 +291,6 @@ def preferences(request):"""template_data = {}template_data.update(csrf(request))- template_data['active_tab'] = USER_TAB# Process the preferences formif request.method == 'POST':@@ -371,7 +364,6 @@ def overview(request):"""template_data = {}- template_data['active_tab'] = WORKOUT_TABlatest_trainings = TrainingSchedule.objects.filter(user=request.user)template_data['workouts'] = latest_trainings@@ -386,7 +378,6 @@ def view_workout(request, id):"""Show the workout with the given ID"""template_data = {}- template_data['active_tab'] = WORKOUT_TABworkout = get_object_or_404(TrainingSchedule, pk=id, user=request.user)template_data['workout'] = workout@@ -510,7 +501,6 @@ class WorkoutDeleteView(YamlDeleteMixin, DeleteView):Generic view to delete a workout routine"""- active_tab = WORKOUT_TABmodel = TrainingSchedulesuccess_url = reverse_lazy('manager.views.overview')title = ugettext_lazy('Delete workout')@@ -522,7 +512,6 @@ class WorkoutEditView(YamlFormMixin, UpdateView):Generic view to update an existing workout routine"""- active_tab = WORKOUT_TABmodel = TrainingScheduleform_class = WorkoutFormtitle = ugettext_lazy('Edit workout')@@ -537,7 +526,6 @@ class DayView(YamlFormMixin):Base generic view for exercise day"""- active_tab = WORKOUT_TABmodel = Dayform_class = DayForm@@ -649,7 +637,6 @@ def edit_set(request, id, day_id, set_id=None):template_data = {}template_data.update(csrf(request))- template_data['active_tab'] = WORKOUT_TAB# Load workoutworkout = get_object_or_404(TrainingSchedule, pk=id, user=request.user)@@ -858,7 +845,6 @@ def api_edit_set(request):def edit_setting(request, id, set_id, exercise_id, setting_id=None):template_data = {}template_data.update(csrf(request))- template_data['active_tab'] = WORKOUT_TAB# Load workoutworkout = get_object_or_404(TrainingSchedule, pk=id, user=request.user)@@ -967,7 +953,6 @@ class WorkoutLogUpdateView(YamlFormMixin, UpdateView):"""Generic view to edit an existing workout log weight entry"""- active_tab = WORKOUT_TABmodel = WorkoutLogform_class = WorkoutLogFormcustom_js = '''$(document).ready(function () {@@ -992,7 +977,6 @@ def workout_log_add(request, pk):template_data = {}template_data.update(csrf(request))- template_data['active_tab'] = WORKOUT_TAB# Load the day and check ownershipday = get_object_or_404(Day, pk=pk)@@ -1104,9 +1088,6 @@ class WorkoutLogDetailView(DetailView):# Call the base implementation first to get a contextcontext = super(WorkoutLogDetailView, self).get_context_data(**kwargs)- # Active tab- context['active_tab'] = WORKOUT_TAB-# Prepare the entries for rendering and the D3 chartworkout_log = {}entry = WorkoutLog()diff --git a/nutrition/views.py b/nutrition/views.pyindex 480a47d40..cc9cf2fee 100644--- a/nutrition/views.py+++ b/nutrition/views.py@@ -50,7 +50,6 @@ from manager.utils import load_languagefrom manager.utils import load_ingredient_languagesfrom workout_manager import get_version-from workout_manager.constants import NUTRITION_TABfrom workout_manager.generic_views import YamlFormMixinfrom workout_manager.generic_views import YamlDeleteMixinfrom workout_manager import helpers@@ -73,7 +72,6 @@ class PlanForm(ModelForm):def overview(request):template_data = {}template_data.update(csrf(request))- template_data['active_tab'] = NUTRITION_TABplans = NutritionPlan.objects.filter(user=request.user)template_data['plans'] = plans@@ -101,7 +99,6 @@ class PlanDeleteView(YamlDeleteMixin, DeleteView):Generic view to delete a nutritional plan"""- active_tab = NUTRITION_TABmodel = NutritionPlansuccess_url = reverse_lazy('nutrition.views.overview')title = ugettext_lazy('Delete nutritional plan?')@@ -113,7 +110,6 @@ class PlanEditView(YamlFormMixin, UpdateView):Generic view to update an existing nutritional plan"""- active_tab = NUTRITION_TABmodel = NutritionPlanform_class = PlanFormtitle = ugettext_lazy('Add a new nutritional plan')@@ -125,7 +121,6 @@ def view(request, id):"""Show the nutrition plan with the given ID"""template_data = {}- template_data['active_tab'] = NUTRITION_TABplan = get_object_or_404(NutritionPlan, pk=id, user=request.user)template_data['plan'] = plan@@ -317,7 +312,6 @@ class MealCreateView(YamlFormMixin, CreateView):Generic view to add a new meal to a nutrition plan"""- active_tab = NUTRITION_TABmodel = Mealform_class = MealFormtitle = ugettext_lazy('Add new meal')@@ -346,7 +340,6 @@ class MealEditView(YamlFormMixin, UpdateView):Generic view to update an existing meal"""- active_tab = NUTRITION_TABmodel = Mealform_class = MealFormtitle = ugettext_lazy('Edit meal')@@ -398,7 +391,6 @@ def edit_meal_item(request, id, meal_id, item_id=None):"""Form to add a meal to a plan"""template_data = {}- template_data['active_tab'] = NUTRITION_TAB# Load the planplan = get_object_or_404(NutritionPlan, pk=id, user=request.user)@@ -469,7 +461,6 @@ def ingredient_overview(request):"""template_data = {}- template_data['active_tab'] = NUTRITION_TAB# Filter the ingredients the user will see by its language# (the user can also want to see ingredients in English, see load_ingredient_languages)@@ -489,7 +480,6 @@ def ingredient_overview(request):def ingredient_view(request, id):template_data = {}- template_data['active_tab'] = NUTRITION_TABingredient = get_object_or_404(Ingredient, pk=id)template_data['ingredient'] = ingredient@@ -529,7 +519,6 @@ class IngredientEditView(YamlFormMixin, UpdateView):Generic view to update an existing ingredient"""- active_tab = NUTRITION_TABmodel = Ingredientform_class = IngredientFormtitle = ugettext_lazy('Add a new ingredient')@@ -541,7 +530,6 @@ class IngredientCreateView(YamlFormMixin, CreateView):Generic view to add a new ingredient"""- active_tab = NUTRITION_TABmodel = Ingredientform_class = IngredientFormtitle = ugettext_lazy('Add a new ingredient')diff --git a/weight/views.py b/weight/views.pyindex 28e82be0e..543f39cec 100644--- a/weight/views.py+++ b/weight/views.py@@ -45,7 +45,6 @@ from weight.models import WeightEntryfrom weight import helpersfrom workout_manager.generic_views import YamlFormMixin-from workout_manager.constants import WEIGHT_TABfrom workout_manager.constants import DATE_FORMATS@@ -64,7 +63,6 @@ class WeightAddView(YamlFormMixin, CreateView):"""Generic view to add a new weight entry"""- active_tab = WEIGHT_TABmodel = WeightEntryform_class = WeightFormcustom_js = '''$(document).ready(function () {@@ -87,7 +85,6 @@ class WeightUpdateView(YamlFormMixin, UpdateView):"""Generic view to edit an existing weight entry"""- active_tab = WEIGHT_TABmodel = WeightEntryform_class = WeightFormcustom_js = '''$(document).ready(function () {@@ -135,7 +132,6 @@ def overview(request):* http://d3js.org/"""template_data = {}- template_data['active_tab'] = WEIGHT_TABmin_date = WeightEntry.objects.filter(user=request.user).aggregate(Min('creation_date'))['creation_date__min']max_date = WeightEntry.objects.filter(user=request.user).aggregate(Max('creation_date'))['creation_date__max']@@ -210,7 +206,6 @@ class WeightCsvImportFormPreview(FormPreview):return {'form': form,'stage_field': self.unused_name('stage'),'state': self.state,- 'active_tab': WEIGHT_TAB,'form_action': reverse('weight-import-csv')}def process_preview(self, request, form, context):diff --git a/workout_manager/constants.py b/workout_manager/constants.pyindex 7b336f8c3..c571f8279 100644--- a/workout_manager/constants.py+++ b/workout_manager/constants.py@@ -1,17 +1,17 @@# -*- coding: utf-8 -*-# 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@@ -20,15 +20,16 @@ WORKOUT_TAB = 'workout'EXERCISE_TAB = 'exercises'WEIGHT_TAB = 'weight'NUTRITION_TAB = 'nutrition'+SOFTWARE_TAB = 'software'USER_TAB = 'user'# Valid date formatsDATE_FORMATS = [- '%d.%m.%Y', # '25.10.2012'- '%d.%m.%y', # '25.10.12'-- '%m/%d/%Y', # '10/25/2012'- '%m/%d/%y', # '10/25/12'-- '%Y-%m-%d', # '2012-10-25'- ]\ No newline at end of file+ '%d.%m.%Y', # '25.10.2012'+ '%d.%m.%y', # '25.10.12'++ '%m/%d/%Y', # '10/25/2012'+ '%m/%d/%y', # '10/25/12'++ '%Y-%m-%d', # '2012-10-25'+ ]diff --git a/workout_manager/urls.py b/workout_manager/urls.pyindex 575ccf2f6..b024ac7cc 100644--- a/workout_manager/urls.py+++ b/workout_manager/urls.py@@ -1,14 +1,15 @@-from django.conf.urls import patterns, include, url+from django.conf.urls import include, urlfrom django.conf.urls.i18n import i18n_patternsfrom django.contrib import adminadmin.autodiscover()urlpatterns = i18n_patterns('',- url(r'^admin/', include(admin.site.urls)),+ url(r'^admin/', include(admin.site.urls)),url(r'^', include('manager.urls')),url(r'exercise/', include('exercises.urls')),url(r'weight/', include('weight.urls')),url(r'nutrition/', include('nutrition.urls')),+ url(r'software/', include('software.urls', namespace='software', app_name='software')),url(r'^browserid/', include('django_browserid.urls')),)