django-publicauth / publicauth / views.py

from django.conf import settings as global_settings
from django.utils.translation import ugettext as _
from django.shortcuts import redirect
from django.contrib import messages
from django.http import Http404
from django.contrib import auth 

from annoying.decorators import render_to

from publicauth.utils import str_to_class, get_backend
from publicauth import settings


def logout(request):
    auth.logout(request)
    messages.add_message(request, messages.SUCCESS, publicauth.lang.SUCCESS_LOGOUT)
    return redirect("/")


def begin(request, provider):
    """
    Display authentication form. This is also the first step
    in registration. The actual login is in social_complete 
    function below.
    """
    # merge data from POST and GET methods
    data = request.GET.copy()
    data.update(request.POST)

    request.session['next_url'] = request.GET.get("next") or global_settings.LOGIN_REDIRECT_URL
        
    # start the authentication process 
    backend = get_backend(provider)
    backend.begin(request, data)

    return {}


def complete(request, provider):
    """
    Complete PublicID authorization process.
    If PublicID was successfuly authenticated:
     * if no user with such ID exists and current user is authenticated then
       assign PublicID to this user.
     * if user with such ID exists and current user is anonimouse then login 
       as this user.
     * if no user with such ID exists and user must fill extra fields, 
       redirect him to registration form.
    """
    backend = get_backend(provider)
    response = backend.validate_response(request)
    if request.user.is_authenticated():
        backend.login_user(request)
        backend.attach_account(request, request.user)
    else:
        backend.login_user(request)
        if not settings.REGISTRATION_ALLOWED:
            messages.add_message(request, messages.WARNING, lang.REGISTRATION_DISABLED)
            return redirect(settings.REGISTRATION_DISABLED_REDIRECT)
    backend.complete(request, response)


@render_to('publicauth/extra.html')
def extra(request, provider):
    """                                                
    Handle registration of new user with extra data for profile
    """                                                          
    try:
        identity = request.session['identity']
    except KeyError:
        raise Http404

    if request.method == "POST":
        form = str_to_class(settings.PUBLICID_EXTRA_FORM)(request.POST)
        if form.is_valid():
            user = form.save(identity, provider)
            del request.session['identity']
            user = auth.authenticate(identity=identity, provider=provider)
            if user:
                auth.login(request, user)
                next_url = request.session['next_url']
                del request.session['next_url']
                return redirect(next_url)
    else:
        initial = request.session['extra']
        form = str_to_class(settings.PUBLICID_EXTRA_FORM)(initial=initial)

    return {'form': form}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.