common_db_feed.py
1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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
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]))