def view(request, id): ''' Show the nutrition plan with the given ID ''' template_data = {} user = request.user uid, token = make_token(user) plan = get_object_or_404(NutritionPlan, pk=id, user=user) template_data['plan'] = plan # Load the language and pass it to the template language = load_language() template_data['language'] = language template_data['MEALITEM_WEIGHT_GRAM'] = MEALITEM_WEIGHT_GRAM template_data['MEALITEM_WEIGHT_UNIT'] = MEALITEM_WEIGHT_UNIT # Get the nutritional info template_data['nutritional_data'] = plan.get_nutritional_values() # Tokens for the links template_data['uid'] = uid template_data['token'] = token return render(request, 'plan/view.html', template_data)
def export_pdf_token(self): """ Helper function to test exporting a workout as a pdf using tokens """ user = User.objects.get(username='******') uid, token = make_token(user) response = self.client.get( reverse('manager:workout:pdf-table', kwargs={ 'id': 3, 'uidb64': uid, 'token': token }) ) self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-Type'], 'application/pdf') self.assertEqual( response['Content-Disposition'], 'attachment; filename=Workout-3-table.pdf', ) # Approximate size only self.assertGreater(int(response['Content-Length']), 38000) self.assertLess(int(response['Content-Length']), 42000)
def view(request, id): ''' Show the workout with the given ID ''' template_data = {} user = request.user workout = get_object_or_404(Workout, pk=id, user=user) canonical = workout.canonical_representation uid, token = make_token(user) # Create the backgrounds that show what muscles the workout will work on muscles_front = [] muscles_back = [] for i in canonical['muscles']['front']: if i not in muscles_front: muscles_front.append('images/muscles/main/muscle-{0}.svg'.format(i)) for i in canonical['muscles']['back']: if i not in muscles_back: muscles_back.append('images/muscles/main/muscle-{0}.svg'.format(i)) # Append the silhouette of the human body as the last entry so the browser # renders it in the background muscles_front.append('images/muscles/muscular_system_front.svg') muscles_back.append('images/muscles/muscular_system_back.svg') template_data['workout'] = workout template_data['muscle_backgrounds_front'] = muscles_front template_data['muscle_backgrounds_back'] = muscles_back template_data['uid'] = uid template_data['token'] = token return render(request, 'workout/view.html', template_data)
def export_pdf_with_images_and_comments(self, fail=False, pdf_type="log"): ''' Helper function to test exporting a workout as a pdf, with images and comments ''' user = User.objects.get(username='******') uid, token = make_token(user) response = self.client.get(reverse('manager:schedule:pdf-{0}'.format(pdf_type), kwargs={'pk': 3, 'images': 1, 'comments': 1, 'uidb64': uid, 'token': token})) if fail: self.assertIn(response.status_code, (403, 404, 302)) else: self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-Type'], 'application/pdf') self.assertEqual(response['Content-Disposition'], 'attachment; filename=Schedule-3-{0}.pdf'.format(pdf_type)) # Approximate size only self.assertGreater(int(response['Content-Length']), 29000) self.assertLess(int(response['Content-Length']), 35000)
def view(request, id): ''' Show the nutrition plan with the given ID ''' template_data = {} plan = get_object_or_404(NutritionPlan, pk=id) user = plan.user is_owner = request.user == user if not is_owner and not user.userprofile.ro_access: return HttpResponseForbidden() uid, token = make_token(user) # Load the language and pass it to the template language = load_language() template_data['language'] = language template_data['MEALITEM_WEIGHT_GRAM'] = MEALITEM_WEIGHT_GRAM template_data['MEALITEM_WEIGHT_UNIT'] = MEALITEM_WEIGHT_UNIT # Get the nutritional info template_data['plan'] = plan template_data['nutritional_data'] = plan.get_nutritional_values() # Tokens for the links template_data['uid'] = uid template_data['token'] = token template_data['owner_user'] = user template_data['is_owner'] = is_owner template_data['show_shariff'] = is_owner return render(request, 'plan/view.html', template_data)
def view(request, pk): ''' Show the workout schedule ''' template_data = {} schedule = get_object_or_404(Schedule, pk=pk) user = schedule.user is_owner = request.user == user if not is_owner and not user.userprofile.ro_access: return HttpResponseForbidden() uid, token = make_token(user) template_data['schedule'] = schedule if schedule.is_active: template_data['active_workout'] = schedule.get_current_scheduled_workout() else: template_data['active_workout'] = False schedule.get_current_scheduled_workout() template_data['uid'] = uid template_data['token'] = token template_data['is_owner'] = is_owner template_data['owner_user'] = user template_data['show_shariff'] = is_owner return render(request, 'schedule/view.html', template_data)
def export_pdf_token(self, pdf_type="log"): ''' Helper function to test exporting a workout as a pdf using tokens ''' user = User.objects.get(username='******') uid, token = make_token(user) response = self.client.get(reverse('manager:schedule:pdf-{0}'.format(pdf_type), kwargs={'pk': 1, 'uidb64': uid, 'token': token})) self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-Type'], 'application/pdf') self.assertEqual(response['Content-Disposition'], 'attachment; filename=Schedule-1-{0}.pdf'.format(pdf_type)) # Approximate size only self.assertGreater(int(response['Content-Length']), 29000) self.assertLess(int(response['Content-Length']), 35000) # Wrong or expired token uid = 'MQ' token = '3xv-57ef74923091fe7f186e' response = self.client.get(reverse('manager:schedule:pdf-{0}'.format(pdf_type), kwargs={'pk': 1, 'uidb64': uid, 'token': token})) self.assertEqual(response.status_code, 403)
def view(request, id): """ Show the nutrition plan with the given ID """ template_data = {} plan = get_object_or_404(NutritionPlan, pk=id) user = plan.user is_owner = request.user == user if not is_owner and not user.userprofile.ro_access: return HttpResponseForbidden() uid, token = make_token(user) # Load the language and pass it to the template language = load_language() template_data["language"] = language template_data["MEALITEM_WEIGHT_GRAM"] = MEALITEM_WEIGHT_GRAM template_data["MEALITEM_WEIGHT_UNIT"] = MEALITEM_WEIGHT_UNIT # Get the nutritional info template_data["plan"] = plan template_data["nutritional_data"] = plan.get_nutritional_values() # Tokens for the links template_data["uid"] = uid template_data["token"] = token template_data["owner_user"] = user template_data["is_owner"] = is_owner template_data["show_shariff"] = is_owner return render(request, "plan/view.html", template_data)
def calendar(request, username=None, year=None, month=None): ''' Show a calendar with all the workout logs ''' is_owner, user = check_access(request.user, username) logger.info('aa bb cc') uid, token = make_token(user) year = int(year) if year else datetime.date.today().year month = int(month) if month else datetime.date.today().month context = {} logs = WorkoutLog.objects.filter(user=user, date__year=year, date__month=month).order_by('exercise') logs_filtered = cache.get( cache_mapper.get_workout_log(user.pk, year, month)) if not logs_filtered: logs_filtered = {} # Process the logs. Group by date and check for impressions for log in logs: if log.date not in logs_filtered: session = log.get_workout_session() if session: impression = session.impression else: # Default is 'neutral' impression = WorkoutSession.IMPRESSION_NEUTRAL logs_filtered[log.date.day] = { 'impression': impression, 'log': log } cache.set(cache_mapper.get_workout_log(user.pk, year, month), logs_filtered) (current_workout, schedule) = Schedule.objects.get_current_workout(user) context['calendar'] = WorkoutCalendar(logs_filtered).formatmonth( year, month) context['logs'] = process_log_entries(logs)[0] context['current_year'] = year context['current_month'] = month context['current_workout'] = current_workout context['owner_user'] = user context['is_owner'] = is_owner context['impressions'] = WorkoutSession.IMPRESSION context['month_list'] = WorkoutLog.objects.filter(user=user).dates( 'date', 'month') context['show_shariff'] = is_owner and user.userprofile.ro_access return render(request, 'workout/calendar.html', context)
def view(request, pk): ''' Show the workout with the given ID ''' template_data = {} workout = get_object_or_404(Workout, pk=pk) user = workout.user is_owner = request.user == user if not is_owner and not user.userprofile.ro_access: return HttpResponseForbidden() canonical = workout.canonical_representation uid, token = make_token(user) # Create the backgrounds that show what muscles the workout will work on muscles_front = [] muscles_back = [] for i in canonical['muscles']['front']: if i not in muscles_front: muscles_front.append( 'images/muscles/main/muscle-{0}.svg'.format(i)) for i in canonical['muscles']['back']: if i not in muscles_back: muscles_back.append('images/muscles/main/muscle-{0}.svg'.format(i)) for i in canonical['muscles']['frontsecondary']: if i not in muscles_front and i not in canonical['muscles']['front']: muscles_front.append( 'images/muscles/secondary/muscle-{0}.svg'.format(i)) for i in canonical['muscles']['backsecondary']: if i not in muscles_back and i not in canonical['muscles']['back']: muscles_back.append( 'images/muscles/secondary/muscle-{0}.svg'.format(i)) # Append the silhouette of the human body as the last entry so the browser # renders it in the background muscles_front.append('images/muscles/muscular_system_front.svg') muscles_back.append('images/muscles/muscular_system_back.svg') template_data['workout'] = workout template_data['muscle_backgrounds_front'] = muscles_front template_data['muscle_backgrounds_back'] = muscles_back template_data['uid'] = uid template_data['token'] = token template_data['is_owner'] = is_owner template_data['owner_user'] = user template_data['show_shariff'] = is_owner return render(request, 'workout/view.html', template_data)
def view(request, id): """ Show the nutrition plan with the given ID """ template_data = {} plan = get_object_or_404(NutritionPlan, pk=id) user = plan.user is_owner = request.user == user if not is_owner and not user.userprofile.ro_access: return HttpResponseForbidden() uid, token = make_token(user) # Process and show the last 5 diary entries log_data = [] planned_calories = plan.get_nutritional_values()['total']['energy'] for item in plan.get_log_overview()[:5]: log_data.append({ 'date': item['date'], 'planned_calories': planned_calories, 'logged_calories': item['energy'], 'difference': item['energy'] - planned_calories }) # Load the language and pass it to the template language = load_language() template_data['language'] = language template_data['MEALITEM_WEIGHT_GRAM'] = MEALITEM_WEIGHT_GRAM template_data['MEALITEM_WEIGHT_UNIT'] = MEALITEM_WEIGHT_UNIT # Get the nutritional info template_data['plan'] = plan template_data['nutritional_data'] = \ plan.get_nutritional_values() # Get the weight entry used template_data['weight_entry'] = plan.get_closest_weight_entry() # Tokens for the links template_data['uid'] = uid template_data['log_data'] = log_data template_data['token'] = token template_data['owner_user'] = user template_data['is_owner'] = is_owner template_data['show_shariff'] = is_owner return render(request, 'plan/view.html', template_data)
def export_pdf_token(self): """ Helper function to test exporting a workout as a pdf using tokens """ user = User.objects.get(username="******") uid, token = make_token(user) response = self.client.get(reverse("manager:workout:pdf-log", kwargs={"id": 3, "uidb64": uid, "token": token})) self.assertEqual(response.status_code, 200) self.assertEqual(response["Content-Type"], "application/pdf") self.assertEqual(response["Content-Disposition"], "attachment; filename=Workout-3-log.pdf") # Approximate size only self.assertGreater(int(response["Content-Length"]), 29000) self.assertLess(int(response["Content-Length"]), 35000)
def view(request, pk): ''' Show the workout with the given ID ''' template_data = {} workout = get_object_or_404(Workout, pk=pk) user = workout.user is_owner = request.user == user if not is_owner and not user.userprofile.ro_access: return HttpResponseForbidden() canonical = workout.canonical_representation uid, token = make_token(user) # Create the backgrounds that show what muscles the workout will work on muscles_front = [] muscles_back = [] for i in canonical['muscles']['front']: if i not in muscles_front: muscles_front.append('images/muscles/main/muscle-{0}.svg'.format(i)) for i in canonical['muscles']['back']: if i not in muscles_back: muscles_back.append('images/muscles/main/muscle-{0}.svg'.format(i)) for i in canonical['muscles']['frontsecondary']: if i not in muscles_front and i not in canonical['muscles']['front']: muscles_front.append('images/muscles/secondary/muscle-{0}.svg'.format(i)) for i in canonical['muscles']['backsecondary']: if i not in muscles_back and i not in canonical['muscles']['back']: muscles_back.append('images/muscles/secondary/muscle-{0}.svg'.format(i)) # Append the silhouette of the human body as the last entry so the browser # renders it in the background muscles_front.append('images/muscles/muscular_system_front.svg') muscles_back.append('images/muscles/muscular_system_back.svg') template_data['workout'] = workout template_data['muscle_backgrounds_front'] = muscles_front template_data['muscle_backgrounds_back'] = muscles_back template_data['uid'] = uid template_data['token'] = token template_data['is_owner'] = is_owner template_data['owner_user'] = user template_data['show_shariff'] = is_owner return render(request, 'workout/view.html', template_data)
def calendar(request, username=None, year=None, month=None): ''' Show a calendar with all the workout logs ''' is_owner, user = check_access(request.user, username) uid, token = make_token(user) year = int(year) if year else datetime.date.today().year month = int(month) if month else datetime.date.today().month context = {} logs = WorkoutLog.objects.filter(user=user, date__year=year, date__month=month).order_by('exercise') logs_filtered = cache.get(cache_mapper.get_workout_log(user.pk, year, month)) if not logs_filtered: logs_filtered = {} # Process the logs. Group by date and check for impressions for log in logs: if log.date not in logs_filtered: session = log.get_workout_session() if session: impression = session.impression else: # Default is 'neutral' impression = WorkoutSession.IMPRESSION_NEUTRAL logs_filtered[log.date.day] = {'impression': impression, 'log': log} cache.set(cache_mapper.get_workout_log(user.pk, year, month), logs_filtered) (current_workout, schedule) = Schedule.objects.get_current_workout(user) context['calendar'] = WorkoutCalendar(logs_filtered).formatmonth(year, month) context['logs'] = process_log_entries(logs)[0] context['current_year'] = year context['current_month'] = month context['current_workout'] = current_workout context['owner_user'] = user context['is_owner'] = is_owner context['impressions'] = WorkoutSession.IMPRESSION context['month_list'] = WorkoutLog.objects.filter(user=user).dates('date', 'month') context['show_shariff'] = is_owner and user.userprofile.ro_access return render(request, 'workout/calendar.html', context)
def test_export_json_token(self): ''' Function to test exporting a workout as a json using tokens ''' user = User.objects.get(username='******') uid, token = make_token(user) response = self.client.get( reverse('manager:workout:json-data', kwargs={ 'id': 3, 'uidb64': uid, 'token': token })) self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-Type'], 'application/json') self.assertEqual(response['Content-Disposition'], 'attachment; filename=Workout-3.json')
def export_pdf_token(self): ''' Helper function to test exporting a workout as a pdf using tokens ''' user = User.objects.get(username='******') uid, token = make_token(user) response = self.client.get(reverse('manager:workout:pdf-log', kwargs={'id': 3, 'uidb64': uid, 'token': token})) self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-Type'], 'application/pdf') self.assertEqual(response['Content-Disposition'], 'attachment; filename=Workout-3-log.pdf') # Approximate size only self.assertGreater(int(response['Content-Length']), 29000) self.assertLess(int(response['Content-Length']), 35000)
def export_ical_token(self): ''' Helper function that checks exporing an ical file using tokens for access ''' user = User.objects.get(username='******') uid, token = make_token(user) response = self.client.get(reverse('workout-ical', kwargs={'pk': 3, 'uidb64': uid, 'token': token})) self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-Type'], 'text/calendar') self.assertEqual(response['Content-Disposition'], 'attachment; filename=Calendar-workout-3.ics') # Approximate size self.assertGreater(len(response.content), 540) self.assertLess(len(response.content), 560)
def export_pdf_token(self): ''' Helper function to test exporting a workout as a pdf using tokens ''' user = User.objects.get(username='******') uid, token = make_token(user) response = self.client.get(reverse('manager:schedule:pdf', kwargs={'pk': 1, 'uidb64': uid, 'token': token})) self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-Type'], 'application/pdf') self.assertEqual(response['Content-Disposition'], 'attachment; filename=Schedule-1-log.pdf') # Approximate size only self.assertGreater(int(response['Content-Length']), 31000) self.assertLess(int(response['Content-Length']), 35000)
def view(request, pk): ''' Show the workout schedule ''' template_data = {} user = request.user uid, token = make_token(user) schedule = get_object_or_404(Schedule, pk=pk, user=user) template_data['schedule'] = schedule if schedule.is_active: template_data['active_workout'] = schedule.get_current_scheduled_workout() else: template_data['active_workout'] = False schedule.get_current_scheduled_workout() template_data['uid'] = uid template_data['token'] = token return render(request, 'schedule/view.html', template_data)
def export_pdf_token(self): ''' Helper function to test exporting a nutritional plan as a pdf using a token as access (no fails) ''' user = User.objects.get(pk=2) uid, token = make_token(user) response = self.client.get(reverse('nutrition:plan:export-pdf', kwargs={'id': 4, 'uidb64': uid, 'token': token})) 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 self.assertGreater(int(response['Content-Length']), 29000) self.assertLess(int(response['Content-Length']), 34000)
def view(request, pk): """ Show the workout with the given ID """ template_data = {} workout = get_object_or_404(Workout, pk=pk) user = workout.user is_owner = request.user == user if not is_owner and not user.userprofile.ro_access: return HttpResponseForbidden() canonical = workout.canonical_representation uid, token = make_token(user) # Create the backgrounds that show what muscles the workout will work on muscles_front = [] muscles_back = [] for i in canonical["muscles"]["front"]: if i not in muscles_front: muscles_front.append("images/muscles/main/muscle-{0}.svg".format(i)) for i in canonical["muscles"]["back"]: if i not in muscles_back: muscles_back.append("images/muscles/main/muscle-{0}.svg".format(i)) # Append the silhouette of the human body as the last entry so the browser # renders it in the background muscles_front.append("images/muscles/muscular_system_front.svg") muscles_back.append("images/muscles/muscular_system_back.svg") template_data["workout"] = workout template_data["muscle_backgrounds_front"] = muscles_front template_data["muscle_backgrounds_back"] = muscles_back template_data["uid"] = uid template_data["token"] = token template_data["is_owner"] = is_owner template_data["owner_user"] = user template_data["show_shariff"] = is_owner return render(request, "workout/view.html", template_data)
def view(request, pk): """ Show the workout with the given ID """ template_data = {} workout = get_object_or_404(Workout, pk=pk) user = workout.user is_owner = request.user == user if not is_owner and not user.userprofile.ro_access: return HttpResponseForbidden() uid, token = make_token(user) template_data['workout'] = workout template_data['uid'] = uid template_data['token'] = token template_data['is_owner'] = is_owner template_data['owner_user'] = user template_data['show_shariff'] = is_owner return render(request, 'workout/view.html', template_data)
def export_ical_token(self): ''' Helper function that checks exporing an ical file using tokens for access ''' user = User.objects.get(username='******') uid, token = make_token(user) response = self.client.get( reverse( 'manager:workout:ical', kwargs={'pk': 3, 'uidb64': uid, 'token': token})) self.assertEqual(response.status_code, 200) self.assertEqual(response['Content-Type'], 'text/calendar') self.assertEqual(response['Content-Disposition'], 'attachment; filename=Calendar-workout-3.ics') # Approximate size self.assertGreater(len(response.content), 540) self.assertLess(len(response.content), 560)
def export_pdf_token(self): ''' Helper function to test exporting a nutritional plan as a pdf using a token as access (no fails) ''' user = User.objects.get(pk=2) uid, token = make_token(user) response = self.client.get( reverse( 'nutrition:plan:export-pdf', kwargs={'id': 4, 'uidb64': uid, 'token': token})) 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 self.assertGreater(int(response['Content-Length']), 29000) self.assertLess(int(response['Content-Length']), 34000)