Commit a17327bf837229c94e0ba103625edf6c765abb2c
1 parent
582b4b3a
Exists in
master
and in
4 other branches
New db feeding commands
Showing
5 changed files
with
131 additions
and
49 deletions
Show diff stats
app/cli/__init__.py deleted
... | ... | @@ -1,48 +0,0 @@ |
1 | -import click | |
2 | -from flask import Blueprint | |
3 | -from app.models import db, User | |
4 | - | |
5 | -bp = Blueprint('pdc_db', __name__) | |
6 | - | |
7 | - | |
8 | -@bp.cli.command('delete_user') | |
9 | -@click.argument('user_id') | |
10 | -def delete(user_id): | |
11 | - user = User.query.get(user_id) | |
12 | - db.session.delete(user) | |
13 | - db.session.commit() | |
14 | - | |
15 | - | |
16 | -@bp.cli.command('create_db') | |
17 | -def create(): | |
18 | - db.create_all() | |
19 | - admin = User(email='admin@nowhere.org', name='admin', login='admin', password='admin') | |
20 | - db.session.add(admin) | |
21 | - db.session.commit() | |
22 | - | |
23 | - | |
24 | -@bp.cli.command('add_user') | |
25 | -@click.argument('email') | |
26 | -@click.argument('name') | |
27 | -@click.argument('password') | |
28 | -def add(email, name, password): | |
29 | - user = User(email=email, name=name, password=password) | |
30 | - db.session.add(user) | |
31 | - db.session.commit() | |
32 | - print("added ", name) | |
33 | - | |
34 | - | |
35 | -@bp.cli.command('show_all') | |
36 | -def show_all(): | |
37 | - print("{:<5} {:<15} {:<15} {:<15} {:<15}".format('id', 'name', 'login', 'passwd', 'email')) | |
38 | - print("{:<5} {:<15} {:<15} {:<15} {:<15}".format('-' * 5, '-' * 15, '-' * 15, '-' * 15, '-' * 15)) | |
39 | - for user in User.query.all(): | |
40 | - print(user.login) | |
41 | - print("{:<5} {:<15} {:<15} {:<15} {:<15}".format( | |
42 | - user.id, | |
43 | - user.name, | |
44 | - user.login, | |
45 | - user.password, | |
46 | - user.email | |
47 | - )) | |
48 | - |
... | ... | @@ -0,0 +1,124 @@ |
1 | +import click | |
2 | +import random | |
3 | + | |
4 | +from sqlalchemy.ext.automap import automap_base | |
5 | +from sqlalchemy.orm import Session | |
6 | +from sqlalchemy import create_engine | |
7 | + | |
8 | +from app.models import db, User, Agent, Service, Project, Capacity, Period, Charge | |
9 | + | |
10 | +from db_config import mysql_uri | |
11 | + | |
12 | +from . import bp | |
13 | + | |
14 | +@bp.cli.command("feed_from_lesia") | |
15 | +def feed_from_lesia(): | |
16 | + Base = automap_base() | |
17 | + | |
18 | + engine = create_engine(mysql_uri) | |
19 | + | |
20 | + # reflect the tables | |
21 | + Base.prepare(engine, reflect=True) | |
22 | + | |
23 | + # mapped classes are now created with names by default | |
24 | + # matching that of the table name. | |
25 | + LesiaAgent = Base.classes.agent | |
26 | + LesiaService = Base.classes.gestit_services | |
27 | + LesiaProject = Base.classes.gestit_projets | |
28 | + LesiaFonction = Base.classes.gestit_fonctions | |
29 | + | |
30 | + lesia_session = Session(engine) | |
31 | + agents = lesia_session.query(LesiaAgent).all() | |
32 | + for a in agents: | |
33 | + n_a = Agent(firstname=a.nom, secondname=a.prenom) | |
34 | + db.session.add(n_a) | |
35 | + db.session.commit() | |
36 | + | |
37 | + services = lesia_session.query(LesiaService).all() | |
38 | + for s in services: | |
39 | + n_s = Service(name=s.nom) | |
40 | + db.session.add(n_s) | |
41 | + db.session.commit() | |
42 | + | |
43 | + projects = lesia_session.query(LesiaProject).all() | |
44 | + for p in projects: | |
45 | + n_p = Project(name=p.nom) | |
46 | + db.session.add(n_p) | |
47 | + db.session.commit() | |
48 | + | |
49 | + fonctions = lesia_session.query(LesiaFonction).all() | |
50 | + for f in fonctions: | |
51 | + n_c = Capacity(name=f.nom) | |
52 | + db.session.add(n_c) | |
53 | + db.session.commit() | |
54 | + | |
55 | +@bp.cli.command("feed_periods") | |
56 | +def feed_periods(): | |
57 | + for y in ['2019', '2020', '2021']: | |
58 | + for s in ['S1', 'S2']: | |
59 | + period_name = "{}_{}".format(y, s) | |
60 | + p = Period(name=period_name) | |
61 | + db.session.add(p) | |
62 | + db.session.commit() | |
63 | + | |
64 | + | |
65 | +@bp.cli.command("random_charges") | |
66 | +def random_charges(): | |
67 | + for i in range(0, 100): | |
68 | + agent_id = random.choice([i for (i,) in db.session.query(Agent.id).all() ]) | |
69 | + project_id = random.choice([i for (i,) in db.session.query(Project.id).all()]) | |
70 | + service_id = random.choice([i for (i,) in db.session.query(Service.id).all()]) | |
71 | + capacity_id = random.choice([i for (i,) in db.session.query(Capacity.id).all()]) | |
72 | + period_id = random.choice([i for (i,) in db.session.query(Period.id).all()]) | |
73 | + percent = random.choice(range(10, 110, 10)) | |
74 | + charge = Charge(agent_id=agent_id , | |
75 | + project_id=project_id, | |
76 | + service_id=service_id, | |
77 | + capacity_id=capacity_id, | |
78 | + period_id=period_id, | |
79 | + charge_rate=percent) | |
80 | + print("adding {}_{}_{}_{}_{}_{}".format(agent_id, project_id, service_id, capacity_id, period_id, percent)) | |
81 | + db.session.add(charge) | |
82 | + db.session.commit() | |
83 | + | |
84 | + | |
85 | +@bp.cli.command('delete_user') | |
86 | +@click.argument('user_id') | |
87 | +def delete_user(user_id): | |
88 | + user = User.query.get(user_id) | |
89 | + db.session.delete(user) | |
90 | + db.session.commit() | |
91 | + | |
92 | + | |
93 | +@bp.cli.command('create_db') | |
94 | +def create_db(): | |
95 | + db.create_all() | |
96 | + admin = User(email='admin@nowhere.org', name='admin', login='admin', password='admin') | |
97 | + db.session.add(admin) | |
98 | + db.session.commit() | |
99 | + | |
100 | + | |
101 | +@bp.cli.command('add_user') | |
102 | +@click.argument('email') | |
103 | +@click.argument('name') | |
104 | +@click.argument('password') | |
105 | +def add_user(email, name, password): | |
106 | + user = User(email=email, name=name, password=password) | |
107 | + db.session.add(user) | |
108 | + db.session.commit() | |
109 | + print("added ", name) | |
110 | + | |
111 | + | |
112 | +@bp.cli.command('show_all') | |
113 | +def show_all(): | |
114 | + print("{:<5} {:<15} {:<15} {:<15} {:<15}".format('id', 'name', 'login', 'passwd', 'email')) | |
115 | + print("{:<5} {:<15} {:<15} {:<15} {:<15}".format('-' * 5, '-' * 15, '-' * 15, '-' * 15, '-' * 15)) | |
116 | + for user in User.query.all(): | |
117 | + print(user.login) | |
118 | + print("{:<5} {:<15} {:<15} {:<15} {:<15}".format( | |
119 | + user.id, | |
120 | + user.name, | |
121 | + user.login, | |
122 | + user.password, | |
123 | + user.email | |
124 | + )) | ... | ... |
pdc_web.py
requirements.txt