def main(config_path, address_port): logger = logging.getLogger('app') try: app = get_app(config_path).get_app_dispatcher() if is_test() or app.config['debug']['serve_static']: cwd = os.getcwd() os.chdir(get_root_path()) if not os.path.exists(os.path.join(get_root_path(), 'sessions')): os.mkdir(os.path.join(get_root_path(), 'sessions')) os.chdir(cwd) if not is_test(): address_port = address_port.rsplit(':', 1) address = '0.0.0.0' if len(address_port) == 1 else address_port[0] port = address_port[0] if len( address_port) == 1 else address_port[1] werkzeug.serving.run_simple( address, int(port), app, use_debugger=app.config['debug'] != None) else: return app except Exception as e: logger.error('Exception encountered when starting the application', exc_info=True) raise e
def main(config_file): logger = logging.getLogger('app') try: app = get_app(config_file) if is_test() or app.config['debug']['serve_static']: os.chdir(get_root_path()) if not os.path.exists(os.path.join(get_root_path(), 'sessions')): os.mkdir(os.path.join(get_root_path(), 'sessions')) if not is_test(): app.run() else: return app except Exception as e: logger.error('Exception encountered when starting the application', exc_info=True) raise e
def authentication_processor(handler): def post_auth(): if 'user' in app.session: User.get(id=app.session['user']['id']) app.session.pop('sidebar', None) return handler() class_path, _ = match(app.mapping, web.ctx.path) if class_path: if '.' in class_path: mod, cls = class_path.rsplit('.', 1) mod = __import__(mod, None, None, ['']) cls = getattr(mod, cls) else: cls = app.fvars[class_path] if issubclass(cls, ICTVAuthPage): if is_test(): if hasattr(app, 'test_user'): u = User.selectBy( email=app.test_user['email']).getOne() app.session['user'] = u.to_dictionary( ['id', 'fullname', 'username', 'email']) return post_auth() elif 'user' in app.session: del app.session[ 'user'] # This may not be the ideal way of changing users if app.config['debug']['autologin']: u = User.selectBy(email='admin@ictv').getOne(None) if u is not None: app.session['user'] = u.to_dictionary( ['id', 'fullname', 'username', 'email']) return post_auth() mode = app.config['authentication'][ 0] # The first mode is the main authentication mode if mode not in mode_to_processor: raise Exception( 'Authentication method "%s" specified in config file is not supported' % app.config['authentication']) return mode_to_processor[mode](post_auth) return post_auth()
def authentication_processor(): #Avoid processing for static files if request_static(): return def post_auth(): if 'user' in app.session: User.get(id=app.session['user']['id']) app.session.pop('sidebar', None) endpoint, _ = app.create_url_adapter(flask.request).match() view_class = app.view_functions.get(endpoint).view_class if issubclass(view_class, ICTVAuthPage): if is_test(): if hasattr(app, 'test_user'): u = User.selectBy(email=app.test_user['email']).getOne() app.session['user'] = u.to_dictionary( ['id', 'fullname', 'username', 'email']) return post_auth() elif 'user' in app.session: del app.session[ 'user'] # This may not be the ideal way of changing users if app.config['debug']['autologin']: u = User.selectBy(email='admin@ictv').getOne(None) if u is not None: app.session['user'] = u.to_dictionary( ['id', 'fullname', 'username', 'email']) return post_auth() mode = app.config['authentication'][ 0] # The first mode is the main authentication mode if mode not in mode_to_processor: raise Exception( 'Authentication method "%s" specified in config file is not supported' % app.config['authentication']) return mode_to_processor[mode](post_auth) post_auth()
flask.g.homedomain = '%s://%s' % (flask.request.scheme, flask.request.host) flask.g.query = ("?" if flask.request.query_string.decode() != "" else "") + flask.request.query_string.decode() flask.g.home = flask.request.url_root flask.g.homepath = flask.g.home.split(flask.g.homedomain + "/")[-1] return web_ctx_processor def dump_log_stats(): from ictv.common.logging import loggers_stats LogStat.dump_log_stats(loggers_stats) if not is_test( ): # TODO: Find why this ignores the first SIGINT when placed in ictv-webapp from ictv.libs.register_exit_fun import register_exit_fun @register_exit_fun def exit_fun(): if database.database_path is not None: sqlhub.threadConnection = SQLObjectThreadConnection.get_conn() sqlhub.doInTransaction(dump_log_stats) close_database() os._exit(0) def get_config(config_path): with open(config_path) as f: config = yaml.unsafe_load(f) with open(
from ictv.models.asset import Asset from ictv.models.building import Building from ictv.models.channel import Channel, PluginChannel, ChannelBundle from ictv.models.ictv_object import DBVersion from ictv.models.log_stat import LogStat from ictv.models.plugin import Plugin from ictv.models.plugin_param_access_rights import PluginParamAccessRights from ictv.models.role import Role from ictv.models.screen import Screen, ScreenMac from ictv.models.subscription import Subscription from ictv.models.template import Template from ictv.models.user import User from ictv.common.utils import is_test database_version = 0 if is_test(): database_path = 'sqlite://' + tempfile.mkstemp()[1] else: database_path = None class SQLObjectThreadConnection(object): _local = threading.local() @classmethod def get_conn(cls): if 'conn' not in cls._local.__dict__: cls._local.conn = create_connection() return cls._local.conn