def shell(): """Interactive stutuz console.""" from inspect import cleandoc from stutuz import db, models banner = '''\ Interactive stutuz console. Useful names: app Flask application for stutuz db The database instance models Namespace for all models ''' banner = cleandoc(banner) try: current_app.preprocess_request() context = dict(app=current_app, db=db, models=models) try: import bpython except ImportError: import code code.interact(banner, local=context) else: bpython.embed(context, ['-i'], banner) finally: current_app.process_response(current_app.response_class())
def __init__(self, *args, **kwargs): app_request = kwargs.get('request', None) if isinstance(app_request, tuple): app, request = app_request if request and app: self.ctx = app.request_context(request.environ) self.ctx.push() current_app.preprocess_request() del kwargs['request'] super(BaseNamespace, self).__init__(*args, **kwargs)
def decorated_func(*args, **kwargs): """This function has to run within application context.""" if not has_app_context() or new_context: with get_application().app_context(): current_app.preprocess_request() result = f(*args, **kwargs) else: result = f(*args, **kwargs) return result
def decorated_func(*args, **kwargs): """Force this function to run within the application context.""" if not has_app_context() or new_context: with get_application().test_request_context('/'): # FIXME we should use maybe app_context() current_app.preprocess_request() result = f(*args, **kwargs) else: result = f(*args, **kwargs) return result
def start_request(*args, **kwargs): """ Make a request context. http://flask.pocoo.org/docs/1.0/api/#flask.Flask.test_request_context """ ctx = current_app.test_request_context(*args, **kwargs) ctx.push() current_app.preprocess_request() return ctx
def batch(): """ Execute multiple requests, submitted as a batch. :statuscode 207: Multi status """ # TODO: we could probably turn off csrf protection for each requests # and only use the CSRF in the header of the parent request requests, err = RequestSchema().load(request.get_json(), many=True) responses = [] status_code = 207 for req in requests: method = req['method'] url = req['url'] body = req.get('body', None) headers = req.get('headers', {}) with current_app.app_context(): headers.setdefault('accept', 'application/json') with current_app.test_request_context(url, method=method, data=body, headers=headers): try: # Can modify flask.g here without affecting # flask.g of the root request for the batch # Pre process Request rv = current_app.preprocess_request() if rv is None: # Main Dispatch rv = current_app.dispatch_request() except Exception as e: rv = current_app.handle_user_exception(e) response = current_app.make_response(rv) # Post process Request response = current_app.process_response(response) # Response is a Flask response object. # _read_response(response) reads response.response # and returns a string. If your endpoints return JSON object, # this string would be the response as a JSON string. responses.append({ "status_code": response.status_code, "body": response.data.decode('utf-8'), "headers": [{'name': k, 'value': v} for k, v in response.headers.items()] }) # if error response if (response.status_code % 400 < 100) or (response.status_code % 400 < 100): status_code = response.status_code break return json.dumps(responses), status_code
def batch(): """ Execute multiple requests, submitted as a batch. :status code 207: Multi status :response body: Individual request status code Batch Request data Example: [ { "method": "PATCH", "path": "/party-api/v1/respondents/email", "body": respondent_email_3, "headers": <headers> }, { "method": "PATCH", "path": "/party-api/v1/respondents/email", "body": respondent_email_0, "headers": <headers> }, ] """ try: requests = json.loads(request.data) except ValueError as e: abort(400) responses = [] for index, req in enumerate(requests): method = req['method'] path = req['path'] body = req.get('body', None) headers = req.get('headers', None) with current_app.app_context(): with current_app.test_request_context(path, method=method, json=body, headers=headers): try: rv = current_app.preprocess_request() if rv is None: rv = current_app.dispatch_request() except Exception as e: rv = current_app.handle_user_exception(e) response = current_app.make_response(rv) response = current_app.process_response(response) responses.append({ "status": response.status_code, }) return make_response(json.dumps(responses), 207)
def _handle_cli_init(manager): current_app.preprocess_request() call_plugins('on_cli_init', manager)
def run(self): current_app.preprocess_request() g.site.walk()
def run(self): current_app.preprocess_request() g.site.initialize()
def run(self, strict, addnew, push, message): current_app.preprocess_request() if not message: message = 'synced changes' _sync(strict, addnew, push, message)
def run(self, edit, article): current_app.preprocess_request() _add_tags_for_indexed_article(article, edit)
def prepare_context(): ctx = app.test_request_context() ctx.push() app.preprocess_request()
def run_in_context(repo_path, func, *args, **kwargs): """run the function in a YAWT/Flask request context""" app = yawt.create_app(repo_path) with app.test_request_context(): current_app.preprocess_request() func(*args, **kwargs)
def _init_app(): ctx = current_app.test_request_context() ctx.push() current_app.preprocess_request()
def run(self, post, network, link=None): current_app.preprocess_request() return _post_and_save(post, network, link)