From 28a7e0a23c0a89f96225d29a0494cc5ca04e1b10 Mon Sep 17 00:00:00 2001 From: Richard Hitier Date: Wed, 14 Apr 2021 10:32:48 +0200 Subject: [PATCH] Strip row values the soonest and once --- app/commands/commands.py | 55 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/app/commands/commands.py b/app/commands/commands.py index d715cc7..3d91b85 100644 --- a/app/commands/commands.py +++ b/app/commands/commands.py @@ -35,38 +35,47 @@ def feed_from_irap(csv_file_name): for row in csvreader: # print('\n'.join(row.keys())) # break + # Remove any leading/trailing spaces + row = {k: v.strip() for k, v in row.items()} rows.append(row) - # todo: service_title - # project_tile - # typology_tile - # thematic_tile - # agent_tile + firstname_key = 'NOM' + secondname_key = 'prénom' + project_key = 'PROJETS' + service_key = 'Groupe métier' + typology_title = 'TYPOLOGIE' + thematic_title = 'thématique' - projects = [r['PROJETS'].strip() for r in rows] + # Get the columns values + # + projects = [r[project_key] for r in rows] projects = sorted(set(projects)) - agents = [(r['NOM'].strip(), r['prénom'].strip()) for r in rows] + agents = [(r[firstname_key], r[secondname_key].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 = [r[service_key] for r in rows] services = sorted(set(services)) + # thematics = [r[thematic_title] for r in rows] + # thematics = sorted(set(thematics)) + # typologies = [r[typology_title] for r in rows] + # typologies = sorted(set(typologies)) + + # Feed agents from column + # 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() + # Feed projects from column + # for p in projects: n_p = Project(name=p) db.session.add(n_p) db.session.commit() + # Feed services from column + # for s in services: n_s = Service(name=s) db.session.add(n_s) @@ -78,27 +87,21 @@ def feed_from_irap(csv_file_name): 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() + p = Project.query.filter(Project.name == r[project_key]).one() + a = Agent.query.filter(Agent.firstname == r[firstname_key], Agent.secondname == r[secondname_key]).one() + s = Service.query.filter(Service.name == r[service_key]).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) + charge = int(100 * float(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, + # capacity_id=0, period_id=t.id, charge_rate=charge) db.session.add(n_c) -- libgit2 0.21.2