Added calories to meal plan presentation

This commit is contained in:
Jim Martens 2020-02-23 16:06:44 +01:00
parent bcd7dd1997
commit 060db6b867
3 changed files with 63 additions and 3 deletions

View File

@ -70,6 +70,16 @@
<td>{{ mealplan.saturday_dinner.name }}</td>
<td>{{ mealplan.sunday_dinner.name }}</td>
</tr>
<tr>
<th scope="row">{% trans "Calories vs demand" %}</th>
<td>{{ calories_monday }}%</td>
<td>{{ calories_tuesday }}%</td>
<td>{{ calories_wednesday }}%</td>
<td>{{ calories_thursday }}%</td>
<td>{{ calories_friday }}%</td>
<td>{{ calories_saturday }}%</td>
<td>{{ calories_sunday }}%</td>
</tr>
</tbody>
</table>
{% endblock %}

View File

@ -8,5 +8,5 @@ urlpatterns = [
path('', views.index, name='index'),
path('legal-notice/', views.legal, name='legal'),
path('privacy/', views.privacy, name='privacy'),
path('mealplans/<int:user_id>/<str:mealplan>/', views.mealplan, name='mealplan'),
path('mealplans/<int:user_id>/<str:mealplan_title>/', views.mealplan, name='mealplan'),
]

View File

@ -15,6 +15,7 @@ from django.urls import reverse
from food_planner.forms import ProfileForm
from food_planner.forms import UserForm
from food_planner.models import MealPlan
from food_planner.models import Profile
def index(request) -> HttpResponse:
@ -30,16 +31,65 @@ def index(request) -> HttpResponse:
return render(request, template_name='food_planner/landing.html', context=context)
def mealplan(request, user_id: int, mealplan: str) -> HttpResponse:
def mealplan(request, user_id: int, mealplan_title: str) -> HttpResponse:
mealplan_obj = MealPlan.objects.get(author_id=user_id, name=mealplan_title)
# calculate calories for each day, TODO: extract later
profile = Profile.objects.get(user=request.user)
calories_required = profile.daily_calories_demand
calories_monday = get_calories_sum(mealplan_obj, 'monday')
calories_monday_percentage = calories_monday / calories_required * 100
calories_tuesday = get_calories_sum(mealplan_obj, 'tuesday')
calories_tuesday_percentage = calories_tuesday / calories_required * 100
calories_wednesday = get_calories_sum(mealplan_obj, 'wednesday')
calories_wednesday_percentage = calories_wednesday / calories_required * 100
calories_thursday = get_calories_sum(mealplan_obj, 'thursday')
calories_thursday_percentage = calories_thursday / calories_required * 100
calories_friday = get_calories_sum(mealplan_obj, 'friday')
calories_friday_percentage = calories_friday / calories_required * 100
calories_saturday = get_calories_sum(mealplan_obj, 'saturday')
calories_saturday_percentage = calories_saturday / calories_required * 100
calories_sunday = get_calories_sum(mealplan_obj, 'sunday')
calories_sunday_percentage = calories_sunday / calories_required * 100
context = {
'site': {
'title': 'Food planner'
},
'mealplan': MealPlan.objects.get(author_id=user_id, name=mealplan)
'mealplan': mealplan_obj,
'calories_monday': f"{calories_monday_percentage:.2f}",
'calories_tuesday': f"{calories_tuesday_percentage:.2f}",
'calories_wednesday': f"{calories_wednesday_percentage:.2f}",
'calories_thursday': f"{calories_thursday_percentage:.2f}",
'calories_friday': f"{calories_friday_percentage:.2f}",
'calories_saturday': f"{calories_saturday_percentage:.2f}",
'calories_sunday': f"{calories_sunday_percentage:.2f}",
}
return render(request, template_name='food_planner/mealplan.html', context=context)
def get_calories_sum(mealplan: MealPlan, weekday: str) -> float:
calories_sum = 0
breakfast = mealplan.__getattribute__(weekday + '_breakfast')
morning_snack = mealplan.__getattribute__(weekday + '_morning_snack')
lunch = mealplan.__getattribute__(weekday + '_lunch')
afternoon_snack = mealplan.__getattribute__(weekday + '_afternoon_snack')
dinner = mealplan.__getattribute__(weekday + '_dinner')
calories_sum += breakfast.calories if breakfast is not None else 0
calories_sum += morning_snack.calories if morning_snack is not None else 0
calories_sum += lunch.calories if lunch is not None else 0
calories_sum += afternoon_snack.calories if afternoon_snack is not None else 0
calories_sum += dinner.calories if dinner is not None else 0
return calories_sum
@transaction.atomic
def register(request) -> HttpResponse:
if request.user.is_authenticated: