From 14f36f553c4e57883d938bb17324ab939d395371 Mon Sep 17 00:00:00 2001 From: Richard Hitier Date: Thu, 8 Apr 2021 19:44:29 +0200 Subject: [PATCH] Move lesia init commands to module --- app/commands/commands.py | 39 ++++++++++++++++----------------------- app/commands/lesia_db.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 23 deletions(-) create mode 100644 app/commands/lesia_db.py diff --git a/app/commands/commands.py b/app/commands/commands.py index d5bda86..f89eeaa 100644 --- a/app/commands/commands.py +++ b/app/commands/commands.py @@ -15,6 +15,18 @@ from app.auth.models import User from . import bp +@bp.cli.command("lesia_to_csv") +def lesia_to_csv(): + """ + Extract some datas from lesia db to csv file for later db creation + - agents + - services + - capacities + - projects + :return: + """ + + @bp.cli.command("feed_from_lesia") def feed_from_lesia(): """ @@ -22,27 +34,8 @@ def feed_from_lesia(): configure the proper database uri in the db_config.py file. """ - Base = automap_base() - - engine = create_engine(current_app.config['LESIA_AGENTS_DB_URI']) - - # reflect the tables - try: - Base.prepare(engine, reflect=True) - except OperationalError: - current_app.logger.error("Please, configure the mysql database (see db_config.py)") - sys.exit(-1) - - # mapped classes are now created with names by default - # matching that of the table name. - lesia_agent = Base.classes.agent - lesia_service = Base.classes.gestit_services - lesia_project = Base.classes.gestit_projets - lesia_fonction = Base.classes.gestit_fonctions - lesia_affectation = Base.classes.gestit_affectations - lesia_periods = Base.classes.gestit_semestres - - lesia_session = Session(engine) + from .lesia_db import lesia_agent, lesia_session, lesia_service, lesia_project, \ + lesia_fonction, lesia_periods, lesia_affectation agents = lesia_session.query(lesia_agent).all() for a in agents: @@ -161,11 +154,11 @@ def create_db(): except IntegrityError: current_app.logger.error("User admin already exists, database should be empty at create") if sqlite_uri: - current_app.logger.error("see "+sqlite_uri) + current_app.logger.error("see " + sqlite_uri) sys.exit(-1) if sqlite_uri: - current_app.logger.info("Created sqlite db: "+sqlite_uri) + current_app.logger.info("Created sqlite db: " + sqlite_uri) @bp.cli.command('user_add') diff --git a/app/commands/lesia_db.py b/app/commands/lesia_db.py new file mode 100644 index 0000000..47063ac --- /dev/null +++ b/app/commands/lesia_db.py @@ -0,0 +1,31 @@ +import sys + +from flask import current_app +from sqlalchemy import create_engine +from sqlalchemy.exc import OperationalError +from sqlalchemy.ext.automap import automap_base +from sqlalchemy.orm import Session + +lesia_base = automap_base() + +engine = create_engine(current_app.config['LESIA_AGENTS_DB_URI']) + +# reflect the tables +try: + lesia_base.prepare(engine, reflect=True) +except OperationalError: + current_app.logger.error("Please, configure the mysql database (see db_config.py)") + sys.exit(-1) + +lesia_session = Session(engine) + + +# mapped classes are now created with names by default +# matching that of the table name. +lesia_agent = lesia_base.classes.agent +lesia_service = lesia_base.classes.gestit_services +lesia_project = lesia_base.classes.gestit_projets +lesia_fonction = lesia_base.classes.gestit_fonctions +lesia_affectation = lesia_base.classes.gestit_affectations +lesia_periods = lesia_base.classes.gestit_semestres + -- libgit2 0.21.2