Commit 0e188a927a16060c343ac4d8bcc93165f94fed34
1 parent
ad3a824d
Exists in
master
and in
4 other branches
Import more agent fields from irap csv
Showing
1 changed file
with
89 additions
and
16 deletions
Show diff stats
app/commands/commands.py
@@ -12,7 +12,7 @@ from sqlalchemy.ext.automap import automap_base | @@ -12,7 +12,7 @@ from sqlalchemy.ext.automap import automap_base | ||
12 | from sqlalchemy.orm import Session | 12 | from sqlalchemy.orm import Session |
13 | from sqlalchemy import create_engine | 13 | from sqlalchemy import create_engine |
14 | 14 | ||
15 | -from app.models import db, Agent, Service, Project, Capacity, Period, Charge | 15 | +from app.models import db, Agent, Service, Project, Capacity, Period, Charge, AgentStatus, Company, AgentBap, AgentGrade |
16 | from app.auth.models import User, _nameToRole, _roleToName | 16 | from app.auth.models import User, _nameToRole, _roleToName |
17 | 17 | ||
18 | from . import bp | 18 | from . import bp |
@@ -65,6 +65,11 @@ def feed_from_irap(csv_file_name): | @@ -65,6 +65,11 @@ def feed_from_irap(csv_file_name): | ||
65 | 65 | ||
66 | firstname_key = 'NOM' | 66 | firstname_key = 'NOM' |
67 | secondname_key = 'prénom' | 67 | secondname_key = 'prénom' |
68 | + status_key = 'STATUT' | ||
69 | + virtual_key = 'virtuel' | ||
70 | + company_key = 'société' | ||
71 | + bap_key = 'BAP' | ||
72 | + grade_key = 'GRADE CORPS' | ||
68 | project_key = 'PROJETS' | 73 | project_key = 'PROJETS' |
69 | service_key = 'Groupe métier' | 74 | service_key = 'Groupe métier' |
70 | # typology_title = 'TYPOLOGIE' | 75 | # typology_title = 'TYPOLOGIE' |
@@ -72,23 +77,65 @@ def feed_from_irap(csv_file_name): | @@ -72,23 +77,65 @@ def feed_from_irap(csv_file_name): | ||
72 | 77 | ||
73 | # Get the columns values | 78 | # Get the columns values |
74 | # | 79 | # |
75 | - projects = [r[project_key] for r in rows] | 80 | + projects = [] |
81 | + agents = [] | ||
82 | + services = [] | ||
83 | + baps = [] | ||
84 | + grades = [] | ||
85 | + companies = [] | ||
86 | + # thematics = [] | ||
87 | + # typologies = [] | ||
88 | + for r in rows: | ||
89 | + # thematics.push( r[thematic_title]) | ||
90 | + # typologies.push( r[typology_title]) | ||
91 | + projects.append(r[project_key]) | ||
92 | + services.append(r[service_key]) | ||
93 | + baps.append(r[bap_key]) | ||
94 | + grades.append(r[grade_key]) | ||
95 | + companies.append(r[company_key]) | ||
96 | + agents.append({ | ||
97 | + 'firstname': r[firstname_key], | ||
98 | + 'secondname': r[secondname_key], | ||
99 | + 'status': r[status_key], | ||
100 | + 'virtual': r[virtual_key], | ||
101 | + 'company': r[company_key], | ||
102 | + 'bap': r[bap_key], | ||
103 | + 'grade': r[grade_key]}) | ||
104 | + | ||
105 | + # Now, uniq/sort the lists | ||
106 | + # | ||
107 | + # thematics = sorted(set(thematics)) | ||
108 | + # typologies = sorted(set(typologies)) | ||
76 | projects = sorted(set(projects)) | 109 | projects = sorted(set(projects)) |
77 | - agents = [(r[firstname_key], r[secondname_key].strip()) for r in rows] | ||
78 | - agents = sorted(set(agents)) | ||
79 | - services = [r[service_key] for r in rows] | ||
80 | services = sorted(set(services)) | 110 | services = sorted(set(services)) |
111 | + baps = sorted(set(baps)) | ||
112 | + grades = sorted(set(grades)) | ||
113 | + companies = sorted(set(companies)) | ||
81 | 114 | ||
82 | - # thematics = [r[thematic_title] for r in rows] | ||
83 | - # thematics = sorted(set(thematics)) | ||
84 | - # typologies = [r[typology_title] for r in rows] | ||
85 | - # typologies = sorted(set(typologies)) | 115 | + agents = list({(a['firstname'], a['secondname']): a for a in agents}.values()) |
116 | + print(agents) | ||
117 | + for a in agents: | ||
118 | + print(a) | ||
86 | 119 | ||
87 | - # Feed agents from column | 120 | + # Feed baps from column |
88 | # | 121 | # |
89 | - for a in agents: | ||
90 | - n_a = Agent(firstname=a[0], secondname=a[1]) | ||
91 | - db.session.add(n_a) | 122 | + for b in baps: |
123 | + n_b = AgentBap(name=b) | ||
124 | + db.session.add(n_b) | ||
125 | + db.session.commit() | ||
126 | + | ||
127 | + # Feed projects from column | ||
128 | + # | ||
129 | + for g in grades: | ||
130 | + n_g = AgentGrade(name=g) | ||
131 | + db.session.add(n_g) | ||
132 | + db.session.commit() | ||
133 | + | ||
134 | + # Feed projects from column | ||
135 | + # | ||
136 | + for c in companies: | ||
137 | + n_c = Company(name=c) | ||
138 | + db.session.add(n_c) | ||
92 | db.session.commit() | 139 | db.session.commit() |
93 | 140 | ||
94 | # Feed projects from column | 141 | # Feed projects from column |
@@ -115,7 +162,33 @@ def feed_from_irap(csv_file_name): | @@ -115,7 +162,33 @@ def feed_from_irap(csv_file_name): | ||
115 | db.session.commit() | 162 | db.session.commit() |
116 | 163 | ||
117 | # Add one default capacity | 164 | # Add one default capacity |
118 | - db.session.add(Capacity(name="Travailleur")) | 165 | + db.session.add(Capacity(name="Agent")) |
166 | + db.session.commit() | ||
167 | + | ||
168 | + # Feed agents from column | ||
169 | + # | ||
170 | + for a in agents: | ||
171 | + status = AgentStatus.query.filter(AgentStatus.name == a['status']).one_or_none() | ||
172 | + if status is None and a['status']: | ||
173 | + status = AgentStatus(name=a['status']) | ||
174 | + company = Company.query.filter(Company.name == a['company']).one_or_none() | ||
175 | + if company is None and a['company']: | ||
176 | + company = Company(name=a['company']) | ||
177 | + bap = AgentBap.query.filter(AgentBap.name == a['bap']).one_or_none() | ||
178 | + if bap is None and a['bap']: | ||
179 | + bap = AgentBap(name=a['bap']) | ||
180 | + grade = AgentGrade.query.filter(AgentGrade.name == a['grade']).one_or_none() | ||
181 | + if grade is None and a['grade']: | ||
182 | + grade = AgentBap(name=a['grade']) | ||
183 | + virtual = 1 if a['virtual'] else 0 | ||
184 | + n_a = Agent(firstname=a['firstname'], | ||
185 | + secondname=a['secondname'], | ||
186 | + status_id=status.id if status else None, | ||
187 | + company_id=status.id if status else None, | ||
188 | + bap_id=bap.id if bap else None, | ||
189 | + grade_id=grade.id if bap else None, | ||
190 | + virtual=virtual) | ||
191 | + db.session.add(n_a) | ||
119 | db.session.commit() | 192 | db.session.commit() |
120 | 193 | ||
121 | # Now feed the charges. | 194 | # Now feed the charges. |
@@ -351,12 +424,12 @@ def user_update(user_id, name, role, email, password): | @@ -351,12 +424,12 @@ def user_update(user_id, name, role, email, password): | ||
351 | user.set_role(role) | 424 | user.set_role(role) |
352 | print(f"User --{user.name}-- role updated to {_roleToName[user.role]}") | 425 | print(f"User --{user.name}-- role updated to {_roleToName[user.role]}") |
353 | if email: | 426 | if email: |
354 | - user.email=email | 427 | + user.email = email |
355 | print(f"User --{user.name}-- email updated to {user.email}") | 428 | print(f"User --{user.name}-- email updated to {user.email}") |
356 | if password: | 429 | if password: |
357 | print(f"User --{user.name}-- password updated") | 430 | print(f"User --{user.name}-- password updated") |
358 | user.set_password(password) | 431 | user.set_password(password) |
359 | - if not ( name or role or email or password): | 432 | + if not (name or role or email or password): |
360 | print(f"No update for user --{user.name}--") | 433 | print(f"No update for user --{user.name}--") |
361 | db.session.commit() | 434 | db.session.commit() |
362 | 435 |