Exemplo n.º 1
0
def new_goal(request):
	"""
	Creates a new Goal object from either a name specified in the GET
	or the POST. If it's in the POST, we can take a few more details.
	"""
	
	forms = []
	
	# If no name was specified in the querystring, take the user
	# back to the homepage
	if request.method == 'GET' and not request.GET.get('name'):
		return HttpResponseRedirect('/')
	
	# Create a new Goal object and set it as the form instance, so we
	# can prepopulate the form
	goal = Goal(name = request.GET.get('name'))
	
	if request.method == 'POST':
		goal_form = GoalForm(request.POST, prefix = 'goal')
	else:
		goal_form = GoalForm(instance = goal, prefix = 'goal')
	
	# If the user is anonymous (as users can come to this page without
	# logging in), create a signup/login form and att that to the page
	if request.user.is_anonymous():
		signup_form = SignupForm(request.POST or None, prefix = 'signup')
		
		# Sign the user up or log him in, then remove the form from the
		# page if the form was submitted with no errors.
		if signup_form.is_valid():
			profile = signup_form.save()

			from django.contrib.auth import login
			login(request, profile.user)

			user = profile.user
		else:
			forms.append(signup_form)
			user = None
	else:
		user = request.user
	
	# If the user is logged in, and the goal details are correct,
	# create the new Goal object and attach it to the user. Message the
	# user then take them to the Start Plan page.
	
	if not user is None and goal_form.is_valid():
		goal = goal_form.save(commit = False)	
		goal.user = user
		goal.save()
		
		request.user.message_set.create(
			message = 'Your goal has been created.'
		)
		
		return HttpResponseRedirect(
			reverse('start_plan', args = [goal.slug])
		)
	
	# If we came to this page via a GET, or the POST has invalid data,
	# (re)display the form page
	forms.append(goal_form)
	
	return render_to_response(
		'start.html',
		{
			'forms': forms,
			'goal': goal,
			'meta_title': (goal.name,)
		},
		RequestContext(request)
	)
Exemplo n.º 2
0
def start_plan(request, goal):
	if request.user.is_anonymous():
		if request.method == 'GET':
			signup_form = SignupForm(prefix = 'signup')
			return render_to_response(
				'start.html',
				{
					'forms': [signup_form],
					'goal': goal
				},
				RequestContext(request)
			)
		else:
			signup_form = SignupForm(request.POST, prefix = 'signup')
			
			if signup_form.is_valid():
				profile = signup_form.save()
				profile.user.message_set.create(
					message = 'Your account has been created.'
				)
				
				from django.contrib.auth import login
				login(request, profile.user)
			else:
				return render_to_response(
					'start.html',
					{
						'forms': [signup_form],
						'goal': goal
					},
					RequestContext(request)
				)
	
	if goal.plans.filter(user = request.user, live = True).count() > 0:
		if request.method == 'POST' and request.POST.get('next'):
			return HttpResponseRedirect(
				request.POST.get('next')
			)
		else:
			return HttpResponseRedirect(
				reverse('edit_plan', args = [goal.slug])
			)
	
	plan = Plan(
		goal = goal,
		user = request.user
	)
	
	try:
		plan.original = goal.original_plan()	
	except Plan.DoesNotExist:
		pass
	
	from datetime import date, timedelta
	if plan.original and plan.original.deadline:
		plan.deadline = date.today() + timedelta(
			(
				plan.original.deadline - plan.original.started.date()
			).days
		)
	
	if request.method == 'GET':
		form = PlanForm(instance = plan)
	else:
		form = PlanForm(request.POST, instance = plan)
		if form.is_valid():
			plan = form.save()
			request.user.message_set.create(
				message = 'Your plan has been created.'
			)
			
			return HttpResponseRedirect(
				request.POST.get('next')
			)
	
	return render_to_response(
		'plan/edit.html',
		{
			'forms': [form],
			'goal': goal,
			'plan': plan,
			'next': reverse(
				'actions_edit',
				args = [plan.goal.slug]
			),
			'meta_title': (goal.name,)
		},
		RequestContext(request)
	)