Commit 854e5e733344d9b8e3bd2a47224f0c40df26f23a

Authored by hitier
1 parent 0e188a92

Feed statuses, sort agents

Showing 1 changed file with 22 additions and 7 deletions   Show diff stats
app/commands/commands.py
... ... @@ -83,6 +83,7 @@ def feed_from_irap(csv_file_name):
83 83 baps = []
84 84 grades = []
85 85 companies = []
  86 + statuses = []
86 87 # thematics = []
87 88 # typologies = []
88 89 for r in rows:
... ... @@ -93,14 +94,16 @@ def feed_from_irap(csv_file_name):
93 94 baps.append(r[bap_key])
94 95 grades.append(r[grade_key])
95 96 companies.append(r[company_key])
96   - agents.append({
  97 + statuses.append(r[status_key])
  98 + agent_dict = {
97 99 'firstname': r[firstname_key],
98 100 'secondname': r[secondname_key],
99 101 'status': r[status_key],
100 102 'virtual': r[virtual_key],
101 103 'company': r[company_key],
102 104 'bap': r[bap_key],
103   - 'grade': r[grade_key]})
  105 + 'grade': r[grade_key]}
  106 + agents.append(agent_dict)
104 107  
105 108 # Now, uniq/sort the lists
106 109 #
... ... @@ -111,11 +114,16 @@ def feed_from_irap(csv_file_name):
111 114 baps = sorted(set(baps))
112 115 grades = sorted(set(grades))
113 116 companies = sorted(set(companies))
  117 + statuses = sorted(set(statuses))
114 118  
  119 + # as agents is a list of dicts, sorting is a bit tricky
  120 + #
  121 + # the first one liner will store the last agent's line only
  122 + # then we alpha sort on the name
  123 + # on both, the discrimination is based on the name couple: (firstname, secondname)
  124 + #
115 125 agents = list({(a['firstname'], a['secondname']): a for a in agents}.values())
116   - print(agents)
117   - for a in agents:
118   - print(a)
  126 + agents = sorted(agents, key=lambda a: (a['firstname'], a['secondname']))
119 127  
120 128 # Feed baps from column
121 129 #
... ... @@ -138,6 +146,13 @@ def feed_from_irap(csv_file_name):
138 146 db.session.add(n_c)
139 147 db.session.commit()
140 148  
  149 + # Feed statuses from column
  150 + #
  151 + for s in statuses:
  152 + n_s = AgentStatus(name=s)
  153 + db.session.add(n_s)
  154 + db.session.commit()
  155 +
141 156 # Feed projects from column
142 157 #
143 158 for p in projects:
... ... @@ -165,7 +180,7 @@ def feed_from_irap(csv_file_name):
165 180 db.session.add(Capacity(name="Agent"))
166 181 db.session.commit()
167 182  
168   - # Feed agents from column
  183 + # Feed agents from columns
169 184 #
170 185 for a in agents:
171 186 status = AgentStatus.query.filter(AgentStatus.name == a['status']).one_or_none()
... ... @@ -184,7 +199,7 @@ def feed_from_irap(csv_file_name):
184 199 n_a = Agent(firstname=a['firstname'],
185 200 secondname=a['secondname'],
186 201 status_id=status.id if status else None,
187   - company_id=status.id if status else None,
  202 + company_id=company.id if company else None,
188 203 bap_id=bap.id if bap else None,
189 204 grade_id=grade.id if bap else None,
190 205 virtual=virtual)
... ...