示例#1
0
class OptionsConfig(Config):
    """Load options from a common config file
    """
    def __init__(self, conf_file):
        """Update Flask default data from janitoo option file
        """
        Config.__init__(self)
        if not os.path.isfile(conf_file):
            raise RuntimeError("Can't find %s" %conf_file )
        self.CONF_FILE = conf_file
        self.options = JNTOptions({"conf_file":conf_file})
        self.options.load()
        if 'hostname' not in self.options.data or self.options.data['hostname'] is None:
            self.options.data['hostname'] = socket.gethostname()
        system = self.options.data
        webapp = self.options.get_options('webapp')
        database = self.options.get_options('database')
        #~ try:
            #~ flask = self.options.get_options('flask')
        #~ except ConfigParser.NoSectionError:
            #~ flask = {}
        self.SQLALCHEMY_DATABASE_URI = database['sqlalchemy.url']
        if 'host' in webapp and 'port' in webapp:
            self.SERVER_NAME = "%s:%s"%(webapp['host'], webapp['port'])
        if 'cache_dir' in system:
            self.CACHE_DIR = os.path.join(system['cache_dir'], 'flask_cache_store')
示例#2
0
 def __init__(self, url=u'sqlite:////tmp/janitoo_db.sqlite', pkg_name='janitoo_db', ep_name='janitoo', conf_file=None, **kwargs):
     """
     """
     self.pkg_name = pkg_name
     self.ep_name = ep_name
     if conf_file is None:
         src = os.path.join(pkg_resources.resource_filename(pkg_resources.Requirement.parse(self.pkg_name), 'config'), u'alembic_template.conf')
     else:
         src = conf_file
         options = JNTOptions({'conf_file':conf_file})
         options.load()
         alembic = options.get_options('database')
         url = alembic['sqlalchemy.url']
     file_ = os.path.join(tempfile.gettempdir(), u'jntal_%s.conf')%(''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(26)))
     shutil.copyfile(src, file_)
     alConfig.__init__(self, file_=file_, ini_section='database', **kwargs)
     config_path = pkg_resources.resource_filename(pkg_resources.Requirement.parse(self.pkg_name), 'config')
     self.set_main_option("script_location", os.path.join(config_path, 'alembic'))
     self.set_main_option("sqlalchemy.url", url)
     version_locations = u"%s/alembic/versions %s/models/%s"%(config_path, config_path, self.ep_name)
     for entrypoint in pkg_resources.iter_entry_points(group='janitoo.models'):
         pkg = entrypoint.module_name.split('.')[0]
         config_path = pkg_resources.resource_filename(pkg_resources.Requirement.parse(pkg), 'config')
         version_locations +=  u" %s/models/%s"%(config_path, entrypoint.name)
     self.set_main_option("version_locations", version_locations)
示例#3
0
 def test_052_dbserver_auto_migrate(self):
     options = JNTOptions({'conf_file':self.getDataFile(self.server_conf)})
     options.load()
     options.set_option('database','auto_migrate', True)
     self.start()
     self.assertHeartbeatNode()
     self.stop()
示例#4
0
 def test_001_engine(self):
     options = JNTOptions({'conf_file':'tests/data/janitoo_db.conf'})
     options.load()
     engine = create_db_engine(options)
     self.dbmaker = sessionmaker()
     # Bind the sessionmaker to engine
     self.dbmaker.configure(bind=engine)
     self.dbsession = scoped_session(self.dbmaker)
     Base.metadata.create_all(bind=engine)
示例#5
0
 def setUp(self):
     JNTTServer.setUp(self)
     options = JNTOptions({'conf_file':self.getDataFile(self.server_conf)})
     options.load()
     self.dbengine = create_db_engine(options)
     self.dbmaker = sessionmaker()
     # Bind the sessionmaker to engine
     self.dbmaker.configure(bind=self.dbengine)
     self.dbsession = scoped_session(self.dbmaker)
     Base.metadata.drop_all(bind=self.dbengine)
示例#6
0
 def test_051_dbserver_no_auto_migrate(self):
     options = JNTOptions({'conf_file':self.getDataFile(self.server_conf)})
     options.load()
     options.set_option('database','auto_migrate', False)
     try:
         with self.assertRaises(JanitooException):
             self.start()
             self.assertHeartbeatNode()
             self.stop()
     finally:
         options.set_option('database','auto_migrate', True)
示例#7
0
def configure_extensions(app, config):
    """Configures the extensions."""

    # Flask-WTF CSRF
    csrf.init_app(app)

    # Flask-SQLAlchemy
    db.init_app(app)

    # Flask-Migrate
    #~ migrate.init_app(app, db, directory="config", filename="janitoo_manager.conf", section="database")

    # Flask-Mail
    mail.init_app(app)

    # Flask-Debugtoolbar
    debugtoolbar.init_app(app)

    # Flask-Themes
    themes.init_themes(app, app_identifier="janitoo_manager")

    # Flask-Login
    login_manager.login_view = app.config["LOGIN_VIEW"]
    login_manager.refresh_view = app.config["REAUTH_VIEW"]
    login_manager.anonymous_user = GuestMan

    @login_manager.user_loader
    def load_user(user_id):
        """Loads the user. Required by the `login` extension."""

        #~ unread_count = db.session.query(db.func.count(Conversation.id)).\
            #~ filter(Conversation.unread,
                   #~ Conversation.user_id == user_id).subquery()
        #~ u = db.session.query(User, unread_count).filter(User.id == user_id).\
            #~ first()
        u = db.session.query(UserMan).filter(UserMan.id == user_id).\
            first()

        if u:
            user_instance = u
            #~ user_instance, user_instance.pm_unread = u
            return user_instance
        else:
            return None

    login_manager.init_app(app)

    # Flask-BabelEx
    babel.init_app(app=app, default_domain=JanitooDomain(app))

    @babel.localeselector
    def get_locale():
        # if a user is logged in, use the locale from the user settings
        if current_user.is_authenticated and current_user.language:
            return current_user.language
        # otherwise we will just fallback to the default language
        return flask_config["DEFAULT_LANGUAGE"]

    # SocketIO
    socketio.init_app(app)

    # janitoo_flask
    options = JNTOptions({"conf_file":config.CONF_FILE})
    options.load()
    janitoo.init_app(app, socketio, options=options.data, db=db)
    janitoo.extend_network('janitoo_manager')
    janitoo.extend_listener('janitoo_manager')