<commit_before>from google.appengine.api import users

import functools

import flask
import flaskext.login
import flaskext.oauth

import util
import config
import model
from main import app


################################################################################
# Flaskext Login
################################################################################
login_manager = flaskext.login.LoginManager()


class AnonymousUser(flaskext.login.AnonymousUser):
  id = 0
  admin = False
  name = 'Anonymous'

  def key(self):
    return None

login_manager.anonymous_user = AnonymousUser


class FlaskUser(AnonymousUser):
  def __init__(self, user_db):
    self.user_db = user_db
    self.id = user_db.key.id()
    self.name = user_db.name
    self.admin = user_db.admin

  def key(self):
    return self.user_db.key.urlsafe()

  def get_id(self):
    return self.user_db.key.urlsafe()

  def is_authenticated(self):
    return True

  def is_active(self):
    return self.user_db.active

  def is_anonymous(self):
    return False


@login_manager.user_loader
def load_user(key):
  user_db = model.User.retrieve_by_key_safe(key)
  if user_db:
    return FlaskUser(user_db)
  return None


login_manager.setup_app(app)


def current_user_id():
  return flaskext.login.current_user.id


def current_user_key():
  return flaskext.login.current_user.user_db.key


def current_user_db():
  return current_user_key().get()


def is_logged_in():
  return current_user_id() != 0


def login_required(f):
  @functools.wraps(f)
  def decorated_function(*args, **kws):
    if is_logged_in():
      return f(*args, **kws)
    else:
      return flask.redirect(flask.url_for('login', next=flask.request.url))
  return decorated_function


################################################################################
# Login stuff
################################################################################
@app.route('/login/')
def login():
  next_url = util.get_next_url()
  if flask.url_for('login') in next_url:
    next_url = flask.url_for('welcome')

  google_login_url = flask.url_for('login_google', next=next_url)
  twitter_login_url = flask.url_for('login_twitter', next=next_url)
  facebook_login_url = flask.url_for('login_facebook', next=next_url)

  return flask.render_template(
      'login.html',
      title='Login',
      html_class='login',
      google_login_url=google_login_url,
      twitter_login_url=twitter_login_url,
      facebook_login_url=facebook_login_url,
      next_url=next_url,
    )
<commit_msg>Added admin_required decorator in the auth.py<commit_after>from google.appengine.api import users

import functools

import flask
import flaskext.login
import flaskext.oauth

import util
import config
import model
from main import app


################################################################################
# Flaskext Login
################################################################################
login_manager = flaskext.login.LoginManager()


class AnonymousUser(flaskext.login.AnonymousUser):
  id = 0
  admin = False
  name = 'Anonymous'

  def key(self):
    return None

login_manager.anonymous_user = AnonymousUser


class FlaskUser(AnonymousUser):
  def __init__(self, user_db):
    self.user_db = user_db
    self.id = user_db.key.id()
    self.name = user_db.name
    self.admin = user_db.admin

  def key(self):
    return self.user_db.key.urlsafe()

  def get_id(self):
    return self.user_db.key.urlsafe()

  def is_authenticated(self):
    return True

  def is_active(self):
    return self.user_db.active

  def is_anonymous(self):
    return False


@login_manager.user_loader
def load_user(key):
  user_db = model.User.retrieve_by_key_safe(key)
  if user_db:
    return FlaskUser(user_db)
  return None


login_manager.setup_app(app)


def current_user_id():
  return flaskext.login.current_user.id


def current_user_key():
  return flaskext.login.current_user.user_db.key


def current_user_db():
  return current_user_key().get()


def is_logged_in():
  return current_user_id() != 0


def login_required(f):
  @functools.wraps(f)
  def decorated_function(*args, **kws):
    if is_logged_in():
      return f(*args, **kws)
    else:
      return flask.redirect(flask.url_for('login', next=flask.request.url))
  return decorated_function


def admin_required(f):
  @functools.wraps(f)
  def decorated_function(*args, **kws):
    if is_logged_in() and current_user_db().admin:
      return f(*args, **kws)
    else:
      flask.abort(401)
  return decorated_function


################################################################################
# Login stuff
################################################################################
@app.route('/login/')
def login():
  next_url = util.get_next_url()
  if flask.url_for('login') in next_url:
    next_url = flask.url_for('welcome')

  google_login_url = flask.url_for('login_google', next=next_url)
  twitter_login_url = flask.url_for('login_twitter', next=next_url)
  facebook_login_url = flask.url_for('login_facebook', next=next_url)

  return flask.render_template(
      'login.html',
      title='Login',
      html_class='login',
      google_login_url=google_login_url,
      twitter_login_url=twitter_login_url,
      facebook_login_url=facebook_login_url,
      next_url=next_url,
    )

{'fraction_non_alphanumeric': 0.21233133433283358, 'fraction_numerical': 0.0013118440779610195, 'mean_word_length': 4.884233737596472, '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': '3653173', 'n_tokens_mistral': 1775, 'n_tokens_neox': 1685, 'n_words': 322}