我们从Python开源项目中,提取了以下31个代码示例,用于说明如何使用flask.current_app.testing()。
def __call__(self, form, field): if current_app.testing: return True if request.json: response = request.json.get('g-recaptcha-response', '') else: response = request.form.get('g-recaptcha-response', '') remote_ip = request.remote_addr if not response: raise ValidationError(field.gettext(self.message)) if not self._validate_recaptcha(response, remote_ip): field.recaptcha_error = 'incorrect-captcha-sol' raise ValidationError(field.gettext(self.message))
def __call__(self, form, field): if current_app.testing: return True if request.json: challenge = request.json.get('recaptcha_challenge_field', '') response = request.json.get('recaptcha_response_field', '') else: challenge = request.form.get('recaptcha_challenge_field', '') response = request.form.get('recaptcha_response_field', '') remote_ip = request.remote_addr if not challenge or not response: raise ValidationError(field.gettext(self.message)) if not self._validate_recaptcha(challenge, response, remote_ip): field.recaptcha_error = 'incorrect-captcha-sol' raise ValidationError(field.gettext(self.message))
def redirect_to_ssl(self): """ Redirect incoming requests to HTTPS. """ criteria = [ request.is_secure, current_app.debug, current_app.testing, request.headers.get('X-Forwarded-Proto', 'http') == 'https' ] if request.headers.get('User-Agent', '').lower().startswith(self.exclude_user_agents): return if not any(criteria): if request.url.startswith('http://'): url = request.url.replace('http://', 'https://', 1) r = redirect(url, code=301) return r
def __call__(self, form, field): if current_app.testing or not flaskbb_config["RECAPTCHA_ENABLED"]: return True if request.json: response = request.json.get('g-recaptcha-response', '') else: response = request.form.get('g-recaptcha-response', '') remote_ip = request.remote_addr if not response: raise ValidationError(field.gettext(self.message)) if not self._validate_recaptcha(response, remote_ip): field.recaptcha_error = 'incorrect-captcha-sol' raise ValidationError(field.gettext(self.message))
def log(self, logger): if self.status_code == 500: # something actually went wrong; investigate dct = self.to_dict() if current_app.debug or current_app.testing: message = dct.pop("message") logger.warning(message, extra=dct, exc_info=True) else: logger.warning(dct) else: # usually log at INFO; a raised exception can be an error or expected behavior (e.g. 404) logger.info(self.to_dict())
def server_shutdown(): if not current_app.testing: abort(404) shutdown = request.environ.get('werkzeug.server.shutdown') if not shutdown: abort(500) shutdown() return 'Shutting down...'
def setUp(self): self.application = create_app('config.LoadedConfig') self.ctx = self.application.app_context() self.ctx.push() current_app.config.from_object(config.LoadedConfig) current_app.register_blueprint(ga4gh) current_app.testing = True self.tester = current_app.test_client()
def shutdown(): if not current_app.testing: abort(404) shoutdown = request.environ.get('werkzeug.server.shutdown') if not shoutdown: abort(500) shoutdown() return u'?????????...'
def __init__(self, *args, **kwargs): if current_app.testing: self.TIME_LIMIT = None super(Form, self).__init__(*args, **kwargs)
def server_shutdown(): if not current_app.testing: abort(404) shutdown = request.environ.get('werkzeng.server.shutdown') if not shutdown: abort(500) shutdown() return 'shutting down...'
def create_app(): app = Flask(__name__) app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 7776000 # 3 months app.logger.setLevel(logging.INFO) app.register_blueprint(main, url_prefix='') app.register_blueprint(classes, url_prefix='/classes') app.register_blueprint(rooms, url_prefix='/rooms') app.register_blueprint(teachers, url_prefix='/teachers') app.jinja_env.globals.update(get_term=get_term, get_term_from_date=get_term_from_date) app.term_data = None def get_term_data(): if current_app.term_data is None: if current_app.testing: current_app.DATA_DIR = TEST_DATA_DIR else: current_app.DATA_DIR = DATA_DIR data_path = path.join(current_app.DATA_DIR, ALL_COURSES_FNAME) if not path.exists(data_path): print("Couldn't find term data at {}, exiting".format(data_path)) sys.exit(1) logging.info("Loading term data from %s...", data_path) with open(data_path) as f: courses_dict = json.load(f, object_pairs_hook=OrderedDict) current_app.courses_meta = courses_dict['meta'] current_app.term_data = courses_dict['term_data'] current_app.term_key_to_name = get_term_mapping(current_app.term_data) current_app.term_name_to_key = {v:k for k, v in current_app.term_key_to_name.items()} logging.info("Term data loaded") return current_app.term_data app.get_term_data = get_term_data @app.before_request def before_request(): get_term_data() @app.context_processor def inject_user(): return { "courses_meta": current_app.courses_meta, "last_updated": datetime.datetime.fromtimestamp(current_app.courses_meta['time']).strftime("%Y-%m-%d"), "term_key_to_name": current_app.term_key_to_name, "term_name_to_key": current_app.term_name_to_key } @app.errorhandler(404) def page_not_found(e): return render_template('404.html'), 404 return app