Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Some work on how generic views are used

Roland Geider97bfe1170cab
diff --git a/manager/urls.py b/manager/urls.py
index 3858642f3..d81d08962 100644
--- a/manager/urls.py
+++ b/manager/urls.py
@@ -38,7 +38,7 @@ urlpatterns = patterns('manager.views',
login_required(DayEditView.as_view()),
name = 'day-edit'),
- url(r'^workout/(?P<pk>\d+)/day/add/$',
+ url(r'^workout/(?P<workout_pk>\d+)/day/add/$',
login_required(DayCreateView.as_view()),
name = 'day-add'),
diff --git a/manager/views.py b/manager/views.py
index 897829ea0..83b03ed57 100644
--- a/manager/views.py
+++ b/manager/views.py
@@ -418,7 +418,7 @@ class DayForm(ModelForm):
exclude=('training',)
-class DayView(YamlFormMixin, UpdateView):
+class DayView(YamlFormMixin):
"""
Base generic view for exercise day
"""
@@ -442,7 +442,7 @@ class DayView(YamlFormMixin, UpdateView):
if self.object:
workout = self.object.training
else:
- workout = TrainingSchedule.objects.get(pk = self.kwargs['pk'])
+ workout = TrainingSchedule.objects.get(pk = self.kwargs['workout_pk'])
used_days = []
for day in workout.day_set.all():
@@ -474,20 +474,22 @@ class DayCreateView(DayView, CreateView):
"""
title = ugettext_lazy('Add workout day')
+ owner_object = {'pk': 'workout_pk', 'class': TrainingSchedule}
+
+
def form_valid(self, form):
"""
Set the workout this day belongs to
"""
- form.instance.workout = TrainingSchedule.objects.get(pk = self.kwargs['pk'])
+ form.instance.workout = TrainingSchedule.objects.get(pk = self.kwargs['workout_pk'])
return super(DayCreateView, self).form_valid(form)
# Send some additional data to the template
def get_context_data(self, **kwargs):
context = super(DayCreateView, self).get_context_data(**kwargs)
context['form_action'] = reverse('day-add',
- kwargs={'pk': self.kwargs['pk']})
-
+ kwargs={'workout_pk': self.kwargs['workout_pk']})
return context
diff --git a/nutrition/views.py b/nutrition/views.py
index 06003779e..0f3cbbf45 100644
--- a/nutrition/views.py
+++ b/nutrition/views.py
@@ -100,20 +100,7 @@ class PlanDeleteView(YamlDeleteMixin, DeleteView):
model = NutritionPlan
success_url = reverse_lazy('nutrition.views.overview')
title = ugettext_lazy('Delete nutritional plan?')
-
- # Send some additional data to the template
- def get_context_data(self, **kwargs):
- context = super(PlanDeleteView, self).get_context_data(**kwargs)
- context['form_action'] = reverse('nutrition-delete',
- kwargs={'pk': self.object.id})
-
- return context
-
- # Check that only the owner can access this
- def get_object(self, queryset = None):
- return get_object_or_404(self.model,
- pk = self.kwargs['pk'],
- user = self.request.user)
+ form_action_urlname = 'nutrition-delete'
class PlanEditView(YamlFormMixin, UpdateView):
"""
@@ -124,20 +111,7 @@ class PlanEditView(YamlFormMixin, UpdateView):
model = NutritionPlan
form_class = PlanForm
title = ugettext_lazy('Add a new nutritional plan')
-
- # Send some additional data to the template
- def get_context_data(self, **kwargs):
- context = super(PlanEditView, self).get_context_data(**kwargs)
- context['form_action'] = reverse('nutrition-edit',
- kwargs={'pk': self.object.id})
-
- return context
-
- # Check that only the owner can access this
- def get_object(self, queryset = None):
- return get_object_or_404(self.model,
- pk = self.kwargs['pk'],
- user = self.request.user)
+ form_action_urlname = 'nutrition-edit'
@login_required
@@ -312,6 +286,7 @@ class MealCreateView(YamlFormMixin, CreateView):
model = Meal
form_class = MealForm
title = ugettext_lazy('Add new meal')
+ owner_object = {'pk': 'plan_pk', 'class': NutritionPlan}
def form_valid(self, form):
plan = get_object_or_404(NutritionPlan, pk = self.kwargs['plan_pk'], user=self.request.user)
@@ -340,29 +315,12 @@ class MealEditView(YamlFormMixin, UpdateView):
model = Meal
form_class = MealForm
title = ugettext_lazy('Edit meal')
-
+ form_action_urlname = 'meal-edit'
+
def get_success_url(self):
return reverse('nutrition.views.view', kwargs ={'id': self.object.plan.id})
- # Send some additional data to the template
- def get_context_data(self, **kwargs):
- context = super(MealEditView, self).get_context_data(**kwargs)
- context['form_action'] = reverse('meal-edit',
- kwargs={'pk': self.object.id})
-
- return context
-
- # Check that only the owner can access this
- def get_object(self, queryset = None):
- meal = get_object_or_404(self.model,
- pk = self.kwargs['pk'])
-
- # TODO: check this
- if meal.plan.user != self.request.user:
- return HttpResponseForbidden()
- else:
- return meal
-
+
@login_required
def delete_meal(request, id):
"""Deletes the meal with the given ID
@@ -540,14 +498,7 @@ class IngredientEditView(YamlFormMixin, UpdateView):
model = Ingredient
form_class = IngredientForm
title = ugettext_lazy('Add a new ingredient')
-
- # Send some additional data to the template
- def get_context_data(self, **kwargs):
- context = super(IngredientEditView, self).get_context_data(**kwargs)
- context['form_action'] = reverse('ingredient-edit',
- kwargs={'pk': self.object.id})
-
- return context
+ form_action_urlname = 'ingredient-edit'
class IngredientCreateView(YamlFormMixin, CreateView):
diff --git a/workout_manager/generic_views.py b/workout_manager/generic_views.py
index 4de861b21..197fb9f01 100644
--- a/workout_manager/generic_views.py
+++ b/workout_manager/generic_views.py
@@ -32,6 +32,7 @@ class YamlFormMixin(ModelFormMixin):
form_action = ''
form_action_urlname = ''
title = ''
+ owner_object = False
def get_context_data(self, **kwargs):
'''
@@ -77,17 +78,7 @@ class YamlFormMixin(ModelFormMixin):
# Set the title
context['title'] = self.title
- return context
-
- def get_owner_object(self):
- """
- Returns the object that has a user associated with it.
-
- This is used in the dispatch method to check that only the owner of an
- object can edit it.
- """
- return False
-
+ return context
def dispatch(self, request, *args, **kwargs):
"""
@@ -98,10 +89,15 @@ class YamlFormMixin(ModelFormMixin):
is not the owner.
"""
- # These seem to be necessary if for calling get_object
+ # These seem to be necessary for calling get_object
self.kwargs = kwargs
self.request = request
- owner_object = self.get_object().get_owner_object()
+
+ # For new objects, we have to manually load the owner object
+ if self.owner_object:
+ owner_object = self.owner_object['class'].objects.get(pk = kwargs[self.owner_object['pk']])
+ else:
+ owner_object = self.get_object().get_owner_object()
# Nothing to see, please move along
if owner_object and owner_object.user != self.request.user: