Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #1278 #1365

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ target/

# IDE
.idea/
.vscode/

# External Libraries
wger/core/static/yarn
Expand All @@ -66,6 +67,7 @@ node_modules
# Virtual envs
venv
venv-wger
.python-version

# Dummy translation files to copy to react or flutter repo
/wger/i18n.tsx
Expand All @@ -75,3 +77,6 @@ venv-wger
/wger/app_en.arb
/coverage.lcov
/media/

# macOS
.DS_Store
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ Developers
* Bernardo Koen - https://github.com/BernardoKoen
* Gabriel Liss - https://github.com/gabeliss
* Alexandra Rhodes - https://github.com/arhodes130
* Ben Southcott - https://github.com/blsouthcott

Translators
-----------
Expand Down
40 changes: 39 additions & 1 deletion wger/nutrition/tests/test_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

# wger
from wger.core.tests import api_base_test
Expand Down Expand Up @@ -189,3 +189,41 @@ class PlanApiTestCase(api_base_test.ApiBaseResourceTestCase):
private_resource = True
special_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'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))
49 changes: 28 additions & 21 deletions wger/nutrition/views/plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
MEALITEM_WEIGHT_GRAM,
MEALITEM_WEIGHT_UNIT,
)
from wger.nutrition.models import NutritionPlan
from wger.nutrition.models import NutritionPlan, Meal, MealItem
from wger.utils.generic_views import (
WgerDeleteMixin,
WgerFormMixin,
Expand Down Expand Up @@ -203,33 +203,40 @@ 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.all()

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):
Expand Down