Example #1
0
def add(request):
	if request.method == 'POST':
		form = NewWorkoutForm(request.POST)
		if form.is_valid():
			workout=form.save()
			workout.time_of_workout = workout.time_of_workout.strftime("%Y-%m-%d") + timezone.now().strftime(" %H:%M:%S+00:00")		
			workout.save()
			for meas in workout.exercise.measure.all():
				Score(workout=workout, measure=meas, result=0).save()
			messages.success(request, 'Your workout has been added, please update results.')
			return HttpResponseRedirect(reverse('getfit.views.workout', args=[workout.id,]))
	else:
		form = NewWorkoutForm()		
	return render(request, 'add.html', {'form': form, 'type': "New", 'action': "Add", 'add_active': "active", 'jspath': '/django/contrib/admin/media/'})
Example #2
0
def edit(request, workout_id):
	try:
		workout = Workout.objects.get(pk = workout_id)
	except:
		workout = None
	if workout:
		if request.method == 'POST':
			form = NewWorkoutForm(request.POST)
			if form.is_valid():
				workout.exercise = form.cleaned_data['exercise']
				workout.time_of_workout = form.cleaned_data['time_of_workout']
				workout.time_of_workout = workout.time_of_workout.strftime("%Y-%m-%d") + timezone.now().strftime(" %H:%M:%S+00:00")		
				workout.save()
				messages.success(request, 'Your workout has been updated.')
				return HttpResponseRedirect(reverse('getfit.views.workout', args=[workout.id,]))
		else:
			eastern=pytz.timezone('US/Eastern')
			form = NewWorkoutForm({'exercise': workout.exercise.id, 'time_of_workout': workout.time_of_workout.astimezone(eastern).strftime('%Y-%m-%d')})
			return render(request, 'add.html', {'form': form, 'type': "Edit", 'action': "Update"})
	else:
		messages.error(request, u"That workout doesn't exist.")
		return HttpResponseRedirect(reverse('getfit.views.home'))