Commit 67814e417caf4be085941d03ea6a908483a9fee3
1 parent
8ffdad81
Exists in
master
and in
4 other branches
New cli command feed_from_irap()
Showing
1 changed file
with
88 additions
and
0 deletions
Show diff stats
app/commands/commands.py
@@ -6,6 +6,7 @@ import random | @@ -6,6 +6,7 @@ import random | ||
6 | 6 | ||
7 | from flask import current_app | 7 | from flask import current_app |
8 | from sqlalchemy.exc import OperationalError, IntegrityError | 8 | from sqlalchemy.exc import OperationalError, IntegrityError |
9 | +from sqlalchemy.orm.exc import NoResultFound | ||
9 | from sqlalchemy.sql import func | 10 | from sqlalchemy.sql import func |
10 | from sqlalchemy.ext.automap import automap_base | 11 | from sqlalchemy.ext.automap import automap_base |
11 | from sqlalchemy.orm import Session | 12 | from sqlalchemy.orm import Session |
@@ -17,6 +18,93 @@ from app.auth.models import User, _nameToRole | @@ -17,6 +18,93 @@ from app.auth.models import User, _nameToRole | ||
17 | from . import bp | 18 | from . import bp |
18 | 19 | ||
19 | 20 | ||
21 | +@bp.cli.command("feed_from_irap") | ||
22 | +@click.option('--csv-file', '-f', 'csv_file_name', help="the csv file path to feed from") | ||
23 | +def feed_from_irap(csv_file_name): | ||
24 | + """ | ||
25 | + Use an Irap csv charges files and feed db with | ||
26 | + | ||
27 | + :param csv_file_name: | ||
28 | + :return: | ||
29 | + """ | ||
30 | + | ||
31 | + rows = [] | ||
32 | + | ||
33 | + with open(csv_file_name, newline='') as csvfile: | ||
34 | + csvreader = csv.DictReader(csvfile, delimiter=',', quotechar='"') | ||
35 | + for row in csvreader: | ||
36 | + # print('\n'.join(row.keys())) | ||
37 | + # break | ||
38 | + rows.append(row) | ||
39 | + | ||
40 | + # todo: service_title | ||
41 | + # project_tile | ||
42 | + # typology_tile | ||
43 | + # thematic_tile | ||
44 | + # agent_tile | ||
45 | + | ||
46 | + projects = [r['PROJETS'].strip() for r in rows] | ||
47 | + projects = sorted(set(projects)) | ||
48 | + agents = [(r['NOM'].strip(), r['prénom'].strip()) for r in rows] | ||
49 | + agents = sorted(set(agents)) | ||
50 | + thematics = [r['thématique'].strip() for r in rows] | ||
51 | + thematics = sorted(set(thematics)) | ||
52 | + typologies = [r['TYPOLOGIE'].strip() for r in rows] | ||
53 | + typologies = sorted(set(typologies)) | ||
54 | + services = [r['Groupe métier'].strip() for r in rows] | ||
55 | + services = sorted(set(services)) | ||
56 | + | ||
57 | + for a in agents: | ||
58 | + n_a = Agent(firstname=a[0], secondname=a[1]) | ||
59 | + db.session.add(n_a) | ||
60 | + db.session.commit() | ||
61 | + # a = Agent.query.filter(Agent.firstname == 'ESPAIGNOL').one() | ||
62 | + # print(f">{a.secondname}<") | ||
63 | + # sys.exit() | ||
64 | + | ||
65 | + for p in projects: | ||
66 | + n_p = Project(name=p) | ||
67 | + db.session.add(n_p) | ||
68 | + db.session.commit() | ||
69 | + | ||
70 | + for s in services: | ||
71 | + n_s = Service(name=s) | ||
72 | + db.session.add(n_s) | ||
73 | + db.session.commit() | ||
74 | + | ||
75 | + for p in range(2011, 2030): | ||
76 | + n_p = Period(name=f"{p}") | ||
77 | + db.session.add(n_p) | ||
78 | + db.session.commit() | ||
79 | + | ||
80 | + for r in rows: | ||
81 | + p = Project.query.filter(Project.name == r['PROJETS']).one() | ||
82 | + try: | ||
83 | + a = Agent.query.filter(Agent.firstname == r['NOM'], Agent.secondname == r['prénom']).one() | ||
84 | + except NoResultFound: | ||
85 | + print(f"Not found >{a.firstname}< >{a.secondname}<") | ||
86 | + continue | ||
87 | + s = Service.query.filter(Service.name == r['Groupe métier']).one() | ||
88 | + for period_name in range(2011, 2030): | ||
89 | + t = Period.query.filter(Period.name == period_name).one() | ||
90 | + charge = r[f"{period_name}"] | ||
91 | + # print(f">{charge}<") | ||
92 | + try: | ||
93 | + charge = float(charge) | ||
94 | + charge = int(100*charge) | ||
95 | + except ValueError: | ||
96 | + # print(f"Wrong charge {charge}") | ||
97 | + charge = 0 | ||
98 | + n_c = Charge(agent_id=a.id, | ||
99 | + project_id=p.id, | ||
100 | + service_id=s.id, | ||
101 | + capacity_id=0, | ||
102 | + period_id=t.id, | ||
103 | + charge_rate=charge) | ||
104 | + db.session.add(n_c) | ||
105 | + db.session.commit() | ||
106 | + | ||
107 | + | ||
20 | @bp.cli.command("fake_lesia_names") | 108 | @bp.cli.command("fake_lesia_names") |
21 | def fake_lesia_names(): | 109 | def fake_lesia_names(): |
22 | """ | 110 | """ |