common_db_feed.py 2.04 KB
import os
from shutil import copyfile

from app.models import Category, db, Label, Project, ProjectLabel


def resources_to_instancedb(app):
    # copy resource  demo db to test file
    appdir = os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir)
    sqlite_file_name = os.path.abspath(os.path.join(appdir, 'resources', 'lesia-btp.sqlite'))
    if not os.path.isdir(app.instance_path):
        os.mkdir(app.instance_path)
    db_path = os.path.join(app.instance_path, 'test.db')
    copyfile(sqlite_file_name, db_path)
    return db_path

# TODO: rewrite for new category/label model
#
# 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]))