Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Merge branch 'master' into fork/blsouthcott/bugfix/#1278

Roland Geider71d4a4eddeb0
diff --cc .gitignore
index fdeb03458,3f2ebf49d..26effae14
--- a/.gitignore
+++ b/.gitignore
@@@ -77,6 -75,6 +77,9 @@@ venv-wge
/wger/app_en.arb
/coverage.lcov
/media/
+ /static/
+ /extras/docker/open-food-facts/dump/*.tar.gz
+ /extras/docker/open-food-facts/dump/off/*
+
+# macOS
+.DS_Store
diff --cc AUTHORS.rst
index 7a0bb3a90,7f9d9df46..3f3d09c62
--- a/AUTHORS.rst
+++ b/AUTHORS.rst
@@@ -74,7 -74,16 +74,17 @@@ Developer
* Bernardo Koen - https://github.com/BernardoKoen
* Gabriel Liss - https://github.com/gabeliss
* Alexandra Rhodes - https://github.com/arhodes130
+* Ben Southcott - https://github.com/blsouthcott
+ * Jayanth Bontha - https://github.com/JayanthBontha
+ * Roelof Rietbroek https://github.com/strawpants
+ * Ethan Winters - https://github.com/ebwinters
+ * Dieter Plaetinck - https://github.com/Dieterbe
+ * Jonathan La Field - https://github.com/JLaField
+ * Kevin Moy - https://github.com/kmoy1
+ * Taylor Fuller - https://github.com/taylor-fuller
+ * eyJhb - https://github.com/eyJhb
+ * Joshua Shelley - https://github.com/navyjosh
+
Translators
-----------
diff --cc wger/nutrition/tests/test_plan.py
index 76cc191c5,e8f52814a..44ef866c2
--- a/wger/nutrition/tests/test_plan.py
+++ b/wger/nutrition/tests/test_plan.py
@@@ -13,16 -13,8 +13,11 @@@
# 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/>.
+# Django
+from django.urls import reverse, resolve
+
# wger
from wger.core.tests import api_base_test
- from wger.core.tests.base_testcase import (
- WgerDeleteTestCase,
- WgerEditTestCase,
- WgerTestCase,
- )
from wger.nutrition.models import NutritionPlan
@@@ -187,43 -29,6 +32,44 @@@ class PlanApiTestCase(api_base_test.Api
pk = 4
resource = NutritionPlan
private_resource = True
- special_endpoints = ('nutritional_values', )
- data = {'description': 'The description', 'language': 1}
+ overview_cached = False
+ special_endpoints = ('nutritional_values',)
+ data = {'description': 'The description'}
+
+
+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'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 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 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 --cc wger/nutrition/views/plan.py
index 14369f67b,cbdb7d30a..e69a836e5
--- a/wger/nutrition/views/plan.py
+++ b/wger/nutrition/views/plan.py
@@@ -54,20 -40,9 +40,8 @@@ from reportlab.platypus import
)
# wger
- from wger.nutrition.consts import (
- MEALITEM_WEIGHT_GRAM,
- MEALITEM_WEIGHT_UNIT,
- )
+ from wger.nutrition.consts import MEALITEM_WEIGHT_GRAM
-from wger.nutrition.models import NutritionPlan
-from wger.utils.helpers import check_token
+from wger.nutrition.models import NutritionPlan, Meal, MealItem
- from wger.utils.generic_views import (
- WgerDeleteMixin,
- WgerFormMixin,
- )
- from wger.utils.helpers import (
- check_token,
- make_token,
- )
- from wger.utils.language import load_language
from wger.utils.pdf import (
get_logo,
header_colour,
@@@ -203,43 -66,36 +65,42 @@@ def copy(request, pk)
Copy the nutrition plan
"""
- plan = get_object_or_404(NutritionPlan, pk=pk, user=request.user)
+ orig_plan = get_object_or_404(NutritionPlan, pk=pk, user=request.user)
- # Copy plan
- meals = plan.meal_set.select_related()
-
- 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.select_related()
-
- 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_copy.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):
+ def export_pdf(request, id: int):
"""
Generates a PDF with the contents of a nutrition plan