Self hosted FLOSS fitness/workout, nutrition and weight tracker
refactor copy meal plan function and add test for function
Ben Southcottbd88b63e384f
diff --git a/.gitignore b/.gitignoreindex ec0326d8e..fdeb03458 100644--- a/.gitignore+++ b/.gitignore@@ -58,6 +58,7 @@ target/# IDE.idea/+.vscode/# External Librarieswger/core/static/yarn@@ -66,6 +67,7 @@ node_modules# Virtual envsvenvvenv-wger+.python-version# Dummy translation files to copy to react or flutter repo/wger/i18n.tsx@@ -75,3 +77,6 @@ venv-wger/wger/app_en.arb/coverage.lcov/media/++# macOS+.DS_Storediff --git a/wger/nutrition/tests/test_plan.py b/wger/nutrition/tests/test_plan.pyindex bf6abbf9b..04002a37e 100644--- a/wger/nutrition/tests/test_plan.py+++ b/wger/nutrition/tests/test_plan.py@@ -14,7 +14,7 @@# along with Workout Manager. If not, see <http://www.gnu.org/licenses/>.# Django-from django.urls import reverse+from django.urls import reverse, resolve# wgerfrom wger.core.tests import api_base_test@@ -189,3 +189,41 @@ class PlanApiTestCase(api_base_test.ApiBaseResourceTestCase):private_resource = Truespecial_endpoints = ('nutritional_values', )data = {'description': 'The description', 'language': 1}+++class PlanCopyTestCase(WgerTestCase):++ def test_copy_plan(self):+ """+ Tests making a copy of a meal plan+ """+ self.user_login()+ orig_plan = NutritionPlan.objects.get(pk=2)+ response = self.client.get(reverse("nutrition:plan:copy", kwargs={"pk": 2}))+ copied_plan_pk = int(resolve(response.url).kwargs["id"])+ copied_plan = NutritionPlan.objects.get(pk=copied_plan_pk)++ # fields for each object to test for equality+ plan_fields = ("user", "language", "description", "has_goal_calories",)+ meal_fields = ("name", "time", "order",)+ meal_item_fields = ("ingredient", "weight_unit", "order", "amount",)++ # test each Plan object's fields are equal+ for field in plan_fields:+ self.assertEqual(getattr(orig_plan, field), getattr(copied_plan, field))++ orig_plan_meals = orig_plan.meal_set.all()+ copied_plan_meals = copied_plan.meal_set.all()++ for meal_cnt, orig_meal in enumerate(orig_plan_meals):+ # test that the fields are equal for each Meal object for each Plan+ for field in meal_fields:+ self.assertEqual(getattr(orig_meal, field), getattr(copied_plan_meals[meal_cnt], field))++ orig_plan_meal_items = orig_plan_meals[meal_cnt].mealitem_set.all()+ copied_plan_meal_items = copied_plan_meals[meal_cnt].mealitem_set.all()++ # test that the fields are equal for each MealItem object for each Meal+ for item_cnt, orig_meal_item in enumerate(orig_plan_meal_items):+ for field in meal_item_fields:+ self.assertEqual(getattr(orig_meal_item, field), getattr(copied_plan_meal_items[item_cnt], field))diff --git a/wger/nutrition/views/plan.py b/wger/nutrition/views/plan.pyindex cae8300ca..ed60fc922 100644--- a/wger/nutrition/views/plan.py+++ b/wger/nutrition/views/plan.py@@ -16,6 +16,7 @@# Standard Libraryimport logging+from copy import deepcopy# Djangofrom django.contrib.auth.decorators import login_required@@ -42,6 +43,8 @@ from django.views.generic import (UpdateView,)+from django.db import transaction+# Third Partyfrom reportlab.lib import colorsfrom reportlab.lib.pagesizes import A4@@ -58,7 +61,7 @@ from wger.nutrition.consts import (MEALITEM_WEIGHT_GRAM,MEALITEM_WEIGHT_UNIT,)-from wger.nutrition.models import NutritionPlan+from wger.nutrition.models import NutritionPlan, Meal, MealItemfrom wger.utils.generic_views import (WgerDeleteMixin,WgerFormMixin,@@ -203,33 +206,40 @@ def copy(request, pk):Copy the nutrition plan"""- plan = get_object_or_404(NutritionPlan, pk=pk, user=request.user)-- # Copy plan- meals = plan.meal_set.all()+ orig_plan = get_object_or_404(NutritionPlan, pk=pk, user=request.user)- plan_copy = plan- plan_copy.pk = None+ # make new Plan, Meal, and MealItem objects using the values for the fields from the original object+ plan_copy = NutritionPlan(+ user=orig_plan.user,+ language=orig_plan.language,+ description=orig_plan.description,+ has_goal_calories=orig_plan.has_goal_calories+ )plan_copy.save()- # Copy the meals- for meal in meals:- meal_items = meal.mealitem_set.all()-- meal_copy = meal- meal_copy.pk = None- meal_copy.plan = plan_copy+ orig_meals = orig_plan.meal_set.all()+ for orig_meal in orig_meals:+ meal_copy = Meal(+ plan=plan_copy,+ name=orig_meal.name,+ time=orig_meal.time,+ order=orig_meal.order+ )meal_copy.save()- # Copy the individual meal entries- for item in meal_items:- item_copy = item- item_copy.pk = None- item_copy.meal = meal_copy- item.save()+ orig_meal_items = orig_meal.mealitem_set.all()+ for orig_meal_item in orig_meal_items:+ meal_item_copy = MealItem(+ meal=meal_copy,+ ingredient=orig_meal_item.ingredient,+ weight_unit=orig_meal_item.weight_unit,+ order=orig_meal_item.order,+ amount=orig_meal_item.amount+ )+ meal_item_copy.save()# Redirect- return HttpResponseRedirect(reverse('nutrition:plan:view', kwargs={'id': plan.id}))+ return HttpResponseRedirect(reverse('nutrition:plan:view', kwargs={'id': plan_copy.id}))def export_pdf(request, id, uidb64=None, token=None):