from app.models import Category, db, Label, Project, ProjectLabel categorized_labels = {'pole': ['Spatial', 'Sol'], 'domaine': ['soleil-terre', 'atmosphere', 'r&t', 'géologie']} projects_categories = {'ChemCam': {'pole': 'Spatial', 'domaine': 'géologie'}, 'Pilot': {'pole': 'Spatial', 'domaine': 'atmosphere'}, 'Ambre': {'pole': 'Spatial', 'domaine': 'soleil-terre'}} def feed_projects(): for c_name, labels in categorized_labels.items(): n_c = Category(name=c_name) db.session.add(n_c) for l_name in labels: n_l = Label(name=l_name, category=n_c) db.session.add(n_l) db.session.commit() project_names = projects_categories.keys() for p_name in set(project_names): n_p = Project(name=p_name) db.session.add(n_p) db.session.commit() for p, categories in projects_categories.items(): n_p = db.session.query(Project).filter(Project.name == p).one() for c_name, l_name in categories.items(): n_c = db.session.query(Category).filter(Category.name == c_name).one() n_l = db.session.query(Label).filter(Label.name == l_name).one() n_pc = ProjectLabel(project=n_p, category=n_c, label=n_l) db.session.add(n_pc) db.session.commit() def show_categories(): for c in db.session.query(Category).all(): print(c.name + ": ", ",".join([l.name for l in c.categorized_labels]))