from totalimpact import default_settings
import os, logging, sys
import analytics
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_debugtoolbar import DebugToolbarExtension

# see http://wiki.pylonshq.com/display/pylonscookbook/Alternative+logging+configuration
logging.basicConfig(
    stream=sys.stdout,
    level=logging.DEBUG,
    format='[%(process)d] %(levelname)8s %(threadName)30s %(name)s - %(message)s'
)

logger = logging.getLogger("ti")


app = Flask(__name__)
app.config.from_object(default_settings)

app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv("POSTGRESQL_URL")
app.config["SQLALCHEMY_POOL_SIZE"] = 50
# app.config["SQLALCHEMY_ECHO"] = True
# app.config["SQLALCHEMY_RECORD_QUERIES"] = True

db = SQLAlchemy(app)

app.config["SECRET_KEY"] = os.getenv("SECRET_KEY")
# set up Flask-DebugToolbar
if (os.getenv("FLASK_DEBUG", False) == "True"):
    logger.info("Setting app.debug=True; Flask-DebugToolbar will display")
    app.debug = True
    app.config["DEBUG_TB_INTERCEPT_REDIRECTS"] = False
toolbar = DebugToolbarExtension(app)


# segment.io logging
analytics.init(os.getenv("SEGMENTIO_PYTHON_KEY"))
analytics.identify("CORE", {
				       'internal': True,
				       'name': 'IMPACTSTORY CORE'})

from totalimpact import views

@@ -75,16 +75,38 @@
 alytics%0A
+import sqlalchemy.exc%0A
 from fla
@@ -1262,16 +1262,204 @@
 ORE'%7D)%0A%0A
+try:%0A%09from totalimpact import views%0Aexcept sqlalchemy.exc.ProgrammingError:%0A%09logger.info(%22SQLAlchemy database tables not found, so creating them%22)%0A%09db.session.rollback()%0A%09db.create_all()%0A%09
 from tot

create database tables if they aren't there