import os

basedir = os.path.abspath(os.path.dirname(__file__))


class Config:
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'hard to guess string'
    MAIL_SERVER = 'mail.gandi.net'
    MAIL_PORT = 587
    MAIL_USE_TLS = True
    MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
    MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
    MAIL_SUBJECT_PREFIX = '[braindump]'
    MAIL_SENDER = 'braindump <noreply@braindump.pw>'
    APP_ADMIN = os.environ.get('BRAINDUMP_ADMIN')
    SQLALCHEMY_TRACK_MODIFICATIONS = False

    @staticmethod
    def init_app(app):
        pass


class DevelopmentConfig(Config):
    DEBUG = True
    SQLALCHEMY_DATABASE_URI = 'postgresql://{0}:{1}@{2}/{3}'.format(
        'braindump',
        'braindump',
        'localhost',
        'braindump')


class TestingConfig(Config):
    DEBUG = True
    TESTING = True
    WTF_CSRF_ENABLED = False
    SQLALCHEMY_DATABASE_URI = 'postgresql://{0}@{1}/{2}'.format(
        'ubuntu',
        'localhost',
        'circle_test')


class ProductionConfig(Config):
    SQLALCHEMY_DATABASE_URI = 'postgresql://{0}:{1}@{2}/{3}'.format(
        os.environ.get('DB_USER'),
        os.environ.get('DB_PASS'),
        os.environ.get('DB_HOST'),
        os.environ.get('DB_NAME'))

    @classmethod
    def init_app(cls, app):
        Config.init_app(app)

        # email errors to the administrators
        import logging
        from logging.handlers import SMTPHandler
        credentials = None
        secure = None
        if getattr(cls, 'MAIL_USERNAME', None) is not None:
            credentials = (cls.MAIL_USERNAME, cls.MAIL_PASSWORD)
            if getattr(cls, 'MAIL_USE_TLS', None):
                secure = ()
        mail_handler = SMTPHandler(
            mailhost=(cls.MAIL_SERVER, cls.MAIL_PORT),
            fromaddr=cls.MAIL_SENDER,
            toaddrs=[cls.APP_ADMIN],
            subject=cls.MAIL_SUBJECT_PREFIX + ' Application Error',
            credentials=credentials,
            secure=secure)
        mail_handler.setLevel(logging.ERROR)
        app.logger.addHandler(mail_handler)


class UnixConfig(ProductionConfig):
    @classmethod
    def init_app(cls, app):
        ProductionConfig.init_app(app)

        # log to syslog
        import logging
        from logging.handlers import SysLogHandler
        syslog_handler = SysLogHandler()
        syslog_handler.setLevel(logging.WARNING)
        app.logger.addHandler(syslog_handler)

config = {
    'development': DevelopmentConfig,
    'testing': TestingConfig,
    'production': ProductionConfig,
    'unix': UnixConfig,

    'default': DevelopmentConfig
}

@@ -771,24 +771,923 @@
 raindump')%0A%0A
+    @classmethod%0A    def init_app(cls, app):%0A        Config.init_app(app)%0A%0A        with app.app_context():%0A            from app import db%0A            from app.models import User, Notebook%0A%0A            db.init_app(app)%0A            db.create_all()%0A%0A            # Check if User Already Created%0A            u = User.query.filter_by(email='admin@example.com').first()%0A            if u:%0A                pass%0A            else:%0A                # Create Admin User%0A                u = User(%0A                    email='admin@example.com', password='password',%0A                    confirmed=True)%0A                db.session.add(u)%0A                db.session.commit()%0A%0A                # Create Default Notebook for Admin User%0A                nb = Notebook(%0A                    title='Default Notebook',%0A                    author_id=u.id)%0A                db.session.add(nb)%0A                db.session.commit()%0A%0A
 %0Aclass Testi

create admin user in dev mode