Exemple #1
0
def register(request):
    """
    DataHub account registration form.

    GET returns an HttpResponse containing the account registration form.
    POST creates a name/email/password account and logs the new user in.
    Other links from the page lead to Python Social Auth options (Google,
    Facebook, Twitter, etc).
    """
    # Redirect succesful logins to `next` if set.
    # Failing that `redirect_url`.
    # Failing that, LOGIN_REDIRECT_URL from settings.py.
    redirect_uri = get_or_post(
        request, 'next', fallback=get_or_post(
            request, 'redirect_url', fallback=settings.LOGIN_REDIRECT_URL))
    redirect_absolute_uri = add_query_params_to_url(
        request.build_absolute_uri(redirect_uri),
        {'auth_user': request.user.get_username()})

    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username'].lower()
            email = form.cleaned_data['email'].lower()
            password = form.cleaned_data['password']
            User.objects.create_user(username, email, password)
            # A signal handler in signals.py listens for the pre_save signal
            # and throws an IntegrityError if the user's email address is not
            # unique. Username uniqueness is handled by the model.
            #
            # In the future, another pre_save signal handler will check if a
            # DataHub database exists for the user and create one if it
            # doesn't exist. If the database cannot be created, that handler
            # will throw an exception.
            user = datahub_authenticate(username, password)
            if user is not None and user.is_active:
                django_login(request, user)
                # Append auth_user to redirect_uri so apps like Kibitz can
                # pull the username out of the redirect. This should be
                # removed when Thrift is removed from DataHub.
                redirect_uri = add_query_params_to_url(
                    redirect_uri, {'auth_user': request.user.get_username()})
                return HttpResponseRedirect(redirect_uri)
        else:
            # Form isn't valid. Fall through and return it to the user with
            # errors.
            pass
    else:
        form = RegistrationForm()

    providers = provider_details()
    context = RequestContext(request, {
        'request': request,
        'user': request.user,
        'form': form,
        'providers': providers,
        'next': redirect_uri,
        'absolute_next': redirect_absolute_uri,
        })
    return render_to_response('register.html', context_instance=context)
Exemple #2
0
def login(request):
    """
    DataHub account login form.

    GET returns and HttpResponse containing the account login form.
    POST logs in name/email/password accounts.
    Other links from the page lead to Python Social Auth options (Google,
    Facebook, Twitter, etc).
    """
    # Redirect succesful logins to `next` if set.
    # Failing that `redirect_url`.
    # Failing that, LOGIN_REDIRECT_URL from settings.py.
    redirect_uri = get_or_post(
        request, 'next', fallback=get_or_post(
            request, 'redirect_url', fallback=settings.LOGIN_REDIRECT_URL))
    redirect_absolute_uri = add_query_params_to_url(
        request.build_absolute_uri(redirect_uri),
        {'auth_user': request.user.get_username()})

    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username'].lower()
            password = form.cleaned_data['password']
            user = datahub_authenticate(username, password)
            if user is not None and user.is_active:
                django_login(request, user)
                # Append auth_user to redirect_uri so apps like Kibitz can
                # pull the username out of the redirect. This should be
                # removed when Thrift is removed from DataHub.
                redirect_uri = add_query_params_to_url(
                    redirect_uri, {'auth_user': request.user.get_username()})
                return HttpResponseRedirect(redirect_uri)
            else:
                form.add_error(None, "Username and password do not match.")
        else:
            # Form isn't valid. Fall through to return it to the user with
            # errors.
            pass
    else:
        form = LoginForm()

    providers = provider_details()
    context = RequestContext(request, {
        'request': request,
        'user': request.user,
        'form': form,
        'providers': providers,
        'next': redirect_uri,
        'absolute_next': redirect_absolute_uri,
        })
    return render_to_response('login.html', context_instance=context)