Self hosted FLOSS fitness/workout, nutrition and weight tracker
Allow anonymous access to the nutrition plan page
Roland Geiderd48e362d6d23
diff --git a/wger/nutrition/templates/mobile/plan/view.html b/wger/nutrition/templates/mobile/plan/view.htmlindex 884544acd..920d4e852 100644--- a/wger/nutrition/templates/mobile/plan/view.html+++ b/wger/nutrition/templates/mobile/plan/view.html@@ -30,6 +30,7 @@ $(document).ready(function() {{% endif %}+{% if is_owner %}{% for meal in plan.meal_set.select_related %}<div class="modal fade" id="editoptions-meal-{{ meal.id }}"><div class="modal-dialog">@@ -112,15 +113,15 @@ $(document).ready(function() {</tr><tr><td>{% trans "Protein" %}</td>- <td class="align-right">{{item.get_nutritional_values.protein|floatformat}} {% trans_weight_unit 'g' user %}</td>+ <td class="align-right">{{item.get_nutritional_values.protein|floatformat}} {% trans_weight_unit 'g' owner_user %}</td></tr><tr><td>{% trans "Carbohydrates" %}</td>- <td class="align-right">{{item.get_nutritional_values.carbohydrates|floatformat}} {% trans_weight_unit 'g' user %}</td>+ <td class="align-right">{{item.get_nutritional_values.carbohydrates|floatformat}} {% trans_weight_unit 'g' owner_user %}</td></tr><tr><td>{% trans "Fat" %}</td>- <td class="align-right">{{item.get_nutritional_values.fat|floatformat}} {% trans_weight_unit 'g' user %}</td>+ <td class="align-right">{{item.get_nutritional_values.fat|floatformat}} {% trans_weight_unit 'g' owner_user %}</td></tr></tbody></table>@@ -151,20 +152,25 @@ $(document).ready(function() {{% endfor %}{% endfor %}+{% endif %}{% for meal in plan.meal_set.select_related %}<div class="list-group"><a href="#editoptions-meal-{{ meal.id }}" class="list-group-item active" data-toggle="modal">- <span class="glyphicon glyphicon-cog pull-right"></span>+ {% if is_owner %}+ <span class="glyphicon glyphicon-cog pull-right"></span>+ {% endif %}{% trans "Nr."%} {{ forloop.counter }}{% if meal.time %} – {{meal.time|time:"H:i"}}{% endif %}</a>{% for item in meal.mealitem_set.select_related %}<a href="#editoptions-item-{{ item.id }}" data-toggle="modal" class="list-group-item wger-list-group-item">- <span class="glyphicon glyphicon-cog pull-right"></span>+ {% if is_owner %}+ <span class="glyphicon glyphicon-cog pull-right"></span>+ {% endif %}<h4>{{ item.ingredient.name }}</h4><p>{% if item.get_unit_type == MEALITEM_WEIGHT_GRAM %}{{ item.amount|floatformat:"0" }}g@@ -173,7 +179,7 @@ $(document).ready(function() {{% endif %}</p></a>- {% if forloop.last %}+ {% if forloop.last and is_owner %}<a href="{% url 'nutrition:meal_item:add' meal.id %}"class="list-group-item list-group-item-success"><span class="glyphicon glyphicon-plus"></span>@@ -181,33 +187,39 @@ $(document).ready(function() {</a>{% endif %}{% empty %}+ {% if is_owner %}<a href="{% url 'nutrition:meal_item:add' meal.id %}"title="{% trans 'Add one now.' %}"class="list-group-item list-group-item-success">{% trans "No items for this meal." %}<br>{% trans "Add one now." %}</a>+ {% endif %}{% endfor %}</div>{% empty %}-<a href="{% url 'nutrition:meal:add' plan.id %}"- {% auto_link_css flavour "wger-modal-dialog" %}>- {% trans "No meals for this plan." %}<br>- {% trans "Add one now." %}-</a>+ {% if is_owner %}+ <a href="{% url 'nutrition:meal:add' plan.id %}"+ {% auto_link_css flavour "wger-modal-dialog" %}>+ {% trans "No meals for this plan." %}<br>+ {% trans "Add one now." %}+ </a>+ {% endif %}{% endfor %}+{% if is_owner %}<p style="margin-top:2em;"><a href="{% url 'nutrition:meal:add' plan.id %}"{% auto_link_css flavour "wger-modal-dialog" %}>{% trans "Add a new meal" %}</a></p>+{% endif %}-{% if language.short_name != 'en' %}+{% if is_owner and language.short_name != 'en' %}<p>{% trans "If you find the ingredient list too short, you might want to activate the preference to also show English ingredients." %}<a href="{% url 'core:user:preferences' %}" id="ajax-english-ingredients">{% trans "That's done here" %}</a>:- {% if user.userprofile.show_english_ingredients %}+ {% if owner_user.userprofile.show_english_ingredients %}<img src="{% static 'images/icons/status-on.svg' %}"width="20"height="20"@@ -239,12 +251,12 @@ $(document).ready(function() {</tr></thead><tbody>- {% if user.userprofile.calories and plan.has_goal_calories %}+ {% if is_owner and owner_user.userprofile.calories and plan.has_goal_calories %}{% with total=plan.get_calories_approximation %}<tr style="background-color:{% if total == 1 %}#8ae234;{% elif total == 2 %}#fce94f;{% elif total == 3 %}#fcaf3e;{% elif total > 3 %}#ef2929;{% endif %}"><td>{% trans "Goal" %}</td><td class="align-right">- <strong>{{user.userprofile.calories|floatformat:"0"}} {% trans "kcal" %}</strong>+ <strong>{{owner_user.userprofile.calories|floatformat:"0"}} {% trans "kcal" %}</strong></td></tr>{% endwith %}@@ -253,7 +265,7 @@ $(document).ready(function() {</table>-{% if plan.has_goal_calories %}+{% if is_owner and plan.has_goal_calories %}<p>{% blocktrans %}You have selected that this nutritional plan has agoal amount of calories. Use the calculator or enter the value yourself.{% endblocktrans %}<a href="{% url 'nutrition:calories:view' %}">{% trans "That's done here" %}</a>@@ -268,7 +280,7 @@ $(document).ready(function() {<th class="align-right">{% trans "Total" %}</th><th class="align-right">{% trans "Percent of energy" %}</th><th class="align-right">- {% if user.userprofile.use_metric %}+ {% if owner_user.userprofile.use_metric %}{% trans "g per body kg" %}{% else %}{% trans "oz per body lb" %}@@ -285,31 +297,31 @@ $(document).ready(function() {</tr><tr><td>{% trans "Protein" %}</td>- <td class="align-right">{{nutritional_data.total.protein|floatformat}} {% trans_weight_unit 'g' user %}</td>+ <td class="align-right">{{nutritional_data.total.protein|floatformat}} {% trans_weight_unit 'g' owner_user %}</td><td class="align-right">{{nutritional_data.percent.protein|floatformat}} %</td><td class="align-right">{{nutritional_data.per_kg.protein|floatformat:2}}</td></tr><tr><td>{% trans "Carbohydrates" %}</td>- <td class="align-right">{{nutritional_data.total.carbohydrates|floatformat}} {% trans_weight_unit 'g' user %}</td>+ <td class="align-right">{{nutritional_data.total.carbohydrates|floatformat}} {% trans_weight_unit 'g' owner_user %}</td><td class="align-right">{{nutritional_data.percent.carbohydrates|floatformat}} %</td><td class="align-right">{{nutritional_data.per_kg.carbohydrates|floatformat:2}}</td></tr><tr><td style="padding-left: 2em;">{% trans "Sugar content in carbohydrates" %}</td>- <td class="align-right">{{nutritional_data.total.carbohydrates_sugar|floatformat}} {% trans_weight_unit 'g' user %}</td>+ <td class="align-right">{{nutritional_data.total.carbohydrates_sugar|floatformat}} {% trans_weight_unit 'g' owner_user %}</td><td></td><td></td></tr><tr><td>{% trans "Fat" %}</td>- <td class="align-right">{{nutritional_data.total.fat|floatformat}} {% trans_weight_unit 'g' user %}</td>+ <td class="align-right">{{nutritional_data.total.fat|floatformat}} {% trans_weight_unit 'g' owner_user %}</td><td class="align-right">{{nutritional_data.percent.fat|floatformat}} %</td><td class="align-right">{{nutritional_data.per_kg.fat|floatformat:2}}</td></tr><tr><td style="padding-left: 2em;">{% trans "Saturated fat content in fats" %}</td>- <td class="align-right">{{nutritional_data.total.fat_saturated|floatformat}} {% trans_weight_unit 'g' user %}</td>+ <td class="align-right">{{nutritional_data.total.fat_saturated|floatformat}} {% trans_weight_unit 'g' owner_user %}</td><td></td><td></td></tr>@@ -320,13 +332,13 @@ $(document).ready(function() {</tr><tr><td>{% trans "Fibres" %}</td>- <td class="align-right">{{nutritional_data.total.fibres|floatformat}} {% trans_weight_unit 'g' user %}</td>+ <td class="align-right">{{nutritional_data.total.fibres|floatformat}} {% trans_weight_unit 'g' owner_user %}</td><td></td><td></td></tr><tr><td>{% trans "Sodium" %}</td>- <td class="align-right">{{nutritional_data.total.sodium|floatformat}} {% trans_weight_unit 'g' user %}</td>+ <td class="align-right">{{nutritional_data.total.sodium|floatformat}} {% trans_weight_unit 'g' owner_user %}</td><td></td><td></td></tr>@@ -346,6 +358,7 @@ $(document).ready(function() {{% trans "Download as PDF" %}</a>+{% if is_owner %}<a href="{% url 'nutrition:plan:edit' plan.id %}"{% auto_link_css flavour "wger-modal-dialog" %}><img src="{% static 'images/icons/edit.svg' %}"@@ -372,5 +385,5 @@ $(document).ready(function() {alt="{% trans 'Delete nutrition plan' %}">{% trans "Delete nutrition plan" %}</a>-+{% endif %}{% endblock %}diff --git a/wger/nutrition/templates/plan/view.html b/wger/nutrition/templates/plan/view.htmlindex 22dbdce14..dbe2d480e 100644--- a/wger/nutrition/templates/plan/view.html+++ b/wger/nutrition/templates/plan/view.html@@ -40,9 +40,9 @@ function custom_modal_init()<tr><td colspan="2"></td><td class="align-right">{% trans 'kcal' %}</td>- <td class="align-right">{% trans_weight_unit 'g' user %}</td>- <td class="align-right">{% trans_weight_unit 'g' user %}</td>- <td class="align-right">{% trans_weight_unit 'g' user %}</td>+ <td class="align-right">{% trans_weight_unit 'g' owner_user %}</td>+ <td class="align-right">{% trans_weight_unit 'g' owner_user %}</td>+ <td class="align-right">{% trans_weight_unit 'g' owner_user %}</td></tr>{% for meal in plan.meal_set.select_related %}{% for item in meal.mealitem_set.select_related %}@@ -54,6 +54,7 @@ function custom_modal_init(){% if meal.time %} – {{meal.time|time:"H:i"}}{% endif %}</strong>+ {% if is_owner %}<div class="editoptions"><a href="{% url 'nutrition:meal:delete' meal.id %}"title="{% trans 'Delete' %}">@@ -79,6 +80,7 @@ function custom_modal_init()alt="{% trans 'Add item to meal' %}"title="{% trans 'Add item to meal' %}"></a></div>+ {% endif %}</td>{% endifchanged %}<td>@@ -88,6 +90,7 @@ function custom_modal_init(){% endif %}{{ item.ingredient.name }}+ {% if is_owner %}<span class="editoptions"><a href="{% url 'nutrition:meal_item:delete' item.id %}"title="{% trans 'Delete' %}"@@ -104,6 +107,7 @@ function custom_modal_init()height="16"alt="{% trans 'Edit' %}"></a></span>+ {% endif %}</td><td class="align-right">{{item.get_nutritional_values.energy|floatformat}}</td><td class="align-right">{{item.get_nutritional_values.protein|floatformat}}</td>@@ -111,19 +115,22 @@ function custom_modal_init()<td class="align-right">{{item.get_nutritional_values.fat|floatformat}}</td></tr>{% empty %}+ {% if is_owner %}<tr>- <td colspan="7">- <a href="{% url 'nutrition:meal_item:add' meal.id %}"- title="{% trans 'Add one now.' %}"- class="wger-modal-dialog edit-meal btn btn-default btn-block">- {% trans "No items for this meal." %}- {% trans "Add one now."%}- </a>- </td>-</tr>+ <td colspan="7">+ <a href="{% url 'nutrition:meal_item:add' meal.id %}"+ title="{% trans 'Add one now.' %}"+ class="wger-modal-dialog edit-meal btn btn-default btn-block">+ {% trans "No items for this meal." %}+ {% trans "Add one now."%}+ </a>+ </td>+ </tr>+ {% endif %}{% endfor %}{% empty %}+{% if is_owner %}<p><a href="{% url 'nutrition:meal:add' plan.id %}"class="wger-modal-dialog btn btn-default btn-block">@@ -131,7 +138,9 @@ function custom_modal_init(){% trans "Add one now." %}</a></p>+{% endif %}{% endfor %}+ {% if is_owner %}<tr><td colspan="6"><a href="{% url 'nutrition:meal:add' plan.id %}"@@ -145,10 +154,11 @@ function custom_modal_init()</a></td><tr>+ {% endif %}</tbody></table>-{% if language.short_name != 'en' %}+{% if language.short_name != 'en' and is_owner %}<p>{% trans "If you find the ingredient list too short, you might want to activate the preference to also show English ingredients." %}<a href="{% url 'core:user:preferences' %}" id="ajax-english-ingredients">{% trans "That's done here" %}</a>.@@ -164,7 +174,7 @@ function custom_modal_init()<th class="align-right">{% trans "Total" %}</th><th class="align-right">{% trans "Percent of energy" %}</th><th class="align-right">- {% if user.userprofile.use_metric %}+ {% if owner_user.userprofile.use_metric %}{% trans "g per body kg" %}{% else %}{% trans "oz per body lb" %}@@ -181,31 +191,31 @@ function custom_modal_init()</tr><tr><td>{% trans "Protein" %}</td>- <td class="align-right">{{nutritional_data.total.protein|floatformat}} {% trans_weight_unit 'g' user %}</td>+ <td class="align-right">{{nutritional_data.total.protein|floatformat}} {% trans_weight_unit 'g' owner_user %}</td><td class="align-right">{{nutritional_data.percent.protein|floatformat}} %</td><td class="align-right">{{nutritional_data.per_kg.protein|floatformat:2}}</td></tr><tr><td>{% trans "Carbohydrates" %}</td>- <td class="align-right">{{nutritional_data.total.carbohydrates|floatformat}} {% trans_weight_unit 'g' user %}</td>+ <td class="align-right">{{nutritional_data.total.carbohydrates|floatformat}} {% trans_weight_unit 'g' owner_user %}</td><td class="align-right">{{nutritional_data.percent.carbohydrates|floatformat}} %</td><td class="align-right">{{nutritional_data.per_kg.carbohydrates|floatformat:2}}</td></tr><tr><td style="padding-left: 2em;">{% trans "Sugar content in carbohydrates" %}</td>- <td class="align-right">{{nutritional_data.total.carbohydrates_sugar|floatformat}} {% trans_weight_unit 'g' user %}</td>+ <td class="align-right">{{nutritional_data.total.carbohydrates_sugar|floatformat}} {% trans_weight_unit 'g' owner_user %}</td><td></td><td></td></tr><tr><td>{% trans "Fat" %}</td>- <td class="align-right">{{nutritional_data.total.fat|floatformat}} {% trans_weight_unit 'g' user %}</td>+ <td class="align-right">{{nutritional_data.total.fat|floatformat}} {% trans_weight_unit 'g' owner_user %}</td><td class="align-right">{{nutritional_data.percent.fat|floatformat}} %</td><td class="align-right">{{nutritional_data.per_kg.fat|floatformat:2}}</td></tr><tr><td style="padding-left: 2em;">{% trans "Saturated fat content in fats" %}</td>- <td class="align-right">{{nutritional_data.total.fat_saturated|floatformat}} {% trans_weight_unit 'g' user %}</td>+ <td class="align-right">{{nutritional_data.total.fat_saturated|floatformat}} {% trans_weight_unit 'g' owner_user %}</td><td></td><td></td></tr>@@ -216,13 +226,13 @@ function custom_modal_init()</tr><tr><td>{% trans "Fibres" %}</td>- <td class="align-right">{{nutritional_data.total.fibres|floatformat}} {% trans_weight_unit 'g' user %}</td>+ <td class="align-right">{{nutritional_data.total.fibres|floatformat}} {% trans_weight_unit 'g' owner_user %}</td><td></td><td></td></tr><tr><td>{% trans "Sodium" %}</td>- <td class="align-right">{{nutritional_data.total.sodium|floatformat}} {% trans_weight_unit 'g' user %}</td>+ <td class="align-right">{{nutritional_data.total.sodium|floatformat}} {% trans_weight_unit 'g' owner_user %}</td><td></td><td></td></tr>@@ -243,19 +253,19 @@ function custom_modal_init()</thead><tbody>- {% if user.userprofile.calories and plan.has_goal_calories %}+ {% if is_owner and owner_user.userprofile.calories and plan.has_goal_calories %}{% with total=plan.get_calories_approximation %}<tr style="background-color:{% if total == 1 %}#8ae234;{% elif total == 2 %}#fce94f;{% elif total == 3 %}#fcaf3e;{% elif total > 3 %}#ef2929;{% endif %}"><td>{% trans "Goal" %}</td><td class="align-right">- <strong>{{user.userprofile.calories}} {% trans "kcal" %}</strong></td>+ <strong>{{owner_user.userprofile.calories}} {% trans "kcal" %}</strong></td></tr>{% endwith %}{% endif %}</tbody></table>-{% if plan.has_goal_calories %}+{% if is_owner and plan.has_goal_calories %}<p>{% blocktrans %}You have selected that this nutritional plan has agoal amount of calories. Use the calculator or enter the value yourself.{% endblocktrans %}<a href="{% url 'nutrition:calories:view' %}">{% trans "That's done here" %}</a>@@ -278,6 +288,7 @@ function custom_modal_init()</a></p>+{% if is_owner %}<p><a href="{% url 'nutrition:plan:edit' plan.id %}"class="wger-modal-dialog">@@ -310,4 +321,5 @@ function custom_modal_init(){% trans "Delete nutrition plan" %}</a></p>+{% endif %}{% endblock %}diff --git a/wger/nutrition/tests/test_plan.py b/wger/nutrition/tests/test_plan.pyindex 4c2553230..461417c42 100644--- a/wger/nutrition/tests/test_plan.py+++ b/wger/nutrition/tests/test_plan.py@@ -22,6 +22,48 @@ from wger.manager.tests.testcase import WorkoutManagerEditTestCasefrom wger.manager.tests.testcase import WorkoutManagerTestCase+class PlanAccessTestCase(WorkoutManagerTestCase):+ '''+ Test accessing the workout page+ '''++ def test_access_shared(self):+ '''+ Test accessing the URL of a shared workout+ '''+ plan = NutritionPlan.objects.get(pk=5)++ self.user_login('admin')+ response = self.client.get(plan.get_absolute_url())+ self.assertEqual(response.status_code, 200)++ self.user_login('test')+ response = self.client.get(plan.get_absolute_url())+ self.assertEqual(response.status_code, 200)++ self.user_logout()+ response = self.client.get(plan.get_absolute_url())+ self.assertEqual(response.status_code, 200)++ def test_access_not_shared(self):+ '''+ Test accessing the URL of a private workout+ '''+ plan = NutritionPlan.objects.get(pk=4)++ self.user_login('admin')+ response = self.client.get(plan.get_absolute_url())+ self.assertEqual(response.status_code, 403)++ self.user_login('test')+ response = self.client.get(plan.get_absolute_url())+ self.assertEqual(response.status_code, 200)++ self.user_logout()+ response = self.client.get(plan.get_absolute_url())+ self.assertEqual(response.status_code, 403)++class DeletePlanTestCase(WorkoutManagerDeleteTestCase):'''Tests deleting a nutritional plandiff --git a/wger/nutrition/views/plan.py b/wger/nutrition/views/plan.pyindex ee259d1e4..2ee7b2737 100644--- a/wger/nutrition/views/plan.py+++ b/wger/nutrition/views/plan.py@@ -119,17 +119,20 @@ class PlanEditView(WgerFormMixin, UpdateView):return context-@login_requireddef view(request, id):'''Show the nutrition plan with the given ID'''template_data = {}- user = request.user- uid, token = make_token(user)- plan = get_object_or_404(NutritionPlan, pk=id, user=user)- template_data['plan'] = plan+ plan = get_object_or_404(NutritionPlan, pk=id)+ user = plan.user+ is_owner = request.user == user++ if not is_owner and not user.userprofile.ro_access:+ return HttpResponseForbidden()++ uid, token = make_token(user)# Load the language and pass it to the templatelanguage = load_language()@@ -138,11 +141,14 @@ def view(request, id):template_data['MEALITEM_WEIGHT_UNIT'] = MEALITEM_WEIGHT_UNIT# Get the nutritional info+ template_data['plan'] = plantemplate_data['nutritional_data'] = plan.get_nutritional_values()# Tokens for the linkstemplate_data['uid'] = uidtemplate_data['token'] = token+ template_data['owner_user'] = user+ template_data['is_owner'] = is_ownerreturn render(request, 'plan/view.html', template_data)