Self hosted FLOSS fitness/workout, nutrition and weight tracker
Show table with nutritional information on PDF as well
Roland Geider1616eeaf7b74
diff --git a/wger/nutrition/templates/plan/view.html b/wger/nutrition/templates/plan/view.htmlindex 2699914a5..a90fd37f4 100644--- a/wger/nutrition/templates/plan/view.html+++ b/wger/nutrition/templates/plan/view.html@@ -49,8 +49,10 @@ $(document).ready(function() {<tr>{% ifchanged meal.pk %}<td rowspan="{{meal.mealitem_set.count}}">+ <strong>{% trans "Nr."%} {{ forloop.parentloop.counter }}{% if meal.time %} – {{meal.time|time:"H:i"}}{% endif %}+ </strong><div class="editoptions"><a href="{% url 'wger.nutrition.views.meal.delete_meal' meal.id %}"diff --git a/wger/nutrition/tests/test_pdf.py b/wger/nutrition/tests/test_pdf.pyindex 546957659..3423b9006 100644--- a/wger/nutrition/tests/test_pdf.py+++ b/wger/nutrition/tests/test_pdf.py@@ -42,8 +42,9 @@ class NutritionalPlanPdfExportTestCase(WorkoutManagerTestCase):self.assertEqual(response['Content-Disposition'],'attachment; filename=nutritional-plan.pdf')- # Approximate size, there's a timestamp in the document- self.assertGreater(len(response.content), 2800)+ # Approximate size+ self.assertGreater(int(response['Content-Length']), 33900)+ self.assertLess(int(response['Content-Length']), 34000)# Create an empty planuser = User.objects.get(pk=2)@@ -63,8 +64,9 @@ class NutritionalPlanPdfExportTestCase(WorkoutManagerTestCase):self.assertEqual(response['Content-Disposition'],'attachment; filename=nutritional-plan.pdf')- # Approximate size, there's a timestamp in the document- self.assertGreater(len(response.content), 2800)+ # Approximate size+ self.assertGreater(int(response['Content-Length']), 33300)+ self.assertLess(int(response['Content-Length']), 33380)def test_export_pdf_anonymous(self):'''diff --git a/wger/nutrition/views/plan.py b/wger/nutrition/views/plan.pyindex 8ea813cb6..49e956401 100644--- a/wger/nutrition/views/plan.py+++ b/wger/nutrition/views/plan.py@@ -174,10 +174,10 @@ def export_pdf(request, id):#Load the workoutplan = get_object_or_404(NutritionPlan, pk=id, user=request.user)+ plan_data = plan.get_nutritional_values()# Create the HttpResponse object with the appropriate PDF headers.response = HttpResponse(mimetype='application/pdf')- response['Content-Disposition'] = 'attachment; filename=nutritional-plan.pdf'# Create the PDF object, using the response object as its "file."doc = SimpleDocTemplate(response,@@ -195,7 +195,6 @@ def export_pdf(request, id):# container for the 'Flowable' objectselements = []-data = []# Iterate through the Plan@@ -210,34 +209,31 @@ def export_pdf(request, id):meal_markers.append(len(data))if not meal.time:- P = Paragraph('<para align="center"><strong>%(meal_nr)s</strong></para>' %- {'meal_nr': i},+ p = Paragraph(u'<para align="center"><strong>{nr} {meal_nr}</strong></para>'+ .format(nr=_('Nr.'), meal_nr=i),styleSheet["Normal"])else:- P = Paragraph('<para align="center"><strong>%(meal_nr)s - '- '%(meal_time)s</strong></para>' %- {'meal_nr': i,- 'meal_time': meal.time.strftime("%H:%M")},+ p = Paragraph(u'<para align="center"><strong>'+ u'{nr} {meal_nr} - {meal_time}'+ u'</strong></para>'+ .format(nr=_('Nr.'), meal_nr=i, meal_time=meal.time.strftime("%H:%M")),styleSheet["Normal"])- data.append([P])+ data.append([p])# Ingredientsfor item in meal.mealitem_set.select_related():ingredient_markers.append(len(data))- P = Paragraph('<para>%s</para>' % item.ingredient.name,- styleSheet["Normal"])-+ p = Paragraph(u'<para>{0}</para>'.format(item.ingredient.name), styleSheet["Normal"])if item.get_unit_type() == MEALITEM_WEIGHT_GRAM:unit_name = 'g'else:unit_name = ' ' + item.weight_unit.unit.name- data.append([u"{0}{1}".format(item.amount, unit_name), P])- # Set general table styles+ data.append([Paragraph(u"{0}{1}".format(item.amount, unit_name), styleSheet["Normal"]),+ p])- #('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),- #('BOX', (0,0), (-1,-1), 0.25, colors.black)+ # Set general table stylestable_style = []# Set specific styles, e.g. background for title cells@@ -254,7 +250,7 @@ def export_pdf(request, id):t = Table(data, style=table_style)# Manually set the width of the columns- t._argW[0] = 2 * cm+ t._argW[0] = 2.5 * cm# There is nothing to outputelse:@@ -263,26 +259,71 @@ def export_pdf(request, id):# Set the title (if available)if plan.description:- P = Paragraph('<para align="center"><strong>%(description)s</strong></para>' %+ p = Paragraph('<para align="center"><strong>%(description)s</strong></para>' %{'description': plan.description},styleSheet["Normal"])- elements.append(P)+ elements.append(p)# Filler- P = Paragraph('<para> </para>', styleSheet["Normal"])- elements.append(P)+ p = Paragraph('<para> </para>', styleSheet["Normal"])+ elements.append(p)# append the table to the documentelements.append(t)+ elements.append(Paragraph('<para> </para>', styleSheet["Normal"]))++ # Create table with nutritional calculations+ data = []+ data.append([Paragraph(u'<para align="center">{0}</para>'.format(_('Nutritional data')),+ styleSheet["Bold"])])+ data.append([Paragraph(_('Macronutrients'), styleSheet["Normal"]),+ Paragraph(_('Total'), styleSheet["Normal"]),+ Paragraph(_('Percent of energy'), styleSheet["Normal"]),+ Paragraph(_('g per body kg'), styleSheet["Normal"])])+ data.append([Paragraph(_('Energy'), styleSheet["Normal"]),+ Paragraph(unicode(plan_data['total']['energy']), styleSheet["Normal"])])+ data.append([Paragraph(_('Protein'), styleSheet["Normal"]),+ Paragraph(unicode(plan_data['total']['protein']), styleSheet["Normal"]),+ Paragraph(unicode(plan_data['percent']['protein']), styleSheet["Normal"]),+ Paragraph(unicode(plan_data['per_kg']['protein']), styleSheet["Normal"])])+ data.append([Paragraph(_('Carbohydrates'), styleSheet["Normal"]),+ Paragraph(unicode(plan_data['total']['carbohydrates']), styleSheet["Normal"]),+ Paragraph(unicode(plan_data['percent']['carbohydrates']), styleSheet["Normal"]),+ Paragraph(unicode(plan_data['per_kg']['carbohydrates']), styleSheet["Normal"])])+ data.append([Paragraph(_('Sugar content in carbohydrates'), styleSheet["Normal"]),+ Paragraph(unicode(plan_data['total']['carbohydrates_sugar']),+ styleSheet["Normal"])])+ data.append([Paragraph(_('Fat'), styleSheet["Normal"]),+ Paragraph(unicode(plan_data['total']['fat']), styleSheet["Normal"]),+ Paragraph(unicode(plan_data['percent']['fat']), styleSheet["Normal"]),+ Paragraph(unicode(plan_data['per_kg']['fat']), styleSheet["Normal"])])+ data.append([Paragraph(_('Saturated fat content in fats'), styleSheet["Normal"]),+ Paragraph(unicode(plan_data['total']['fat_saturated']), styleSheet["Normal"])])+ data.append([Paragraph(_('Fibres'), styleSheet["Normal"]),+ Paragraph(unicode(plan_data['total']['fibres']), styleSheet["Normal"])])+ data.append([Paragraph(_('Sodium'), styleSheet["Normal"]),+ Paragraph(unicode(plan_data['total']['sodium']), styleSheet["Normal"])])++ table_style = []+ table_style.append(('BOX', (0, 0), (-1, -1), 1.25, colors.black))+ table_style.append(('GRID', (0, 0), (-1, -1), 0.40, colors.black))+ table_style.append(('SPAN', (0, 0), (-1, 0))) # Title+ table_style.append(('SPAN', (1, 2), (-1, 2))) # Energy+ table_style.append(('SPAN', (1, 5), (-1, 5))) # Sugar+ table_style.append(('SPAN', (1, 7), (-1, 7))) # Saturated fats+ table_style.append(('SPAN', (1, 8), (-1, 8))) # Fibres+ table_style.append(('SPAN', (1, 9), (-1, 9))) # Sodium+ t = Table(data, style=table_style)+ t._argW[0] = 5 * cm+ elements.append(t)# Footer, add filler paragraph- P = Paragraph('<para> </para>', styleSheet["Normal"])- elements.append(P)+ elements.append(Paragraph('<para> </para>', styleSheet["Normal"]))# Print date and infocreated = datetime.date.today().strftime("%d.%m.%Y")url = reverse('nutrition-view', kwargs={'id': plan.id})- P = Paragraph('''<para align="left">+ p = Paragraph('''<para align="left">%(date)s -<a href="%(url)s">%(url)s</a> -%(created)s@@ -293,7 +334,9 @@ def export_pdf(request, id):'version': get_version(),'url': request.build_absolute_uri(url), },styleSheet["Normal"])- elements.append(P)+ elements.append(p)doc.build(elements)+ response['Content-Disposition'] = 'attachment; filename=nutritional-plan.pdf'+ response['Content-Length'] = len(response.content)return response