From 0e188a927a16060c343ac4d8bcc93165f94fed34 Mon Sep 17 00:00:00 2001 From: Richard Hitier Date: Mon, 26 Apr 2021 17:16:59 +0200 Subject: [PATCH] Import more agent fields from irap csv --- app/commands/commands.py | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 89 insertions(+), 16 deletions(-) diff --git a/app/commands/commands.py b/app/commands/commands.py index 7cde95d..1aee668 100644 --- a/app/commands/commands.py +++ b/app/commands/commands.py @@ -12,7 +12,7 @@ from sqlalchemy.ext.automap import automap_base from sqlalchemy.orm import Session from sqlalchemy import create_engine -from app.models import db, Agent, Service, Project, Capacity, Period, Charge +from app.models import db, Agent, Service, Project, Capacity, Period, Charge, AgentStatus, Company, AgentBap, AgentGrade from app.auth.models import User, _nameToRole, _roleToName from . import bp @@ -65,6 +65,11 @@ def feed_from_irap(csv_file_name): firstname_key = 'NOM' secondname_key = 'prénom' + status_key = 'STATUT' + virtual_key = 'virtuel' + company_key = 'société' + bap_key = 'BAP' + grade_key = 'GRADE CORPS' project_key = 'PROJETS' service_key = 'Groupe métier' # typology_title = 'TYPOLOGIE' @@ -72,23 +77,65 @@ def feed_from_irap(csv_file_name): # Get the columns values # - projects = [r[project_key] for r in rows] + projects = [] + agents = [] + services = [] + baps = [] + grades = [] + companies = [] + # thematics = [] + # typologies = [] + for r in rows: + # thematics.push( r[thematic_title]) + # typologies.push( r[typology_title]) + projects.append(r[project_key]) + services.append(r[service_key]) + baps.append(r[bap_key]) + grades.append(r[grade_key]) + companies.append(r[company_key]) + agents.append({ + 'firstname': r[firstname_key], + 'secondname': r[secondname_key], + 'status': r[status_key], + 'virtual': r[virtual_key], + 'company': r[company_key], + 'bap': r[bap_key], + 'grade': r[grade_key]}) + + # Now, uniq/sort the lists + # + # thematics = sorted(set(thematics)) + # typologies = sorted(set(typologies)) projects = sorted(set(projects)) - agents = [(r[firstname_key], r[secondname_key].strip()) for r in rows] - agents = sorted(set(agents)) - services = [r[service_key] for r in rows] services = sorted(set(services)) + baps = sorted(set(baps)) + grades = sorted(set(grades)) + companies = sorted(set(companies)) - # thematics = [r[thematic_title] for r in rows] - # thematics = sorted(set(thematics)) - # typologies = [r[typology_title] for r in rows] - # typologies = sorted(set(typologies)) + agents = list({(a['firstname'], a['secondname']): a for a in agents}.values()) + print(agents) + for a in agents: + print(a) - # Feed agents from column + # Feed baps from column # - for a in agents: - n_a = Agent(firstname=a[0], secondname=a[1]) - db.session.add(n_a) + for b in baps: + n_b = AgentBap(name=b) + db.session.add(n_b) + db.session.commit() + + # Feed projects from column + # + for g in grades: + n_g = AgentGrade(name=g) + db.session.add(n_g) + db.session.commit() + + # Feed projects from column + # + for c in companies: + n_c = Company(name=c) + db.session.add(n_c) db.session.commit() # Feed projects from column @@ -115,7 +162,33 @@ def feed_from_irap(csv_file_name): db.session.commit() # Add one default capacity - db.session.add(Capacity(name="Travailleur")) + db.session.add(Capacity(name="Agent")) + db.session.commit() + + # Feed agents from column + # + for a in agents: + status = AgentStatus.query.filter(AgentStatus.name == a['status']).one_or_none() + if status is None and a['status']: + status = AgentStatus(name=a['status']) + company = Company.query.filter(Company.name == a['company']).one_or_none() + if company is None and a['company']: + company = Company(name=a['company']) + bap = AgentBap.query.filter(AgentBap.name == a['bap']).one_or_none() + if bap is None and a['bap']: + bap = AgentBap(name=a['bap']) + grade = AgentGrade.query.filter(AgentGrade.name == a['grade']).one_or_none() + if grade is None and a['grade']: + grade = AgentBap(name=a['grade']) + virtual = 1 if a['virtual'] else 0 + n_a = Agent(firstname=a['firstname'], + secondname=a['secondname'], + status_id=status.id if status else None, + company_id=status.id if status else None, + bap_id=bap.id if bap else None, + grade_id=grade.id if bap else None, + virtual=virtual) + db.session.add(n_a) db.session.commit() # Now feed the charges. @@ -351,12 +424,12 @@ def user_update(user_id, name, role, email, password): user.set_role(role) print(f"User --{user.name}-- role updated to {_roleToName[user.role]}") if email: - user.email=email + user.email = email print(f"User --{user.name}-- email updated to {user.email}") if password: print(f"User --{user.name}-- password updated") user.set_password(password) - if not ( name or role or email or password): + if not (name or role or email or password): print(f"No update for user --{user.name}--") db.session.commit() -- libgit2 0.21.2