Ejemplo n.º 1
0
def login(request, template_name='registration/login.html', redirect_field_name=REDIRECT_FIELD_NAME):
    """
    Displays the login form and handles the login action.
    """
     # If the user is logged in, redirect to the home page
    user = request.user
    if user.is_authenticated():
        return HttpResponseRedirect("/")
       
    # Otherwise, we process the form
    redirect_to = request.REQUEST.get(redirect_field_name, '')
    if request.method == "POST":
        form = AuthenticationForm(data=request.POST)
        if form.is_valid():
            # Light security check -- make sure redirect_to isn't garbage.
            if not redirect_to or '//' in redirect_to or ' ' in redirect_to:
                redirect_to = settings.LOGIN_REDIRECT_URL
            from django.contrib.auth import login
            login(request, form.get_user())
            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()
            return HttpResponseRedirect(redirect_to)
    else:
        form = AuthenticationForm(request)
    request.session.set_test_cookie()
    return render_to_response(template_name, {
        'form': form,
        redirect_field_name: redirect_to,
    }, context_instance=RequestContext(request))
Ejemplo n.º 2
0
def ws_login(request):
    """
    Parses and responds to a webservices login request.
    """
    from swarm.utils.wsu import return_json_response
    timer_start = datetime.now()
    context = {
        "action": "%s" % reverse("auth_ws_login"),
        "result": { "success": 0, "message": "Invalid username or password" }
    }
    user = request.user
    try:
        if user.is_authenticated():
            # Let the system know the person is already authenticated.
            context['result']['success'] = 1
            context['result']['message'] = "Already authenticated"
        else:            
            if request.method == "POST":
                form = AuthenticationForm(data=request.POST)
                if form.is_valid():
                    from django.contrib.auth import login
                    login(request, form.get_user())
                    if request.session.test_cookie_worked():
                        request.session.delete_test_cookie()
                    context['result']['success'] = 1
                    context['result']['message'] = "Logged in. Redirecting."
                else:
                    # TODO: process errors into json response
                    print form._errors
    except:
        # Admins need to know personally if there's an error happening
        subject = "Authentication Error"
        import sys, traceback
        import cStringIO
        from django.core.mail import mail_admins
        exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
        message = "%s\n\n%s" % (request.raw_post_data, traceback.format_exc(),)
        mail_admins(subject=subject,message=message)

    timer_end = datetime.now()
    context['delta'] = timer_end - timer_start

    return return_json_response(context)