def app(): config_path = get_config_file_path() config = load_config(config_path) app = server.get_app(config) app.config["TESTING"] = True app.config["WTF_CSRF_ENABLED"] = False return app
def get_app_for_cmd(config_file=None, with_external_mods=True): """ Return the flask app object, logging error instead of raising them""" try: conf = load_config(config_file) return get_app(conf, with_external_mods=with_external_mods) except ConfigError as e: log.critical(str(e) + "\n") sys.exit(1)
def get_app_for_cmd(config_file=None, with_external_mods=True): """ Return the flask app object, logging error instead of raising them""" try: conf = load_config(config_file) return get_app(conf, with_external_mods=with_external_mods) except ConfigError as e: log.critical(str(e) + "\n") sys.exit(1)
def geonature_app(): """ set the application context """ config_path = get_config_file_path() config = load_config(config_path) app = server.get_app(config) ctx = app.app_context() ctx.push() yield app ctx.pop()
def add_application_db(module_name, url, module_id=None): log.info('Register the module in t_application ... \n') app_conf = load_config(DEFAULT_CONFIG_FIlE) id_application_geonature = app_conf['ID_APPLICATION_GEONATURE'] app = get_app_for_cmd(DEFAULT_CONFIG_FIlE) try: with app.app_context(): # if module_id: try to insert in t_application # check if the module in TApplications if module_id is None: try: exist_app = None exist_app = DB.session.query(TApplications).filter( TApplications.nom_application == module_name).one() except NoResultFound: # if no result, write in TApplication new_application = TApplications( nom_application=module_name, id_parent=id_application_geonature) DB.session.add(new_application) DB.session.commit() DB.session.flush() module_id = new_application.id_application else: log.info('the module is already in t_application') finally: module_id = module_id if module_id is not None else exist_app.id_application # try to write in gn_commons.t_module if not exist try: module = DB.session.query(TModules).filter( TModules.module_name == module_name).one() except NoResultFound: update_url = "{}/#/{}".format(app_conf['URL_APPLICATION'], url) new_module = TModules(id_module=module_id, module_name=module_name, module_label=module_name.title(), module_url=update_url, module_target="_self", module_picto="extension", active_frontend=True, active_backend=True) DB.session.add(new_module) DB.session.commit() else: log.info('the module is already in t_module, reactivate it') module.active = True DB.session.merge(module) DB.session.commit() except Exception as e: raise GeoNatureError(e) log.info('... ok \n') return module_id
def execute_script(file_name): """ Execute a script to set or delete sample data before test """ config_path = get_config_file_path() config = load_config(config_path) conn = psycopg2.connect(config["SQLALCHEMY_DATABASE_URI"]) cur = conn.cursor() sql_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), file_name) cur.execute(open(sql_file, "r").read()) conn.commit() cur.close() conn.close()
def pytest_sessionstart(session): """ before session.main() is called. """ config_path = get_config_file_path() config = load_config(config_path) app = server.get_app(config) app.config['TESTING'] = True # push the app_context ctx = app.app_context() ctx.push() # setup test data execute_script('delete_sample_data.sql') execute_script('sample_data.sql')
def execute_script(file_name): """ Execute a script to set or delete sample data before test """ config_path = get_config_file_path() config = load_config(config_path) conn = psycopg2.connect(config['SQLALCHEMY_DATABASE_URI']) cur = conn.cursor() sql_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), file_name) cur.execute(open(sql_file, 'r').read()) conn.commit() cur.close() conn.close()
def pytest_sessionstart(session): """ before session.main() is called. """ config_path = get_config_file_path() config = load_config(config_path) app = server.get_app(config) app.config["TESTING"] = True # push the app_context ctx = app.app_context() ctx.push() logging.disable(logging.DEBUG) # setup test data execute_script("delete_sample_data.sql") execute_script("sample_data.sql")
def frontend_routes_templating(app=None): if not app: app = get_app_for_cmd(with_external_mods=False) log.info("Generating frontend routing") # recuperation de la configuration configs_gn = load_config(get_config_file_path()) from geonature.utils.env import list_frontend_enabled_modules with open( str(ROOT_DIR / "frontend/src/app/routing/app-routing.module.ts.sample"), "r") as input_file: template = Template(input_file.read()) routes = [] for url_path, module_code in list_frontend_enabled_modules(app): location = Path(GN_EXTERNAL_MODULE / module_code.lower()) # test if module have frontend if (location / "frontend").is_dir(): path = url_path.lstrip("/") location = "{}/{}#GeonatureModule".format( location, GN_MODULE_FE_FILE) routes.append({ "path": path, "location": location, "module_code": module_code }) # TODO test if two modules with the same name is okay for Angular route_template = template.render( routes=routes, enable_user_management=configs_gn["ACCOUNT_MANAGEMENT"].get( "ENABLE_USER_MANAGEMENT"), enable_sign_up=configs_gn["ACCOUNT_MANAGEMENT"].get( "ENABLE_SIGN_UP"), ) with open( str(ROOT_DIR / "frontend/src/app/routing/app-routing.module.ts"), "w") as output_file: output_file.write(route_template) log.info("...%s\n", MSG_OK)
def add_application_db(app, module_code, url, enable_frontend): log.info("Register the module in gn_commons.t_modules ... \n") from geonature.core.users.models import TApplications from geonature.core.gn_commons.models import TModules app_conf = load_config(DEFAULT_CONFIG_FILE) id_application_geonature = app_conf["ID_APPLICATION_GEONATURE"] # remove / at the end and at the beginning if url[0] == "/": url = url[1:] if url[-1:] == "/": url = url[:-1] # remove white space url = url.replace(" ", "") with app.app_context(): # try to write in gn_commons.t_module if not exist try: module = ( DB.session.query(TModules) .filter(TModules.module_code == module_code) .one() ) except NoResultFound: new_module = TModules( module_code=module_code, module_label=module_code.lower(), module_path=url, module_target="_self", module_picto="fa-puzzle-piece", active_frontend=enable_frontend, active_backend=True, ) DB.session.add(new_module) DB.session.commit() else: log.info("the module is already in t_module, reactivate it") module.active = True DB.session.merge(module) DB.session.commit() log.info("...%s\n", MSG_OK)
def add_application_db(app, module_code, url, enable_frontend, enable_backend): log.info("Register the module in gn_commons.t_modules ... \n") from geonature.core.users.models import TApplications from geonature.core.gn_commons.models import TModules app_conf = load_config(DEFAULT_CONFIG_FILE) id_application_geonature = app_conf["ID_APPLICATION_GEONATURE"] # remove / at the end and at the beginning if url[0] == "/": url = url[1:] if url[-1:] == "/": url = url[:-1] # remove white space url = url.replace(" ", "") with app.app_context(): # try to write in gn_commons.t_module if not exist try: module = ( DB.session.query(TModules) .filter(TModules.module_code == module_code) .one() ) except NoResultFound: new_module = TModules( module_code=module_code, module_label=module_code.lower(), module_path=url, module_target="_self", module_picto="fa-puzzle-piece", active_frontend=enable_frontend, active_backend=enable_backend, ) DB.session.add(new_module) DB.session.commit() else: log.info("the module is already in t_module, reactivate it") module.active = True DB.session.merge(module) DB.session.commit() log.info("...%s\n", MSG_OK)
""" Configuration du logger racine """ import logging from geonature.utils.env import load_config, DEFAULT_CONFIG_FILE conf = load_config(DEFAULT_CONFIG_FILE) root_logger = logging.getLogger() root_logger.addHandler(logging.StreamHandler()) root_logger.setLevel(conf['SERVER']['LOG_LEVEL'])
""" Give a unique entry point for gunicorn """ from geonature.utils.env import load_config, get_config_file_path from server import get_app # get the app config file config_path = get_config_file_path() config = load_config(config_path) #give the app context from server.py in a app object app = get_app(config)
import json from flask import url_for from cookies import Cookie import pytest import server from geonature.utils.env import load_config, get_config_file_path # TODO: fixture pour mettre des données test dans la base a chaque test # https://github.com/pytest-dev/pytest-flask/issues/70 CONF_PATH = get_config_file_path() APP_CONF = load_config(CONF_PATH) APP_ID = APP_CONF.get('ID_APPLICATION_GEONATURE') @pytest.fixture def app(): app = server.get_app(APP_CONF) app.config['TESTING'] = True return app def post_json(client, url, json_dict): """Send dictionary json_dict as a json to the specified url """ return client.post(url, data=json.dumps(json_dict), content_type='application/json') def json_of_response(response):
def app(): config_path = get_config_file_path() config = load_config(config_path) app = server.get_app(config) app.config['TESTING'] = True return app