<commit_before># -*- coding: utf-8 -*-

from functools import wraps
import urlparse
from urllib2 import urlopen

from flask import (g, request, session, flash, redirect, url_for, render_template,
    Markup, escape, json)

from lastuserapp import app
from lastuserapp.models import db, User
from lastuserapp.forms import ConfirmDeleteForm

def avatar_url_email(useremail):
    if request.url.startswith('https:'):
        return 'https://secure.gravatar.com/avatar/%s?s=48&d=mm' % useremail.md5sum
    else:
        return 'http://www.gravatar.com/avatar/%s?s=48&d=mm' % useremail.md5sum


def avatar_url_twitter(twitterid):
    if twitterid:
        return urlopen('http://api.twitter.com/1/users/profile_image/%s' % twitterid).geturl()


@app.before_request
def lookup_current_user():
    """
    If there's a userid in the session, retrieve the user object and add
    to the request namespace object g.
    """
    g.user = None
    if 'userid' in session:
        g.user = User.query.filter_by(userid=session['userid']).first()
        if not 'avatar_url' in session:
            if g.user.email:
                session['avatar_url'] = avatar_url_email(g.user.email)
            elif session.get('userid_external', {}).get('service') == 'twitter':
                session['avatar_url'] = avatar_url_twitter(session['userid_external'].get('username'))
            else:
                session['avatar_url'] = None
        g.avatar_url = session['avatar_url']
    else:
        session.pop('avatar_url', None)
        g.avatar_url = None


def requires_login(f):
    """
    Decorator to require a login for the given view.
    """
    @wraps(f)
    def decorated_function(*args, **kwargs):
        if g.user is None:
            flash(u"You need to be logged in for that page")
            return redirect(url_for('login', next=request.url))
        return f(*args, **kwargs)
    return decorated_function


def get_next_url(referrer=False, external=False):
    """
    Get the next URL to redirect to. Don't return external URLs unless
    explicitly asked for. This is to protect the site from being an unwitting
    redirector to external URLs.
    """
    next_url = request.args.get('next', '')
    if not external:
        if next_url.startswith('http:') or next_url.startswith('https:') or next_url.startswith('//'):
            # Do the domains match?
            if urlparse.urlsplit(next_url).hostname != urlparse.urlsplit(request.url).hostname:
                next_url = ''
    if referrer:
        return next_url or request.referrer or url_for('index')
    else:
        return next_url or url_for('index')


def login_internal(user):
    g.user = user
    session['userid'] = user.userid


def logout_internal():
    g.user = None
    session.pop('userid', None)
    session.pop('userid_external', None)
    session.permanent = False

<commit_msg>Fix for Google OpenID.<commit_after># -*- coding: utf-8 -*-

from functools import wraps
import urlparse
from urllib2 import urlopen

from flask import (g, request, session, flash, redirect, url_for, render_template,
    Markup, escape, json)

from lastuserapp import app
from lastuserapp.models import db, User
from lastuserapp.forms import ConfirmDeleteForm

def avatar_url_email(useremail):
    if request.url.startswith('https:'):
        return 'https://secure.gravatar.com/avatar/%s?s=48&d=mm' % useremail.md5sum
    else:
        return 'http://www.gravatar.com/avatar/%s?s=48&d=mm' % useremail.md5sum


def avatar_url_twitter(twitterid):
    if twitterid:
        return urlopen('http://api.twitter.com/1/users/profile_image/%s' % twitterid).geturl()


@app.before_request
def lookup_current_user():
    """
    If there's a userid in the session, retrieve the user object and add
    to the request namespace object g.
    """
    g.user = None
    if 'userid' in session:
        g.user = User.query.filter_by(userid=session['userid']).first()
        if not 'avatar_url' in session:
            if g.user.email:
                session['avatar_url'] = avatar_url_email(g.user.email)
            elif session.get('userid_external', {}).get('service') == 'twitter':
                session['avatar_url'] = avatar_url_twitter(session['userid_external'].get('username'))
            else:
                session['avatar_url'] = None
        g.avatar_url = session['avatar_url']
    else:
        session.pop('avatar_url', None)
        g.avatar_url = None


def requires_login(f):
    """
    Decorator to require a login for the given view.
    """
    @wraps(f)
    def decorated_function(*args, **kwargs):
        if g.user is None:
            flash(u"You need to be logged in for that page")
            session['next'] = request.url
            return redirect(url_for('login'))
        return f(*args, **kwargs)
    return decorated_function


def get_next_url(referrer=False, external=False):
    """
    Get the next URL to redirect to. Don't return external URLs unless
    explicitly asked for. This is to protect the site from being an unwitting
    redirector to external URLs.
    """
    next_url = session.pop('next', None)
    if next_url:
        return next_url
    next_url = request.args.get('next', '')
    if not external:
        if next_url.startswith('http:') or next_url.startswith('https:') or next_url.startswith('//'):
            # Do the domains match?
            if urlparse.urlsplit(next_url).hostname != urlparse.urlsplit(request.url).hostname:
                next_url = ''
    if referrer:
        return next_url or request.referrer or url_for('index')
    else:
        return next_url or url_for('index')


def login_internal(user):
    g.user = user
    session['userid'] = user.userid


def logout_internal():
    g.user = None
    session.pop('userid', None)
    session.pop('userid_external', None)
    session.permanent = False


{'fraction_non_alphanumeric': 0.11861220304221501, 'fraction_numerical': 0.0030763971970603314, 'mean_word_length': 2.9013333333333335, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '7032387', 'n_tokens_mistral': 1842, 'n_tokens_neox': 1816, 'n_words': 467}