Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add first version of PDF export for nutritional plan

Roland Geider42b9104bc8b7
diff --git a/nutrition/templates/view_nutrition_plan.html b/nutrition/templates/view_nutrition_plan.html
index 4a1fa97a0..8ef513ca9 100644
--- a/nutrition/templates/view_nutrition_plan.html
+++ b/nutrition/templates/view_nutrition_plan.html
@@ -191,7 +191,7 @@ $(document).ready(function() {
</p>
<p>
-<a href="{% url manager.views.pdf_workout 1 %}">
+<a href="{% url nutrition.views.export_pdf plan.id %}">
<img src="{% static 'images/icons/save.svg' %}"
width="32"
height="32"
diff --git a/nutrition/urls.py b/nutrition/urls.py
index bb6f4a7ac..6e7304063 100644
--- a/nutrition/urls.py
+++ b/nutrition/urls.py
@@ -21,8 +21,8 @@ urlpatterns = patterns('nutrition.views',
url(r'^nutrition/ingredient/overview/$', 'ingredient_overview'),
url(r'^nutrition/ingredient/view/(?P<id>\d+)$', 'ingredient_view'),
url(r'^nutrition/ingredient/edit/(?P<id>\w*)$', 'ingredient_edit'),
- url(r'^nutrition/ingredient/delete/(?P<id>\w*)$', 'delete_ingredient'),
+ url(r'^nutrition/ingredient/delete/(?P<id>\d+)$', 'delete_ingredient'),
url(r'^nutrition/ingredient/search/$', 'ingredient_search'),
- #url(r'^nutrition/export_pdf/$', 'export_pdf'),
+ url(r'^nutrition/export_pdf/(?P<id>\d+)$', 'export_pdf'),
)
diff --git a/nutrition/views.py b/nutrition/views.py
index e506987be..3e9483149 100644
--- a/nutrition/views.py
+++ b/nutrition/views.py
@@ -24,19 +24,27 @@ from django.core.context_processors import csrf
from django.contrib.auth.decorators import login_required
from django.contrib.auth.decorators import permission_required
from django.forms import ModelForm
-
+from django.utils.translation import ugettext as _
from nutrition.models import NutritionPlan
from nutrition.models import Meal
from nutrition.models import MealItem
from nutrition.models import Ingredient
+from reportlab.lib.styles import getSampleStyleSheet
+from reportlab.lib.pagesizes import A4, cm
+from reportlab.platypus import Paragraph, SimpleDocTemplate, Table
+from reportlab.lib import colors
from exercises.views import load_language
logger = logging.getLogger('workout_manager.custom')
+# ************************
+# Plan functions
+# ************************
+
class PlanForm(ModelForm):
class Meta:
model = NutritionPlan
@@ -138,6 +146,84 @@ def edit_plan(request, id):
template_data,
context_instance=RequestContext(request))
+def export_pdf(request, id):
+ """Generates a PDF with the contents of a nutrition plan
+
+ See also
+ * http://www.blog.pythonlibrary.org/2010/09/21/reportlab-tables-creating-tables-in-pdfs-with-python/
+ * http://www.reportlab.com/apis/reportlab/dev/platypus.html
+ """
+
+ #Load the workout
+ plan = get_object_or_404(NutritionPlan, pk=id, user=request.user)
+
+ # Create the HttpResponse object with the appropriate PDF headers.
+ response = HttpResponse(mimetype='application/pdf')
+ response['Content-Disposition'] = 'attachment; filename=plan.pdf'
+
+ # Create the PDF object, using the response object as its "file."
+ doc = SimpleDocTemplate(response,
+ pagesize = A4,
+ title = _('Workout'),
+ author = _('Workout Manager'),
+ subject = _('Nutritional plan %s') % request.user.username)
+
+ # container for the 'Flowable' objects
+ elements = []
+
+ styleSheet = getSampleStyleSheet()
+ data = []
+
+ # Iterate through the Plan
+ meal_markers = []
+ ingredient_markers = []
+
+
+ # Meals
+ for meal in plan.meal_set.select_related():
+ meal_markers.append(len(data))
+
+ P = Paragraph('<para align="center"><strong>%s</strong></para>' % meal.time,
+ styleSheet["Normal"])
+
+ data.append([P])
+
+ # Ingredients
+ for item in meal.mealitem_set.select_related():
+ ingredient_markers.append(len(data))
+
+ P = Paragraph('<para>%s</para>' % item.ingredient.name,
+ styleSheet["Normal"])
+ data.append([item.amount_gramm, P])
+
+ # Set general table styles
+ table_style = []
+ table_style = [('INNERGRID', (0,0), (-1,-1), 0.25, colors.black),
+ ('BOX', (0,0), (-1,-1), 0.25, colors.black)]
+
+ # Set specific styles, e.g. background for title cells
+ previous_marker = 0
+ for marker in meal_markers:
+ # Set background colour for headings
+ table_style.append(('BACKGROUND', (0, marker), (-1, marker), colors.green))
+ table_style.append(('BOX', (0, marker), (-1, marker), 1.25, colors.black))
+
+ # Make the headings span the whole width
+ table_style.append(('SPAN', (0, marker), (-1, marker)))
+
+
+ t = Table(data, style = table_style)
+
+ # Manually set the width of the columns
+ t._argW[0] = 2 * cm
+
+
+
+ # write the document and send the response to the browser
+ elements.append(t)
+ doc.build(elements)
+
+ return response
# ************************
@@ -318,7 +404,9 @@ def ingredient_overview(request):
template_data = {}
template_data['active_tab'] = 'nutrition'
- ingredients = Ingredient.objects.filter(language = language.id)
+ #ingredients = Ingredient.objects.filter(language = language.id)
+ ingredients = Ingredient.objects.all()
+
template_data['ingredients'] = ingredients
return render_to_response('ingredient_overview.html',