def export_pdf(request, pk, uidb64=None, token=None): ''' Show the workout schedule ''' user = request.user # Load the workout if uidb64 is not None and token is not None: if check_token(uidb64, token): schedule = get_object_or_404(Schedule, pk=pk) else: return HttpResponseForbidden() else: if request.user.is_anonymous(): return HttpResponseForbidden() schedule = get_object_or_404(Schedule, pk=pk, user=user) # Create the HttpResponse object with the appropriate PDF headers. # and use it to the create the PDF using it as a file like object response = HttpResponse(content_type='application/pdf') doc = SimpleDocTemplate(response, pagesize=A4, leftMargin=cm, rightMargin=cm, topMargin=0.5 * cm, bottomMargin=0.5 * cm, title=_('Workout'), author='wger Workout Manager', subject='Schedule for {0}'.format(request.user.username)) # container for the 'Flowable' objects elements = [] # Set the title p = Paragraph(u'<para align="center">{0}</para>'.format(schedule), styleSheet["HeaderBold"]) elements.append(p) elements.append(Spacer(10 * cm, 0.5 * cm)) # Iterate through the Workout and render the training days for step in schedule.schedulestep_set.all(): p = Paragraph(u'<para>{0} {1}</para>'.format(step.duration, _('Weeks')), styleSheet["HeaderBold"]) elements.append(p) elements.append(Spacer(10 * cm, 0.5 * cm)) for day in step.workout.canonical_representation['day_list']: elements.append(render_workout_day(day, nr_of_weeks=7)) elements.append(Spacer(10 * cm, 0.5 * cm)) # Footer, date and info elements.append(Spacer(10 * cm, 0.5 * cm)) url = reverse('manager:schedule:view', kwargs={'pk': schedule.id}) elements.append(render_footer(request.build_absolute_uri(url))) # write the document and send the response to the browser doc.build(elements) response['Content-Disposition'] = 'attachment; filename=Schedule-{0}-log.pdf'.format(pk) response['Content-Length'] = len(response.content) return response
def export_pdf(request, pk, uidb64=None, token=None): ''' Show the workout schedule ''' user = request.user # Load the workout if uidb64 is not None and token is not None: if check_token(uidb64, token): schedule = get_object_or_404(Schedule, pk=pk) else: return HttpResponseForbidden() else: if request.user.is_anonymous(): return HttpResponseForbidden() schedule = get_object_or_404(Schedule, pk=pk, user=user) # Create the HttpResponse object with the appropriate PDF headers. # and use it to the create the PDF using it as a file like object response = HttpResponse(content_type='application/pdf') doc = SimpleDocTemplate(response, pagesize=A4, leftMargin=cm, rightMargin=cm, topMargin=0.5 * cm, bottomMargin=0.5 * cm, title=_('Workout'), author='wger Workout Manager', subject='Schedule for {0}'.format(request.user.username)) # container for the 'Flowable' objects elements = [] # Set the title p = Paragraph(u'<para align="center">{0}</para>'.format(schedule), styleSheet["HeaderBold"]) elements.append(p) elements.append(Spacer(10*cm, 0.5*cm)) # Iterate through the Workout and render the training days for step in schedule.schedulestep_set.all(): p = Paragraph(u'<para>{0} {1}</para>'.format(step.duration, _('Weeks')), styleSheet["HeaderBold"]) elements.append(p) elements.append(Spacer(10*cm, 0.5*cm)) for day in step.workout.canonical_representation['day_list']: elements.append(render_workout_day(day, nr_of_weeks=7)) elements.append(Spacer(10*cm, 0.5*cm)) # Footer, date and info elements.append(Spacer(10*cm, 0.5*cm)) url = reverse('manager:schedule:view', kwargs={'pk': schedule.id}) elements.append(render_footer(request.build_absolute_uri(url))) # write the document and send the response to the browser doc.build(elements) response['Content-Disposition'] = 'attachment; filename=Schedule-{0}-log.pdf'.format(pk) response['Content-Length'] = len(response.content) return response
def workout_log(request, id, images=False, comments=False, uidb64=None, token=None): """ Generates a PDF with the contents of the given workout See also * http://www.blog.pythonlibrary.org/2010/09/21/reportlab * http://www.reportlab.com/apis/reportlab/dev/platypus.html """ comments = bool(int(comments)) images = bool(int(images)) # Load the workout if uidb64 is not None and token is not None: if check_token(uidb64, token): workout = get_object_or_404(Workout, pk=id) else: return HttpResponseForbidden() else: if request.user.is_anonymous(): return HttpResponseForbidden() workout = get_object_or_404(Workout, pk=id, user=request.user) # Create the HttpResponse object with the appropriate PDF headers. response = HttpResponse(content_type='application/pdf') # Create the PDF object, using the response object as its "file." doc = SimpleDocTemplate( response, pagesize=A4, # pagesize = landscape(A4), leftMargin=cm, rightMargin=cm, topMargin=0.5 * cm, bottomMargin=0.5 * cm, title=_('Workout'), author='wger Workout Manager', subject=_('Workout for %s') % request.user.username) # container for the 'Flowable' objects elements = [] # Set the title p = Paragraph( '<para align="center"><strong>%(description)s</strong></para>' % {'description': workout}, styleSheet["HeaderBold"]) elements.append(p) elements.append(Spacer(10 * cm, 0.5 * cm)) # Iterate through the Workout and render the training days for day in workout.canonical_representation['day_list']: elements.append( render_workout_day(day, images=images, comments=comments)) elements.append(Spacer(10 * cm, 0.5 * cm)) # Footer, date and info elements.append(Spacer(10 * cm, 0.5 * cm)) elements.append( render_footer(request.build_absolute_uri(workout.get_absolute_url()))) # write the document and send the response to the browser doc.build(elements) # Create the HttpResponse object with the appropriate PDF headers. response[ 'Content-Disposition'] = 'attachment; filename=Workout-{0}-log.pdf'.format( id) response['Content-Length'] = len(response.content) return response
def export_pdf(request, id, uidb64=None, token=None): """ Generates a PDF with the contents of a nutrition plan See also * http://www.blog.pythonlibrary.org/2010/09/21/reportlab * http://www.reportlab.com/apis/reportlab/dev/platypus.html """ # Load the plan if uidb64 is not None and token is not None: if check_token(uidb64, token): plan = get_object_or_404(NutritionPlan, pk=id) else: return HttpResponseForbidden() else: if request.user.is_anonymous: return HttpResponseForbidden() plan = 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(content_type='application/pdf') # Create the PDF object, using the response object as its "file." doc = SimpleDocTemplate( response, pagesize=A4, title=_('Nutritional plan'), author='wger Workout Manager', subject=_('Nutritional plan for %s') % request.user.username, topMargin=1 * cm, ) # container for the 'Flowable' objects elements = [] data = [] # Iterate through the Plan meal_markers = [] ingredient_markers = [] # Meals i = 0 for meal in plan.meal_set.select_related(): i += 1 meal_markers.append(len(data)) if not meal.time: p = Paragraph( '<para align="center"><strong>{nr} {meal_nr}</strong></para>'. format(nr=_('Nr.'), meal_nr=i), styleSheet["SubHeader"]) else: p = Paragraph( '<para align="center"><strong>' '{nr} {meal_nr} - {meal_time}' '</strong></para>'.format( nr=_('Nr.'), meal_nr=i, meal_time=meal.time.strftime("%H:%M")), styleSheet["SubHeader"]) data.append([p]) # Ingredients for item in meal.mealitem_set.select_related(): ingredient_markers.append(len(data)) p = Paragraph('<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([ Paragraph("{0:.0f}{1}".format(item.amount, unit_name), styleSheet["Normal"]), p ]) # Add filler data.append([Spacer(1 * cm, 0.6 * cm)]) # Set general table styles table_style = [] # Set specific styles, e.g. background for title cells for marker in meal_markers: # Set background colour for headings table_style.append( ('BACKGROUND', (0, marker), (-1, marker), header_colour)) 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))) # has the plan any data? if data: t = Table(data, style=table_style) # Manually set the width of the columns t._argW[0] = 3.5 * 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"]) # Add site logo elements.append(get_logo()) elements.append(Spacer(10 * cm, 0.5 * cm)) # Set the title (if available) if plan.description: p = Paragraph( '<para align="center"><strong>%(description)s</strong></para>' % {'description': plan.description}, styleSheet["HeaderBold"]) elements.append(p) # Filler elements.append(Spacer(10 * cm, 1.5 * cm)) # append the table to the document elements.append(t) elements.append(Paragraph('<para> </para>', styleSheet["Normal"])) # Create table with nutritional calculations data = [] data.append([ Paragraph( '<para align="center">{0}</para>'.format(_('Nutritional data')), styleSheet["SubHeaderBlack"]) ]) 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(str(plan_data['total']['energy']), styleSheet["Normal"]) ]) data.append([ Paragraph(_('Protein'), styleSheet["Normal"]), Paragraph(str(plan_data['total']['protein']), styleSheet["Normal"]), Paragraph(str(plan_data['percent']['protein']), styleSheet["Normal"]), Paragraph(str(plan_data['per_kg']['protein']), styleSheet["Normal"]) ]) data.append([ Paragraph(_('Carbohydrates'), styleSheet["Normal"]), Paragraph(str(plan_data['total']['carbohydrates']), styleSheet["Normal"]), Paragraph(str(plan_data['percent']['carbohydrates']), styleSheet["Normal"]), Paragraph(str(plan_data['per_kg']['carbohydrates']), styleSheet["Normal"]) ]) data.append([ Paragraph(" " + _('Sugar content in carbohydrates'), styleSheet["Normal"]), Paragraph(str(plan_data['total']['carbohydrates_sugar']), styleSheet["Normal"]) ]) data.append([ Paragraph(_('Fat'), styleSheet["Normal"]), Paragraph(str(plan_data['total']['fat']), styleSheet["Normal"]), Paragraph(str(plan_data['percent']['fat']), styleSheet["Normal"]), Paragraph(str(plan_data['per_kg']['fat']), styleSheet["Normal"]) ]) data.append([ Paragraph(_('Saturated fat content in fats'), styleSheet["Normal"]), Paragraph(str(plan_data['total']['fat_saturated']), styleSheet["Normal"]) ]) data.append([ Paragraph(_('Fibres'), styleSheet["Normal"]), Paragraph(str(plan_data['total']['fibres']), styleSheet["Normal"]) ]) data.append([ Paragraph(_('Sodium'), styleSheet["Normal"]), Paragraph(str(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(('BACKGROUND', (0, 3), (-1, 3), row_color)) # Protein table_style.append( ('BACKGROUND', (0, 4), (-1, 4), row_color)) # Carbohydrates table_style.append(('SPAN', (1, 5), (-1, 5))) # Sugar table_style.append(('LEFTPADDING', (0, 5), (0, 5), 15)) table_style.append(('BACKGROUND', (0, 6), (-1, 6), row_color)) # Fats table_style.append(('SPAN', (1, 7), (-1, 7))) # Saturated fats table_style.append(('LEFTPADDING', (0, 7), (0, 7), 15)) 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] = 6 * cm elements.append(t) # Footer, date and info elements.append(Spacer(10 * cm, 0.5 * cm)) elements.append( render_footer(request.build_absolute_uri(plan.get_absolute_url()))) doc.build(elements) response[ 'Content-Disposition'] = 'attachment; filename=nutritional-plan.pdf' response['Content-Length'] = len(response.content) return response
def workout_log(request, id, images=False, comments=False, uidb64=None, token=None): ''' Generates a PDF with the contents of the given workout See also * http://www.blog.pythonlibrary.org/2010/09/21/reportlab * http://www.reportlab.com/apis/reportlab/dev/platypus.html ''' comments = bool(int(comments)) images = bool(int(images)) # Load the workout if uidb64 is not None and token is not None: if check_token(uidb64, token): workout = get_object_or_404(Workout, pk=id) else: return HttpResponseForbidden() else: if request.user.is_anonymous(): return HttpResponseForbidden() workout = get_object_or_404(Workout, pk=id, user=request.user) # Create the HttpResponse object with the appropriate PDF headers. response = HttpResponse(content_type='application/pdf') # Create the PDF object, using the response object as its "file." doc = SimpleDocTemplate(response, pagesize=A4, # pagesize = landscape(A4), leftMargin=cm, rightMargin=cm, topMargin=0.5 * cm, bottomMargin=0.5 * cm, title=_('Workout'), author='wger Workout Manager', subject=_('Workout for %s') % request.user.username) # container for the 'Flowable' objects elements = [] # Set the title p = Paragraph('<para align="center"><strong>%(description)s</strong></para>' % {'description': workout}, styleSheet["HeaderBold"]) elements.append(p) elements.append(Spacer(10 * cm, 0.5 * cm)) # Iterate through the Workout and render the training days for day in workout.canonical_representation['day_list']: elements.append(render_workout_day(day, images=images, comments=comments)) elements.append(Spacer(10 * cm, 0.5 * cm)) # Footer, date and info elements.append(Spacer(10 * cm, 0.5 * cm)) elements.append(render_footer(request.build_absolute_uri(workout.get_absolute_url()))) # write the document and send the response to the browser doc.build(elements) # Create the HttpResponse object with the appropriate PDF headers. response['Content-Disposition'] = 'attachment; filename=Workout-{0}-log.pdf'.format(id) response['Content-Length'] = len(response.content) return response