Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Do a more robust check when processing the IDs

Roland Geider6a2d55883d63
diff --git a/exercises/views.py b/exercises/views.py
index 30998e465..ab71e41c9 100644
--- a/exercises/views.py
+++ b/exercises/views.py
@@ -183,10 +183,15 @@ def exercise_edit(request, id=None):
template_data.update(csrf(request))
template_data['active_tab'] = 'exercises'
- if not id or id == 'None':
- exercise = Exercise()
- else:
+ # If the object is new, we will receice a 'None' (string) as the ID
+ # from the template, so we check for it (ValueError) and for an actual
+ # None (TypeError)
+ try:
+ int(id)
exercise = get_object_or_404(Exercise, pk=id)
+ except ValueError, TypeError:
+ exercise = Exercise()
+
template_data['exercise'] = exercise
if request.method == 'POST':
diff --git a/manager/views.py b/manager/views.py
index 5d58626cb..579e09375 100644
--- a/manager/views.py
+++ b/manager/views.py
@@ -720,14 +720,20 @@ def edit_day(request, id, day_id=None):
# Load day
# We check for string 'None' because we might get this from the template
- if not day_id or day_id == 'None':
- day = Day()
- else:
+
+ # If the object is new, we will receice a 'None' (string) as the ID
+ # from the template, so we check for it (ValueError) and for an actual
+ # None (TypeError)
+ try:
+ int(day_id)
day = get_object_or_404(Day, pk=day_id)
# Check that the day belongs to the workout
if day.training.id != workout.id:
return HttpResponseForbidden()
+ except ValueError, TypeError:
+ day = Day()
+
template_data['day'] = day
@@ -819,14 +825,19 @@ def edit_set(request, id, day_id, set_id=None):
template_data['day'] = day
# Load set
- if not set_id or set_id == 'None':
- workout_set = Set()
- else:
+
+ # If the object is new, we will receice a 'None' (string) as the ID
+ # from the template, so we check for it (ValueError) and for an actual
+ # None (TypeError)
+ try:
+ int(set_id)
workout_set = get_object_or_404(Set, pk=set_id)
# Check if all objects belong to the workout
if workout_set.exerciseday.id != day.id:
return HttpResponseForbidden()
+ except ValueError, TypeError:
+ workout_set = Set()
template_data['set'] = workout_set
diff --git a/nutrition/views.py b/nutrition/views.py
index 420fe1ff0..b2ea7d4ec 100644
--- a/nutrition/views.py
+++ b/nutrition/views.py
@@ -403,13 +403,17 @@ def edit_meal_item(request, id, meal_id, item_id=None):
# Load the meal item
- if not item_id or item_id == 'None':
- meal_item = MealItem()
- else:
+ # If the object is new, we will receice a 'None' (string) as the ID
+ # from the template, so we check for it (ValueError) and for an actual
+ # None (TypeError)
+ try:
+ int(item_id)
meal_item = get_object_or_404(MealItem, pk=item_id)
template_data['ingredient'] = meal_item.ingredient.id
template_data['ingredient_searchfield'] = meal_item.ingredient.name
+ except ValueError, TypeError:
+ meal_item = MealItem()
template_data['meal_item'] = meal_item
diff --git a/weight/views.py b/weight/views.py
index 1c1819ab3..c3ea98b00 100644
--- a/weight/views.py
+++ b/weight/views.py
@@ -51,10 +51,17 @@ def add(request, id=None):
template_data['active_tab'] = 'weight'
# Load weight
- if id and id != 'None':
+
+ # If the object is new, we will receice a 'None' (string) as the ID
+ # from the template, so we check for it (ValueError) and for an actual
+ # None (TypeError)
+ try:
+ int(id)
weight = get_object_or_404(WeightEntry, pk=id, user=request.user)
- else:
+
+ except ValueError, TypeError:
weight = WeightEntry()
+
template_data['weight'] = weight
# Process request
@@ -150,4 +157,4 @@ def get_weight_data(request):
# Return the results to the server
mimetype = 'application/json'
- return HttpResponse(json.dumps(chart_data), mimetype)
\ No newline at end of file
+ return HttpResponse(json.dumps(chart_data), mimetype)