def test_decorators(): app = Flask('testapp') ext = FlaskJSON() @ext.error_handler def err_handler_func(): pass @ext.invalid_json_error def decoder_func(): pass @ext.encoder def encoder_func(): pass assert ext._error_handler_func == err_handler_func assert ext._decoder_error_func == decoder_func # If FlaskJSON is not initialized with the app then only # '_encoder_class' will be set. assert ext._encoder_class is not None assert app.json_encoder is not ext._encoder_class # And after initialization we set our json encoder. ext.init_app(app) assert app.json_encoder is ext._encoder_class
def test_decorators(): app = Flask(__name__) ext = FlaskJSON() @ext.error_handler def err_handler_func(): pass @ext.invalid_json_error def decoder_func(): pass @ext.encoder def encoder_func(): pass assert_equals(ext._error_handler_func, err_handler_func) assert_equals(ext._decoder_error_func, decoder_func) # If FlaskJSON is not initialized with the app then only # '_encoder_class' will be set. assert_is_not_none(ext._encoder_class) assert_is_not(app.json_encoder, ext._encoder_class) # And after initialization we set our json encoder. ext.init_app(app) assert_is(app.json_encoder, ext._encoder_class)
def create_app_test(): app = Flask(__name__, template_folder='../Views/templates', static_url_path='', static_folder='../Views/static') json = FlaskJSON() cors = CORS(resources={r"/api/*": { "origins": "*" }}, allow_headers=[ "Content-Type", "Accept", "X-Request-With", "access-control-allow-origin", "Access-Control-Allow-Credentials" ], supports_credentials=True) app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0 app.config['MONGODB_SETTINGS'] = { 'db': os.environ.get('MONGO_TEST_NAME'), 'host': os.environ.get('MONGO_TEST_URI') } app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY') mongo.init_app(app) # app.config['SESSION_COOKIE_HTTPONLY'] = True app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(minutes=60) json.init_app(app) cors.init_app(app) app.register_blueprint(main) app.register_blueprint(diagnosis_controller) app.register_blueprint(patients_controller) app.register_blueprint(visualisation_controller) return app
def test_decorators(): app = Flask(__name__) ext = FlaskJSON() @ext.error_handler def err_handler_func(): pass @ext.invalid_json_error def decoder_func(): pass @ext.encoder def encoder_func(): pass assert ext._error_handler_func == err_handler_func assert ext._decoder_error_func == decoder_func # If FlaskJSON is not initialized with the app then only # '_encoder_class' will be set. assert ext._encoder_class is not None assert app.json_encoder is not ext._encoder_class # And after initialization we set our json encoder. ext.init_app(app) assert app.json_encoder is ext._encoder_class
def create_app(): start = time.time() main_run = is_main_run() # create and configure the app app = Flask(__name__) app.config.from_object(os.environ.get( 'APP_SETTINGS', "facial_beauty_predictor.server.config.Config")) if main_run: logging.info("Creating app with config:\n" + '\n'.join(" {}: {}".format(k, v) for k, v in app.config.items())) if main_run: app.errorhandler(Exception)(_on_exception) # if app.config["LOG_METRICS"]: # metrics.init_app(app) # request_check.init_app(app) fetch_files(app) app.config["PERCENTILE_QUEUES"] = {} img_paths_queue = init_worker(app) app.config["IMG_PATHS_QUEUE"] = img_paths_queue json = FlaskJSON() json.init_app(app) init_app(app) if not app.config["DEBUG"]: sentry_sdk.init( dsn="https://[email protected]/1785265", integrations=[FlaskIntegration()], ) logging.info("App initialization done. Took {0:.1f}s".format( time.time() - start)) return app
def test_init_deferred(): app = Flask('testapp') # Check if we correctly handle this. # Well actually it's to increase test coverage. del app.extensions ext = FlaskJSON() ext.init_app(app) assert app.config.get('JSON_ADD_STATUS') == True assert app.config.get('JSON_DATE_FORMAT') is None assert app.config.get('JSON_TIME_FORMAT') is None assert app.config.get('JSON_DATETIME_FORMAT') is None assert app.config.get('JSON_DECODE_ERROR_MESSAGE') == 'Not a JSON.' assert app.request_class is JsonRequest # No testing response class on production. assert app.response_class is not JsonTestResponse assert app.json_encoder is JSONEncoderEx assert app.extensions['json'] == ext
def test_init_deferred(): app = Flask(__name__) # Check if we correctly handle this. # Well actually it's to increase test coverage. del app.extensions ext = FlaskJSON() ext.init_app(app) assert_equals(app.config.get('JSON_ADD_STATUS'), True) assert_is_none(app.config.get('JSON_DATE_FORMAT')) assert_is_none(app.config.get('JSON_TIME_FORMAT')) assert_is_none(app.config.get('JSON_DATETIME_FORMAT')) assert_equals(app.config.get('JSON_DECODE_ERROR_MESSAGE'), 'Not a JSON.') assert_is(app.request_class, JsonRequest) # No testing response class on production. assert_is_not(app.response_class, JsonTestResponse) assert_is(app.json_encoder, JSONEncoderEx) assert_equals(app.extensions['json'], ext)
def test_init_deferred(): app = Flask(__name__) # Check if we correctly handle this. # Well actually it's to increase test coverage. del app.extensions ext = FlaskJSON() ext.init_app(app) assert app.config.get('JSON_ADD_STATUS') == True assert app.config.get('JSON_DATE_FORMAT') is None assert app.config.get('JSON_TIME_FORMAT') is None assert app.config.get('JSON_DATETIME_FORMAT') is None assert app.config.get('JSON_DECODE_ERROR_MESSAGE') == 'Not a JSON.' assert app.request_class is JsonRequest # No testing response class on production. assert app.response_class is not JsonTestResponse assert app.json_encoder is JSONEncoderEx assert app.extensions['json'] == ext
from flask import Flask from flask import request from flask_json import FlaskJSON, json_response app = Flask(__name__) json = FlaskJSON(app) json.init_app(app) @app.route('/sum/<int:operatorone>/<int:operatortwo>', methods=['GET']) def sum(operatorone, operatortwo): value = operatorone + operatortwo return json_response(data=value, headers_={'X-STATUS': 'ok'}) if __name__ == "__main__": app.run(host='0.0.0.0', port=5000)
from flask import Flask, request, jsonify from flask_json import FlaskJSON, JsonError, json_response from os import path from datetime import datetime, timedelta from pathlib import Path from configs.config_data_local import UPLOADS_DEFAULT_DEST, INIT_DATE_TIME_FORMAT, RAIN_FALL_FILE_NAME, HEC_HMS_MODEL_DIR from distributed_model.rain_fall import create_rain_files app = Flask(__name__) flask_json = FlaskJSON() flask_json.init_app(app) @app.route('/') def hello_world(): return 'Welcome to HecHms(Distributed) Server!' @app.route('/HECHMS/distributed/init-model', methods=['GET', 'POST']) @app.route('/HECHMS/distributed/init-model/<string:run_datetime>', methods=['GET', 'POST']) @app.route( '/HECHMS/distributed/init-model/<string:run_datetime>/<int:back_days>/<int:forward_days>', methods=['GET', 'POST']) def prepare_input_files( run_datetime=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), back_days=2, forward_days=3): print('prepare_input_files.') print('run_datetime : ', run_datetime) print('back_days : ', back_days)