- username = request.POST.get('username', None)
- password = request.POST.get('password', None)
- service = request.POST.get('service', None)
- lt = request.POST.get('lt', None)
-
- try:
- login_ticket = LoginTicket.objects.get(ticket=lt)
- except:
- errors.append('Login ticket expired. Please try again.')
- else:
- login_ticket.delete()
- user = authenticate(username=username, password=password)
- if user is not None:
- if user.is_active:
- auth_login(request, user)
- if service is not None:
- ticket = create_service_ticket(user, service)
- return HttpResponseRedirect(service + '?ticket=' + ticket.ticket)
- else:
- return HttpResponseRedirect(success_redirect)
- else:
- errors.append('This account is disabled.')
- else:
- errors.append('Incorrect username and/or password.')
- form = LoginForm(service)
- return render_to_response(template_name, {'form': form, 'errors': errors}, context_instance=RequestContext(request))
-
+ form = LoginForm(request.POST)
+ if form.is_valid():
+ user = form.get_user()
+ auth_login(request, user)
+ service = form.cleaned_data.get('service')
+ if service is not None:
+ ticket = ServiceTicket.objects.create(service=service, user=user)
+ success_redirect = ticket.get_redirect_url()
+ return HttpResponseRedirect(success_redirect)
+ else:
+ form = LoginForm(initial={
+ 'service': service,
+ 'lt': LoginTicket.objects.create()
+ })
+ return render_to_response(template_name, {
+ 'form': form,
+ 'errors': form.get_errors()
+ }, context_instance=RequestContext(request))
+
+