我们从Python开源项目中,提取了以下49个代码示例,用于说明如何使用flask.current_app()。
def test_greenlet_context_copying(self): app = flask.Flask(__name__) greenlets = [] @app.route('/') def index(): reqctx = flask._request_ctx_stack.top.copy() def g(): self.assert_false(flask.request) self.assert_false(flask.current_app) with reqctx: self.assert_true(flask.request) self.assert_equal(flask.current_app, app) self.assert_equal(flask.request.path, '/') self.assert_equal(flask.request.args['foo'], 'bar') self.assert_false(flask.request) return 42 greenlets.append(greenlet(g)) return 'Hello World!' rv = app.test_client().get('/?foo=bar') self.assert_equal(rv.data, b'Hello World!') result = greenlets[0].run() self.assert_equal(result, 42)
def test_greenlet_context_copying_api(self): app = flask.Flask(__name__) greenlets = [] @app.route('/') def index(): reqctx = flask._request_ctx_stack.top.copy() @flask.copy_current_request_context def g(): self.assert_true(flask.request) self.assert_equal(flask.current_app, app) self.assert_equal(flask.request.path, '/') self.assert_equal(flask.request.args['foo'], 'bar') return 42 greenlets.append(greenlet(g)) return 'Hello World!' rv = app.test_client().get('/?foo=bar') self.assert_equal(rv.data, b'Hello World!') result = greenlets[0].run() self.assert_equal(result, 42) # Disable test if we don't have greenlets available
def create_restmin_app(app_name, config_path, base_url, request_processor): from flask import Flask app = Flask(app_name) app.config.from_object(config_path) @app.route(base_url + '<string:resource>', methods = ['GET', 'POST', 'PUT']) @crossdomain(origin='*') def _api_generic(resource): return api_generic(request, request_processor, current_app, resource) @app.route(base_url + '<string:resource>', methods = ['OPTIONS']) @crossdomain(origin='*', headers='Content-Type, X-User, X-Token') def _options(self): return jsonify({'Allow' : 'GET,POST,PUT' }), 200 return app
def _handler_router(self, section, name): """ Main Router for Atlassian Connect plugin TODO: Rest of params """ method = self.sections.get(section, {}).get(name) if method is None: (self.app or current_app).logger.error( 'Invalid handler for %s -- %s' % (section, name)) print((section, name, self.sections)) abort(404) ret = method() if ret is not None: return ret return '', 204
def __init__(self): super(CallbackModule, self).__init__() if not flask.current_app: ctx = app.app_context() ctx.push() self.taskresult = None self.task = None self.play = None self.playbook = None self.stats = None self.loop_items = [] self.play_counter = itertools.count() self.task_counter = itertools.count() if cli: self._options = cli.options else: self._options = None
def setUp(self): app.testing = True app.config.from_object('config_local.Testing') app.config["MAX_DAYS_KEEP_UNFILTERED_RECORDS"] = 5 app.config["PURGE_FILTERED_RECORDS"] = { "severity": { 1: 1, 4: 0 }, "classification": { "test/keep/one": 0, "test/discard/*": 1, } } app.debug = False self.app_context = app.app_context() self.app_context.push() db.init_app(current_app) db.create_all() self.client = app.test_client()
def get_app(self, reference_app=None): """Helper method that implements the logic to look up an application.""" if reference_app is not None: return reference_app if current_app: return current_app if self.app is not None: return self.app raise RuntimeError( 'application not registered on db instance and no application' 'bound to current context' )
def __init__(self, Blueprint=flask.Blueprint, current_app=flask.current_app, redirect=flask.redirect, render_template=flask.render_template, request=flask.request, Response=flask.Response, session=flask.session, url_for=flask.url_for, datastore=datastore, datetime=datetime.datetime, json=json): # Dependency injection self.Blueprint = Blueprint self.current_app = current_app self.redirect = redirect self.render_template = render_template self.request = request self.Response = Response self.session = session self.url_for = url_for self.datastore = datastore self.datetime = datetime self.json = json self.name = 'appmodule' self.import_name = __name__ self._routes = None