Commit 28a7e0a23c0a89f96225d29a0494cc5ca04e1b10

Authored by hitier
1 parent 67814e41

Strip row values the soonest and once

When importing irap's csv file, remove leading and trailing spaces to
any imported cell.
Showing 1 changed file with 29 additions and 26 deletions   Show diff stats
app/commands/commands.py
... ... @@ -35,38 +35,47 @@ def feed_from_irap(csv_file_name):
35 35 for row in csvreader:
36 36 # print('\n'.join(row.keys()))
37 37 # break
  38 + # Remove any leading/trailing spaces
  39 + row = {k: v.strip() for k, v in row.items()}
38 40 rows.append(row)
39 41  
40   - # todo: service_title
41   - # project_tile
42   - # typology_tile
43   - # thematic_tile
44   - # agent_tile
  42 + firstname_key = 'NOM'
  43 + secondname_key = 'prénom'
  44 + project_key = 'PROJETS'
  45 + service_key = 'Groupe métier'
  46 + typology_title = 'TYPOLOGIE'
  47 + thematic_title = 'thématique'
45 48  
46   - projects = [r['PROJETS'].strip() for r in rows]
  49 + # Get the columns values
  50 + #
  51 + projects = [r[project_key] for r in rows]
47 52 projects = sorted(set(projects))
48   - agents = [(r['NOM'].strip(), r['prénom'].strip()) for r in rows]
  53 + agents = [(r[firstname_key], r[secondname_key].strip()) for r in rows]
49 54 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 = [r[service_key] for r in rows]
55 56 services = sorted(set(services))
56 57  
  58 + # thematics = [r[thematic_title] for r in rows]
  59 + # thematics = sorted(set(thematics))
  60 + # typologies = [r[typology_title] for r in rows]
  61 + # typologies = sorted(set(typologies))
  62 +
  63 + # Feed agents from column
  64 + #
57 65 for a in agents:
58 66 n_a = Agent(firstname=a[0], secondname=a[1])
59 67 db.session.add(n_a)
60 68 db.session.commit()
61   - # a = Agent.query.filter(Agent.firstname == 'ESPAIGNOL').one()
62   - # print(f">{a.secondname}<")
63   - # sys.exit()
64 69  
  70 + # Feed projects from column
  71 + #
65 72 for p in projects:
66 73 n_p = Project(name=p)
67 74 db.session.add(n_p)
68 75 db.session.commit()
69 76  
  77 + # Feed services from column
  78 + #
70 79 for s in services:
71 80 n_s = Service(name=s)
72 81 db.session.add(n_s)
... ... @@ -78,27 +87,21 @@ def feed_from_irap(csv_file_name):
78 87 db.session.commit()
79 88  
80 89 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()
  90 + p = Project.query.filter(Project.name == r[project_key]).one()
  91 + a = Agent.query.filter(Agent.firstname == r[firstname_key], Agent.secondname == r[secondname_key]).one()
  92 + s = Service.query.filter(Service.name == r[service_key]).one()
88 93 for period_name in range(2011, 2030):
89 94 t = Period.query.filter(Period.name == period_name).one()
90 95 charge = r[f"{period_name}"]
91   - # print(f">{charge}<")
92 96 try:
93   - charge = float(charge)
94   - charge = int(100*charge)
  97 + charge = int(100 * float(charge))
95 98 except ValueError:
96 99 # print(f"Wrong charge {charge}")
97 100 charge = 0
98 101 n_c = Charge(agent_id=a.id,
99 102 project_id=p.id,
100 103 service_id=s.id,
101   - capacity_id=0,
  104 + # capacity_id=0,
102 105 period_id=t.id,
103 106 charge_rate=charge)
104 107 db.session.add(n_c)
... ...