from flask import Flask, g, render_template, request
from app.commands import create_db, drop_db, populate_db, recreate_db
from app.database import db
from app.extensions import (
    lm, api, travis, mail, bcrypt, celery, babel
)
from app.assets import assets
import app.utils as utils
from app import config
from app.user import user
from app.auth import auth
import time


def create_app(config=config.base_config):
    app = Flask(__name__)
    app.config.from_object(config)

    register_extensions(app)
    register_blueprints(app)
    register_errorhandlers(app)
    register_jinja_env(app)
    register_commands(app)

    def get_locale():
        return request.accept_languages.best_match(config.SUPPORTED_LOCALES)

    if babel.locale_selector_func is None:
        babel.locale_selector_func = get_locale

    @app.before_request
    def before_request():
        g.request_start_time = time.time()
        g.request_time = lambda: '%.5fs' % (time.time() - g.request_start_time)
        g.pjax = 'X-PJAX' in request.headers

    @app.route('/', methods=['GET'])
    def index():
        return render_template('index.html')

    return app


def register_commands(app):
    """Registers custom commands for the Flask CLI."""
    app.cli.command()(create_db)
    app.cli.command()(drop_db)
    app.cli.command()(populate_db)
    app.cli.command()(recreate_db)


def register_extensions(app):
    travis.init_app(app)
    db.init_app(app)
    api.init_app(app)
    lm.init_app(app)
    mail.init_app(app)
    bcrypt.init_app(app)
    celery.config_from_object(app.config)
    assets.init_app(app)
    babel.init_app(app)


def register_blueprints(app):
    app.register_blueprint(user, url_prefix='/user')
    app.register_blueprint(auth)


def register_errorhandlers(app):
    def render_error(e):
        return render_template('errors/%s.html' % e.code), e.code

    for e in [401, 404, 500]:
        app.errorhandler(e)(render_error)


def register_jinja_env(app):
    app.jinja_env.globals['url_for_other_page'] = utils.url_for_other_page
    app.jinja_env.globals['timeago'] = utils.timeago

@@ -1,12 +1,25 @@
+import time%0A%0A
 from flask i
@@ -59,16 +59,112 @@
 request%0A
+import requests%0A%0Afrom app import config%0Afrom app.assets import assets%0Afrom app.auth import auth%0A
 from app
@@ -284,14 +284,8 @@
 ort 
-(%0A    
 lm, 
@@ -325,18 +325,16 @@
 , babel%0A
-)%0A
 from app
@@ -330,38 +330,36 @@
 el%0Afrom app.
-assets
+user
  import 
 assets%0Aimpor
@@ -346,22 +346,20 @@
  import 
-assets
+user
 %0Aimport 
@@ -381,95 +381,8 @@
 ils%0A
-from app import config%0Afrom app.user import user%0Afrom app.auth import auth%0Aimport time%0A
 %0A%0Ade
@@ -1784,24 +1784,87 @@
 dlers(app):%0A
+    %22%22%22Register error handlers with the Flask application.%22%22%22%0A%0A
     def rend
@@ -1961,21 +1961,130 @@
 in %5B
-401, 404, 500
+%0A        requests.codes.INTERNAL_SERVER_ERROR,%0A        requests.codes.NOT_FOUND,%0A        requests.codes.UNAUTHORIZED,%0A    
 %5D:%0A 

Clean up error handler