From 67814e417caf4be085941d03ea6a908483a9fee3 Mon Sep 17 00:00:00 2001 From: Richard Hitier Date: Tue, 13 Apr 2021 18:18:44 +0200 Subject: [PATCH] New cli command feed_from_irap() --- app/commands/commands.py | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+), 0 deletions(-) diff --git a/app/commands/commands.py b/app/commands/commands.py index f2672b8..d715cc7 100644 --- a/app/commands/commands.py +++ b/app/commands/commands.py @@ -6,6 +6,7 @@ import random from flask import current_app from sqlalchemy.exc import OperationalError, IntegrityError +from sqlalchemy.orm.exc import NoResultFound from sqlalchemy.sql import func from sqlalchemy.ext.automap import automap_base from sqlalchemy.orm import Session @@ -17,6 +18,93 @@ from app.auth.models import User, _nameToRole from . import bp +@bp.cli.command("feed_from_irap") +@click.option('--csv-file', '-f', 'csv_file_name', help="the csv file path to feed from") +def feed_from_irap(csv_file_name): + """ + Use an Irap csv charges files and feed db with + + :param csv_file_name: + :return: + """ + + rows = [] + + with open(csv_file_name, newline='') as csvfile: + csvreader = csv.DictReader(csvfile, delimiter=',', quotechar='"') + for row in csvreader: + # print('\n'.join(row.keys())) + # break + rows.append(row) + + # todo: service_title + # project_tile + # typology_tile + # thematic_tile + # agent_tile + + projects = [r['PROJETS'].strip() for r in rows] + projects = sorted(set(projects)) + agents = [(r['NOM'].strip(), r['prénom'].strip()) for r in rows] + agents = sorted(set(agents)) + thematics = [r['thématique'].strip() for r in rows] + thematics = sorted(set(thematics)) + typologies = [r['TYPOLOGIE'].strip() for r in rows] + typologies = sorted(set(typologies)) + services = [r['Groupe métier'].strip() for r in rows] + services = sorted(set(services)) + + for a in agents: + n_a = Agent(firstname=a[0], secondname=a[1]) + db.session.add(n_a) + db.session.commit() + # a = Agent.query.filter(Agent.firstname == 'ESPAIGNOL').one() + # print(f">{a.secondname}<") + # sys.exit() + + for p in projects: + n_p = Project(name=p) + db.session.add(n_p) + db.session.commit() + + for s in services: + n_s = Service(name=s) + db.session.add(n_s) + db.session.commit() + + for p in range(2011, 2030): + n_p = Period(name=f"{p}") + db.session.add(n_p) + db.session.commit() + + for r in rows: + p = Project.query.filter(Project.name == r['PROJETS']).one() + try: + a = Agent.query.filter(Agent.firstname == r['NOM'], Agent.secondname == r['prénom']).one() + except NoResultFound: + print(f"Not found >{a.firstname}< >{a.secondname}<") + continue + s = Service.query.filter(Service.name == r['Groupe métier']).one() + for period_name in range(2011, 2030): + t = Period.query.filter(Period.name == period_name).one() + charge = r[f"{period_name}"] + # print(f">{charge}<") + try: + charge = float(charge) + charge = int(100*charge) + except ValueError: + # print(f"Wrong charge {charge}") + charge = 0 + n_c = Charge(agent_id=a.id, + project_id=p.id, + service_id=s.id, + capacity_id=0, + period_id=t.id, + charge_rate=charge) + db.session.add(n_c) + db.session.commit() + + @bp.cli.command("fake_lesia_names") def fake_lesia_names(): """ -- libgit2 0.21.2