Commit 90fdccc76f4486391cfca8683d4edc5a3961fb35

Authored by hitier
1 parent 889be3e2

Now feed projects' category/labels from lesia db

Showing 2 changed files with 60 additions and 9 deletions   Show diff stats
app/commands/commands.py
... ... @@ -305,7 +305,63 @@ def feed_from_lesia():
305 305 Remember to configure the proper database uri in the db_config.py file.
306 306 """
307 307 from .lesia_db import lesia_agent, lesia_session, lesia_service, lesia_project, \
308   - lesia_fonction, lesia_periods, lesia_affectation
  308 + lesia_fonction, lesia_periods, lesia_affectation, lesia_domains, lesia_poles, \
  309 + lesia_domainprojects
  310 +
  311 + # Feed all lesia 'domaine' names and link to new category "Domaine"
  312 + #
  313 + domain_category = Category(name="Domaine")
  314 + domains = lesia_session.query(lesia_domains)
  315 + for d in domains:
  316 + n_l = Label(name=d.nom, category=domain_category)
  317 + db.session.add(n_l)
  318 + db.session.commit()
  319 +
  320 + # Feed all lesia 'pôle' names and link to new category "Pôle"
  321 + #
  322 + pole_category = Category(name="Pôle")
  323 + poles = lesia_session.query(lesia_poles)
  324 + for p in poles:
  325 + n_l = Label(name=p.nom, category=pole_category)
  326 + db.session.add(n_l)
  327 + db.session.commit()
  328 +
  329 + # Feed lesia project with proper "pôle"
  330 + # (as this information is stored in gestit_projets)
  331 + #
  332 + projects = lesia_session.query(lesia_project).all()
  333 + for p in projects:
  334 + # add project
  335 + n_p = Project(id=p.id, name=p.nom)
  336 + db.session.add(n_p)
  337 + # get corresponding lesia pole name
  338 + pole_name = lesia_session.query(lesia_poles).filter(lesia_poles.id == p.pole_id).one().nom
  339 + # search corresponding Label and store in ProjectLabel table
  340 + n_l = Label.query.filter(Label.name == pole_name).one()
  341 + n_pl = ProjectLabel(project=n_p, category=n_l.category, label=n_l)
  342 + db.session.add(n_pl)
  343 + db.session.commit()
  344 +
  345 + # Get projects domain information and store in ProjectLabel
  346 + #
  347 + domain_projects = lesia_session.query(lesia_domainprojects)
  348 + for dp in domain_projects:
  349 + project_name = lesia_session.query(lesia_project).filter(lesia_project.id == dp.projet_id).one().nom
  350 + domain_name = lesia_session.query(lesia_domains).filter(lesia_domains.id == dp.domaine_id).one().nom
  351 + n_p = Project.query.filter(Project.name == project_name).one()
  352 + n_l = Label.query.filter(Label.name == domain_name).one()
  353 + n_pl = ProjectLabel(project=n_p, category=n_l.category, label=n_l)
  354 + db.session.add(n_pl)
  355 + # Some projects have 2 domain labels in lesia db
  356 + # That is not allowed any more in the new model.
  357 + #
  358 + try:
  359 + db.session.commit()
  360 + except IntegrityError:
  361 + db.session.rollback()
  362 + current_app.logger.error(
  363 + "Error adding project to category/label: {} {} {}".format(n_p.name, n_l.category.name, n_l.name))
  364 + continue
309 365  
310 366 agents = lesia_session.query(lesia_agent).all()
311 367 for a in agents:
... ... @@ -319,12 +375,6 @@ def feed_from_lesia():
319 375 db.session.add(n_s)
320 376 db.session.commit()
321 377  
322   - projects = lesia_session.query(lesia_project).all()
323   - for p in projects:
324   - n_p = Project(id=p.id, name=p.nom)
325   - db.session.add(n_p)
326   - db.session.commit()
327   -
328 378 fonctions = lesia_session.query(lesia_fonction).all()
329 379 for f in fonctions:
330 380 n_c = Capacity(id=f.id, name=f.nom)
... ...
app/commands/lesia_db.py
... ... @@ -20,7 +20,6 @@ except OperationalError as oe:
20 20  
21 21 lesia_session = Session(engine)
22 22  
23   -
24 23 # mapped classes are now created with names by default
25 24 # matching that of the table name.
26 25 lesia_agent = lesia_base.classes.agent
... ... @@ -29,4 +28,6 @@ lesia_project = lesia_base.classes.gestit_projets
29 28 lesia_fonction = lesia_base.classes.gestit_fonctions
30 29 lesia_affectation = lesia_base.classes.gestit_affectations
31 30 lesia_periods = lesia_base.classes.gestit_semestres
32   -
  31 +lesia_domains = lesia_base.classes.gestit_domaines
  32 +lesia_poles = lesia_base.classes.poles
  33 +lesia_domainprojects = lesia_base.classes.gestit_domaine_projets
... ...