示例#1
0
    def __init__(self, server, cfg_manager):
        core_server_url  = cfg_manager.get_value( 'core_server_url', '' )
        self.script_name = urlparse.urlparse(core_server_url).path.split('/weblab')[0] or ''

        self.app = Flask('weblab.core.wl')
        self.app.config['SECRET_KEY'] = os.urandom(32)
        self.app.config['APPLICATION_ROOT'] = self.script_name
        self.app.config['SESSION_COOKIE_PATH'] = self.script_name + '/weblab/'
        self.app.config['SESSION_COOKIE_NAME'] = 'weblabsession'

        # Initialize internationalization code.
        initialize_i18n(self.app)

        # Mostly for debugging purposes, this snippet will print the site-map so that we can check
        # which methods we are routing.
        @self.app.route("/site-map")
        def site_map():
            lines = []
            for rule in self.app.url_map.iter_rules():
                line = str(escape(repr(rule)))
                lines.append(line)

            ret = "<br>".join(lines)
            return ret


        flask_debug = cfg_manager.get_value('flask_debug', False)
        core_facade_port = cfg_manager.get_value(configuration_doc.CORE_FACADE_PORT, 'unknown')
        if flask_debug and not is_testing():
            print("*" * 50, file=sys.stderr)
            print("WARNING " * 5, file=sys.stderr)
            print("flask_debug is set to True. This is an important security leak. Do not use it in production, only for bugfixing!!!", file=sys.stderr)
            print("If you want to see the debug toolbar in Flask pages, also use http://localhost:{0}/weblab/".format(core_facade_port), file=sys.stderr)
            print("WARNING " * 5, file=sys.stderr)
            print("*" * 50, file=sys.stderr)
        self.app.config['DEBUG'] = flask_debug
        if os.path.exists('logs'):
            f = os.path.join('logs','admin_app.log')
        else:
            f = 'admin_app.log'
        file_handler = RotatingFileHandler(f, maxBytes = 50 * 1024 * 1024)
        file_handler.setLevel(logging.WARNING)
        self.app.logger.addHandler(file_handler)

        super(WebLabFlaskServer, self).__init__(cfg_manager, self.app)

        json_api = Blueprint('json', __name__)
        weblab_api.apply_routes_api(json_api, server)
        self.app.register_blueprint(json_api, url_prefix = '/weblab/json')
        self.app.register_blueprint(json_api, url_prefix = '/weblab/login/json')
 
        authn_web = Blueprint('login_web', __name__)
        weblab_api.apply_routes_login_web(authn_web, server)
        self.app.register_blueprint(authn_web, url_prefix = '/weblab/login/web')

        static_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), 'static'))
        core_web = Blueprint('core_web', __name__, static_folder=static_folder)
        weblab_api.apply_routes_web(core_web, server)
        self.app.register_blueprint(core_web, url_prefix = '/weblab/web')

        # Register the blueprint for the new (year 2015) flask-based web client.
        # The .apply_routes_webclient method is dynamically generated, the name matches
        # that in the wl.py module.
        # Attempt at setting the right static folder.
        core_webclient = Blueprint('core_webclient', __name__, static_folder=static_folder)
        weblab_api.apply_routes_webclient(core_webclient, server)
        self.app.register_blueprint(core_webclient, url_prefix = '/weblab/web/webclient')

        @self.app.context_processor
        def inject_weblab_api():
            return dict(weblab_api=weblab_api)

        self.admin_app = AdministrationApplication(self.app, cfg_manager, server)

        if flask_debug:
            from flask_debugtoolbar import DebugToolbarExtension
            toolbar = DebugToolbarExtension()
            toolbar.init_app(self.app)
            self.app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
            self.app.config['DEBUG_TB_PROFILER_ENABLED'] = True
示例#2
0
    def __init__(self, server, cfg_manager):
        core_server_url = cfg_manager.get_value("core_server_url", "")
        self.script_name = urlparse.urlparse(core_server_url).path.split("/weblab")[0] or ""

        self.app = Flask("weblab.core.wl")
        self.app.config["SECRET_KEY"] = os.urandom(32)
        self.app.config["APPLICATION_ROOT"] = self.script_name
        self.app.config["SESSION_COOKIE_PATH"] = self.script_name + "/weblab/"
        self.app.config["SESSION_COOKIE_NAME"] = "weblabsession"

        # Initialize internationalization code.
        if Babel is None:
            print "Not using Babel. Everything will be in English"
        else:
            babel = Babel(self.app)

            supported_languages = ["en"]
            supported_languages.extend([translation.language for translation in babel.list_translations()])

            @babel.localeselector
            def get_locale():
                locale = request.args.get("locale", None)
                if locale is None:
                    locale = request.accept_languages.best_match(supported_languages)
                if locale is None:
                    locale = "en"
                return locale

        # Mostly for debugging purposes, this snippet will print the site-map so that we can check
        # which methods we are routing.
        @self.app.route("/site-map")
        def site_map():
            lines = []
            for rule in self.app.url_map.iter_rules():
                line = str(escape(repr(rule)))
                lines.append(line)

            ret = "<br>".join(lines)
            return ret

        flask_debug = cfg_manager.get_value("flask_debug", False)
        if flask_debug:
            print >> sys.stderr, "*" * 50
            print >> sys.stderr, "WARNING " * 5
            print >> sys.stderr, "flask_debug is set to True. This is an important security leak. Do not use it in production, only for bugfixing!!!"
            print >> sys.stderr, "WARNING " * 5
            print >> sys.stderr, "*" * 50
        self.app.config["DEBUG"] = flask_debug
        if os.path.exists("logs"):
            f = os.path.join("logs", "admin_app.log")
        else:
            f = "admin_app.log"
        file_handler = RotatingFileHandler(f, maxBytes=50 * 1024 * 1024)
        file_handler.setLevel(logging.WARNING)
        self.app.logger.addHandler(file_handler)

        super(WebLabFlaskServer, self).__init__(cfg_manager, self.app)

        json_api = Blueprint("json", __name__)
        weblab_api.apply_routes_api(json_api, server)
        self.app.register_blueprint(json_api, url_prefix="/weblab/json")
        self.app.register_blueprint(json_api, url_prefix="/weblab/login/json")

        authn_web = Blueprint("login_web", __name__)
        weblab_api.apply_routes_login_web(authn_web, server)
        self.app.register_blueprint(authn_web, url_prefix="/weblab/login/web")

        core_web = Blueprint("core_web", __name__)
        weblab_api.apply_routes_web(core_web, server)
        self.app.register_blueprint(core_web, url_prefix="/weblab/web")

        # Register the blueprint for the new (year 2015) flask-based web client.
        # The .apply_routes_webclient method is dynamically generated, the name matches
        # that in the wl.py module.
        # Attempt at setting the right static folder.
        static_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), "static"))
        core_webclient = Blueprint("core_webclient", __name__, static_folder=static_folder)
        weblab_api.apply_routes_webclient(core_webclient, server)
        self.app.register_blueprint(core_webclient, url_prefix="/weblab/web/webclient")

        self.admin_app = AdministrationApplication(self.app, cfg_manager, server)
示例#3
0
    def __init__(self, server, cfg_manager):
        core_server_url  = cfg_manager.get_value( 'core_server_url', '' )
        self.script_name = urlparse.urlparse(core_server_url).path.split('/weblab')[0] or ''

        self.app = Flask('weblab.core.wl')
        self.app.wsgi_app = ProxyFix(self.app.wsgi_app)
        self.app.config['SECRET_KEY'] = os.urandom(32)
        self.app.config['APPLICATION_ROOT'] = self.script_name
        self.app.config['SESSION_COOKIE_PATH'] = self.script_name + '/weblab/'
        self.app.config['SESSION_COOKIE_NAME'] = 'weblabsession'

        # Initialize internationalization code.
        self.babel = initialize_i18n(self.app)

        # Mostly for debugging purposes, this snippet will print the site-map so that we can check
        # which methods we are routing.
        @self.app.route("/site-map")
        def site_map():
            lines = []
            for rule in self.app.url_map.iter_rules():
                line = str(escape(repr(rule)))
                lines.append(line)

            ret = "<br>".join(lines)
            return ret


        flask_debug = cfg_manager.get_value('flask_debug', False)
        core_facade_port = cfg_manager.get_value(configuration_doc.CORE_FACADE_PORT, 'unknown')
        if flask_debug and not is_testing():
            print("*" * 50, file=sys.stderr)
            print("WARNING " * 5, file=sys.stderr)
            print("flask_debug is set to True. This is an important security leak. Do not use it in production, only for bugfixing!!!", file=sys.stderr)
            print("If you want to see the debug toolbar in Flask pages, also use http://localhost:{0}/weblab/".format(core_facade_port), file=sys.stderr)
            print("WARNING " * 5, file=sys.stderr)
            print("*" * 50, file=sys.stderr)
        self.app.config['DEBUG'] = flask_debug
        if os.path.exists('logs'):
            f = os.path.join('logs','admin_app.log')
        else:
            f = 'admin_app.log'
        file_handler = RotatingFileHandler(f, maxBytes = 50 * 1024 * 1024)
        file_handler.setLevel(logging.WARNING)
        self.app.logger.addHandler(file_handler)

        super(WebLabFlaskServer, self).__init__(cfg_manager, self.app)

        json_api = Blueprint('json', __name__)
        weblab_api.apply_routes_api(json_api, server)
        self.app.register_blueprint(json_api, url_prefix = '/weblab/json')
        self.app.register_blueprint(json_api, url_prefix = '/weblab/login/json')
 
        authn_web = Blueprint('login_web', __name__)
        weblab_api.apply_routes_login_web(authn_web, server)
        self.app.register_blueprint(authn_web, url_prefix = '/weblab/login/web')

        static_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), 'static'))
        core_web = Blueprint('core_web', __name__, static_folder=static_folder)
        weblab_api.apply_routes_web(core_web, server)
        self.app.register_blueprint(core_web, url_prefix = '/weblab/web')

        # Register the blueprint for the new (year 2015) flask-based web client.
        # The .apply_routes_webclient method is dynamically generated, the name matches
        # that in the wl.py module.
        # Attempt at setting the right static folder.
        core_webclient = Blueprint('core_webclient', __name__, static_folder=static_folder)
        weblab_api.apply_routes_webclient(core_webclient, server)
        self.app.register_blueprint(core_webclient, url_prefix = '/weblab')

        @self.app.context_processor
        def inject_weblab_api():
            return dict(weblab_api=weblab_api, display_date=display_date, get_locale=get_locale, wl_config=cfg_manager)

        self.admin_app = AdministrationApplication(self.app, cfg_manager, server)

        if flask_debug:
            from flask_debugtoolbar import DebugToolbarExtension
            toolbar = DebugToolbarExtension()
            toolbar.init_app(self.app)
            self.app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False