<commit_before>from pyramid.httpexceptions import HTTPFound

from openid.consumer import consumer
from openid.extensions import ax
from openid.store.filestore import FileOpenIDStore

from yithlibraryserver.compat import urlparse

def _get_consumer(request):
    settings = request.registry.settings
    store = FileOpenIDStore(settings['google_openid_store_path'])
    return consumer.Consumer(request.session, store)


AX_ATTRS = {
    'firstname': 'http://axschema.org/namePerson/first',
    'lastname': 'http://axschema.org/namePerson/last',
    'email': 'http://axschema.org/contact/email',
}


def google_login(request):
    settings = request.registry.settings
    openid_url = settings['google_openid_url']

    consumer = _get_consumer(request)
    auth_req = consumer.begin(openid_url)

    fetch_req = ax.FetchRequest()
    fetch_req.add(ax.AttrInfo(AX_ATTRS['firstname'], required=True))
    fetch_req.add(ax.AttrInfo(AX_ATTRS['lastname'], required=True))
    fetch_req.add(ax.AttrInfo(AX_ATTRS['email'], required=True))

    auth_req.addExtension(fetch_req)

    return_to = request.route_url('google_callback')
    parts = urlparse.urlparse(return_to)
    realm = '%s://%s/' % (parts.scheme, parts.netloc)

    return HTTPFound(location=auth_req.redirectURL(realm, return_to=return_to))


def google_callback(request):
    con = _get_consumer(request)
    info = con.complete(request.GET, request.url)
    if info.status == consumer.SUCCESS:
        fr = ax.FetchResponse.fromSuccessResponse(info)
        print fr.getSingle(AX_ATTRS['firstname'])
        print fr.getSingle(AX_ATTRS['lastname'])
        print fr.getSingle(AX_ATTRS['email'])
        return info.getDisplayIdentifier()
    elif info.status == consumer.CANCEL:
        return 'canceled'
    elif info.status == consumer.FAILURE:
        return 'failure'
    elif info.status == consumer.SETUP_NEEDED:
        return 'setup needed'
    else:<commit_msg>Register or login the user with Google attributes<commit_after>from pyramid.httpexceptions import HTTPFound
from pyramid.security import remember

from openid.consumer import consumer
from openid.extensions import ax
from openid.store.filestore import FileOpenIDStore

from yithlibraryserver.compat import urlparse
from yithlibraryserver.user.utils import update_user

def _get_consumer(request):
    settings = request.registry.settings
    store = FileOpenIDStore(settings['google_openid_store_path'])
    return consumer.Consumer(request.session, store)


AX_ATTRS = {
    'first_name': 'http://axschema.org/namePerson/first',
    'last_name': 'http://axschema.org/namePerson/last',
    'email': 'http://axschema.org/contact/email',
}


def google_login(request):
    settings = request.registry.settings
    openid_url = settings['google_openid_url']

    consumer = _get_consumer(request)
    auth_req = consumer.begin(openid_url)

    fetch_req = ax.FetchRequest()
    fetch_req.add(ax.AttrInfo(AX_ATTRS['first_name'], required=True))
    fetch_req.add(ax.AttrInfo(AX_ATTRS['last_name'], required=True))
    fetch_req.add(ax.AttrInfo(AX_ATTRS['email'], required=True))

    auth_req.addExtension(fetch_req)

    return_to = request.route_url('google_callback')
    parts = urlparse.urlparse(return_to)
    realm = '%s://%s/' % (parts.scheme, parts.netloc)

    if 'next_url' in request.params:
        request.session['next_url'] = request.params['next_url']

    return HTTPFound(location=auth_req.redirectURL(realm, return_to=return_to))


def google_callback(request):
    con = _get_consumer(request)
    info = con.complete(request.GET, request.url)
    if info.status == consumer.SUCCESS:
        fr = ax.FetchResponse.fromSuccessResponse(info)
        user_id = info.getDisplayIdentifier()

        if 'next_url' in request.session:
            next_url = request.session['next_url']
            del request.session['next_url']
        else:
            next_url = request.route_path('home')

        user = request.db.users.find_one({'google_id': user_id})
        info = {
                'provider': 'google',
                'google_id': user_id,
                'first_name': fr.getSingle(AX_ATTRS['first_name']),
                'last_name': fr.getSingle(AX_ATTRS['last_name']),
                'email': fr.getSingle(AX_ATTRS['email']),
            }
        if user is None:
            request.session['user_info'] = info
            request.session['next_url'] = next_url
            return HTTPFound(location=request.route_path('register_new_user'))
        else:
            update_user(request.db, user, info)
            remember_headers = remember(request, str(user['_id']))
            return HTTPFound(location=next_url, headers=remember_headers)

    elif info.status == consumer.CANCEL:
        return 'canceled'
    elif info.status == consumer.FAILURE:
        return 'failure'
    elif info.status == consumer.SETUP_NEEDED:
        return 'setup needed'
    else:
{'fraction_non_alphanumeric': 0.11708603896103896, 'fraction_numerical': 0.0, 'mean_word_length': 3.645617342130066, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6626134', 'n_tokens_mistral': 1527, 'n_tokens_neox': 1490, 'n_words': 265}