From 90fdccc76f4486391cfca8683d4edc5a3961fb35 Mon Sep 17 00:00:00 2001 From: Richard Hitier Date: Mon, 3 May 2021 15:35:04 +0200 Subject: [PATCH] Now feed projects' category/labels from lesia db --- app/commands/commands.py | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- app/commands/lesia_db.py | 5 +++-- 2 files changed, 60 insertions(+), 9 deletions(-) diff --git a/app/commands/commands.py b/app/commands/commands.py index 2e5e1ce..59c434c 100644 --- a/app/commands/commands.py +++ b/app/commands/commands.py @@ -305,7 +305,63 @@ def feed_from_lesia(): Remember to configure the proper database uri in the db_config.py file. """ from .lesia_db import lesia_agent, lesia_session, lesia_service, lesia_project, \ - lesia_fonction, lesia_periods, lesia_affectation + lesia_fonction, lesia_periods, lesia_affectation, lesia_domains, lesia_poles, \ + lesia_domainprojects + + # Feed all lesia 'domaine' names and link to new category "Domaine" + # + domain_category = Category(name="Domaine") + domains = lesia_session.query(lesia_domains) + for d in domains: + n_l = Label(name=d.nom, category=domain_category) + db.session.add(n_l) + db.session.commit() + + # Feed all lesia 'pôle' names and link to new category "Pôle" + # + pole_category = Category(name="Pôle") + poles = lesia_session.query(lesia_poles) + for p in poles: + n_l = Label(name=p.nom, category=pole_category) + db.session.add(n_l) + db.session.commit() + + # Feed lesia project with proper "pôle" + # (as this information is stored in gestit_projets) + # + projects = lesia_session.query(lesia_project).all() + for p in projects: + # add project + n_p = Project(id=p.id, name=p.nom) + db.session.add(n_p) + # get corresponding lesia pole name + pole_name = lesia_session.query(lesia_poles).filter(lesia_poles.id == p.pole_id).one().nom + # search corresponding Label and store in ProjectLabel table + n_l = Label.query.filter(Label.name == pole_name).one() + n_pl = ProjectLabel(project=n_p, category=n_l.category, label=n_l) + db.session.add(n_pl) + db.session.commit() + + # Get projects domain information and store in ProjectLabel + # + domain_projects = lesia_session.query(lesia_domainprojects) + for dp in domain_projects: + project_name = lesia_session.query(lesia_project).filter(lesia_project.id == dp.projet_id).one().nom + domain_name = lesia_session.query(lesia_domains).filter(lesia_domains.id == dp.domaine_id).one().nom + n_p = Project.query.filter(Project.name == project_name).one() + n_l = Label.query.filter(Label.name == domain_name).one() + n_pl = ProjectLabel(project=n_p, category=n_l.category, label=n_l) + db.session.add(n_pl) + # Some projects have 2 domain labels in lesia db + # That is not allowed any more in the new model. + # + try: + db.session.commit() + except IntegrityError: + db.session.rollback() + current_app.logger.error( + "Error adding project to category/label: {} {} {}".format(n_p.name, n_l.category.name, n_l.name)) + continue agents = lesia_session.query(lesia_agent).all() for a in agents: @@ -319,12 +375,6 @@ def feed_from_lesia(): db.session.add(n_s) db.session.commit() - projects = lesia_session.query(lesia_project).all() - for p in projects: - n_p = Project(id=p.id, name=p.nom) - db.session.add(n_p) - db.session.commit() - fonctions = lesia_session.query(lesia_fonction).all() for f in fonctions: n_c = Capacity(id=f.id, name=f.nom) diff --git a/app/commands/lesia_db.py b/app/commands/lesia_db.py index 8c03813..5526504 100644 --- a/app/commands/lesia_db.py +++ b/app/commands/lesia_db.py @@ -20,7 +20,6 @@ except OperationalError as oe: 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 @@ -29,4 +28,6 @@ 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 - +lesia_domains = lesia_base.classes.gestit_domaines +lesia_poles = lesia_base.classes.poles +lesia_domainprojects = lesia_base.classes.gestit_domaine_projets -- libgit2 0.21.2