<commit_before>#!/usr/bin/env python

####################
# Imports
####################

from flask import *
from flask_wtf import *
from flask_cors import *

app = application = Flask(__name__)

app.config['WTF_CSRF_CHECK_DEFAULT'] = False
csrf = CsrfProtect()
csrf.init_app(app)

import os
import json
import md5
import datetime
import random
import string
import calendar
import pytz

import ConfigParser
config = ConfigParser.ConfigParser()
config.read(os.path.join(app.root_path, "stackptr.conf"))
app.secret_key = config.get("app","secret_key")
app.invite_code = config.get("app","invite_code", None)


import logging, sys
logging.basicConfig(stream=sys.stderr)

import crossbarconnect

from werkzeug.security import *
from flask.ext.login import login_user, logout_user, login_required, current_user, LoginManager
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.script import Manager
from flask.ext.migrate import Migrate, MigrateCommand

import stackptr_core

####################
# Config
####################

app.config['SQLALCHEMY_DATABASE_URI'] = config.get("database","uri")

from models import *
db = SQLAlchemy(app)

migrate = Migrate(app, Base)

manager = Manager(app)
manager.add_command('db', MigrateCommand)

####################
# Login
####################

login_manager = LoginManager(app)
login_manager.login_view = 'login'

@login_manager.user_loader
def load_user(id):
	csrf.protect()
	return db.session.query(Users).get(int(id))

@login_manager.request_loader
def load_user_from_request(request):
	apikey = None
	if request.method == "POST":
		apikey = request.form.get('apikey')
	else:
		apikey = request.args.get('apikey')
	if apikey:
		key = db.session.query(ApiKey).filter_by(key=apikey).first()
		if key == None:
			return None
		return db.session.query(Users).filter_by(id=key.userid).first()
	return None

@app.before_request
def before_request():
	g.user = current_user

####################
# Index
####################


@app.route('/')
@login_required
def index():
	return render_template("map.html", current_user=g.user.username)

# registration

@app.route('/registration', methods=['GET','POST'])
def registration():
	if app.invite_code is None:
		return "invites disabled"
	if request.method == "GET":<commit_msg>Return 401 to requests authed by API key instead of returning a redirect to /login<commit_after>#!/usr/bin/env python

####################
# Imports
####################

from flask import *
from flask_wtf import *
from flask_cors import *

app = application = Flask(__name__)

app.config['WTF_CSRF_CHECK_DEFAULT'] = False
csrf = CsrfProtect()
csrf.init_app(app)

import os
import json
import md5
import datetime
import random
import string
import calendar
import pytz

import ConfigParser
config = ConfigParser.ConfigParser()
config.read(os.path.join(app.root_path, "stackptr.conf"))
app.secret_key = config.get("app","secret_key")
app.invite_code = config.get("app","invite_code", None)


import logging, sys
logging.basicConfig(stream=sys.stderr)

import crossbarconnect

from werkzeug.security import *
from flask.ext.login import login_user, logout_user, login_required, current_user, LoginManager
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.script import Manager
from flask.ext.migrate import Migrate, MigrateCommand

import stackptr_core

####################
# Config
####################

app.config['SQLALCHEMY_DATABASE_URI'] = config.get("database","uri")

from models import *
db = SQLAlchemy(app)

migrate = Migrate(app, Base)

manager = Manager(app)
manager.add_command('db', MigrateCommand)

####################
# Login
####################

login_manager = LoginManager(app)
login_manager.login_view = 'login'

@login_manager.user_loader
def load_user(id):
	csrf.protect()
	return db.session.query(Users).get(int(id))

@login_manager.request_loader
def load_user_from_request(request):
	apikey = None
	if request.method == "POST":
		apikey = request.form.get('apikey')
	else:
		apikey = request.args.get('apikey')
	if apikey:
		key = db.session.query(ApiKey).filter_by(key=apikey).first()
		if key == None:
			return None
		return db.session.query(Users).filter_by(id=key.userid).first()
	return None

@login_manager.unauthorized_handler
def unauthorized():
	if request.form.get('apikey') or request.args.get('apikey'):
		abort(401)
	else:
		redirect(login_manager.login_view)

@app.before_request
def before_request():
	g.user = current_user

####################
# Index
####################


@app.route('/')
@login_required
def index():
	return render_template("map.html", current_user=g.user.username)

# registration

@app.route('/registration', methods=['GET','POST'])
def registration():
	if app.invite_code is None:
		return "invites disabled"
	if request.method == "GET":
{'fraction_non_alphanumeric': 0.17917624921597325, 'fraction_numerical': 0.00167259042441982, 'mean_word_length': 6.25948406676783, 'pattern_counts': {'":': 4, '<': 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': '1814832', 'n_tokens_mistral': 1598, 'n_tokens_neox': 1540, 'n_words': 341}