Commit 14f36f553c4e57883d938bb17324ab939d395371

Authored by hitier
1 parent fa86be39

Move lesia init commands to module

Showing 2 changed files with 47 additions and 23 deletions   Show diff stats
app/commands/commands.py
@@ -15,6 +15,18 @@ from app.auth.models import User @@ -15,6 +15,18 @@ from app.auth.models import User
15 from . import bp 15 from . import bp
16 16
17 17
  18 +@bp.cli.command("lesia_to_csv")
  19 +def lesia_to_csv():
  20 + """
  21 + Extract some datas from lesia db to csv file for later db creation
  22 + - agents
  23 + - services
  24 + - capacities
  25 + - projects
  26 + :return:
  27 + """
  28 +
  29 +
18 @bp.cli.command("feed_from_lesia") 30 @bp.cli.command("feed_from_lesia")
19 def feed_from_lesia(): 31 def feed_from_lesia():
20 """ 32 """
@@ -22,27 +34,8 @@ def feed_from_lesia(): @@ -22,27 +34,8 @@ def feed_from_lesia():
22 34
23 configure the proper database uri in the db_config.py file. 35 configure the proper database uri in the db_config.py file.
24 """ 36 """
25 - Base = automap_base()  
26 -  
27 - engine = create_engine(current_app.config['LESIA_AGENTS_DB_URI'])  
28 -  
29 - # reflect the tables  
30 - try:  
31 - Base.prepare(engine, reflect=True)  
32 - except OperationalError:  
33 - current_app.logger.error("Please, configure the mysql database (see db_config.py)")  
34 - sys.exit(-1)  
35 -  
36 - # mapped classes are now created with names by default  
37 - # matching that of the table name.  
38 - lesia_agent = Base.classes.agent  
39 - lesia_service = Base.classes.gestit_services  
40 - lesia_project = Base.classes.gestit_projets  
41 - lesia_fonction = Base.classes.gestit_fonctions  
42 - lesia_affectation = Base.classes.gestit_affectations  
43 - lesia_periods = Base.classes.gestit_semestres  
44 -  
45 - lesia_session = Session(engine) 37 + from .lesia_db import lesia_agent, lesia_session, lesia_service, lesia_project, \
  38 + lesia_fonction, lesia_periods, lesia_affectation
46 39
47 agents = lesia_session.query(lesia_agent).all() 40 agents = lesia_session.query(lesia_agent).all()
48 for a in agents: 41 for a in agents:
@@ -161,11 +154,11 @@ def create_db(): @@ -161,11 +154,11 @@ def create_db():
161 except IntegrityError: 154 except IntegrityError:
162 current_app.logger.error("User admin already exists, database should be empty at create") 155 current_app.logger.error("User admin already exists, database should be empty at create")
163 if sqlite_uri: 156 if sqlite_uri:
164 - current_app.logger.error("see "+sqlite_uri) 157 + current_app.logger.error("see " + sqlite_uri)
165 sys.exit(-1) 158 sys.exit(-1)
166 159
167 if sqlite_uri: 160 if sqlite_uri:
168 - current_app.logger.info("Created sqlite db: "+sqlite_uri) 161 + current_app.logger.info("Created sqlite db: " + sqlite_uri)
169 162
170 163
171 @bp.cli.command('user_add') 164 @bp.cli.command('user_add')
app/commands/lesia_db.py 0 → 100644
@@ -0,0 +1,31 @@ @@ -0,0 +1,31 @@
  1 +import sys
  2 +
  3 +from flask import current_app
  4 +from sqlalchemy import create_engine
  5 +from sqlalchemy.exc import OperationalError
  6 +from sqlalchemy.ext.automap import automap_base
  7 +from sqlalchemy.orm import Session
  8 +
  9 +lesia_base = automap_base()
  10 +
  11 +engine = create_engine(current_app.config['LESIA_AGENTS_DB_URI'])
  12 +
  13 +# reflect the tables
  14 +try:
  15 + lesia_base.prepare(engine, reflect=True)
  16 +except OperationalError:
  17 + current_app.logger.error("Please, configure the mysql database (see db_config.py)")
  18 + sys.exit(-1)
  19 +
  20 +lesia_session = Session(engine)
  21 +
  22 +
  23 +# mapped classes are now created with names by default
  24 +# matching that of the table name.
  25 +lesia_agent = lesia_base.classes.agent
  26 +lesia_service = lesia_base.classes.gestit_services
  27 +lesia_project = lesia_base.classes.gestit_projets
  28 +lesia_fonction = lesia_base.classes.gestit_fonctions
  29 +lesia_affectation = lesia_base.classes.gestit_affectations
  30 +lesia_periods = lesia_base.classes.gestit_semestres
  31 +