Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Fix problem with PDF when the nutritional plan has no data

Roland Geiderfe0ca5709cab
diff --git a/wger/nutrition/tests/pdf.py b/wger/nutrition/tests/pdf.py
index 779c13aef..d8861a95b 100644
--- a/wger/nutrition/tests/pdf.py
+++ b/wger/nutrition/tests/pdf.py
@@ -13,7 +13,10 @@
# You should have received a copy of the GNU Affero General Public License
from django.core.urlresolvers import reverse
+from django.contrib.auth.models import User
+from wger.nutrition.models import NutritionPlan
+from wger.exercises.models import Language
from wger.manager.tests.testcase import WorkoutManagerTestCase
@@ -27,7 +30,7 @@ class NutritionalPlanPdfExportTestCase(WorkoutManagerTestCase):
Helper function to test exporting a nutritional plan as a pdf
'''
- # Create a workout
+ # Get a plan
response = self.client.get(reverse('wger.nutrition.views.plan.export_pdf',
kwargs={'id': 4}))
@@ -42,6 +45,27 @@ class NutritionalPlanPdfExportTestCase(WorkoutManagerTestCase):
# Approximate size, there's a timestamp in the document
self.assertGreater(len(response.content), 2800)
+ # Create an empty plan
+ user = User.objects.get(pk=2)
+ language = Language.objects.get(pk=1)
+ plan = NutritionPlan()
+ plan.user = user
+ plan.language = language
+ plan.save()
+ response = self.client.get(reverse('wger.nutrition.views.plan.export_pdf',
+ kwargs={'id': plan.id}))
+
+ if fail:
+ self.assertIn(response.status_code, (404, 302))
+ else:
+ self.assertEqual(response.status_code, 200)
+ self.assertEqual(response['Content-Type'], 'application/pdf')
+ 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)
+
def test_export_pdf_anonymous(self):
'''
Tests exporting a nutritional plan as a pdf as an anonymous user
diff --git a/wger/nutrition/views/plan.py b/wger/nutrition/views/plan.py
index 7d5b58a8e..5c5ef824e 100644
--- a/wger/nutrition/views/plan.py
+++ b/wger/nutrition/views/plan.py
@@ -254,10 +254,17 @@ def export_pdf(request, id):
# Make the headings span the whole width
table_style.append(('SPAN', (0, marker), (-1, marker)))
- t = Table(data, style=table_style)
+ # has the plan any data?
+ if data:
+ t = Table(data, style=table_style)
- # Manually set the width of the columns
- t._argW[0] = 2 * cm
+ # Manually set the width of the columns
+ t._argW[0] = 2 * cm
+
+ # There is nothing to output
+ else:
+ t = Paragraph(_('<i>This is an empty plan, what did you expect on the PDF?</i>'),
+ styleSheet["Normal"])
# Set the title (if available)
if plan.description: