Commit 74c7dad51ad55aab547be0c5c437987b24ec3fba
1 parent
fed90dbd
Exists in
master
and in
4 other branches
Update irap feed for new model
Showing
1 changed file
with
66 additions
and
35 deletions
Show diff stats
app/commands/commands.py
... | ... | @@ -79,15 +79,22 @@ def feed_from_irap(csv_file_name): |
79 | 79 | grades = [] |
80 | 80 | companies = [] |
81 | 81 | statuses = [] |
82 | + labels = [] | |
82 | 83 | |
83 | - # build a category dict of lists | |
84 | + # Build a category dict of lists | |
84 | 85 | # key being the category name, |
85 | 86 | # list being filled with corresponding labels |
86 | - categories = {k: [] for k in categorie_keys} | |
87 | + categorie_labels = {k: [] for k in categorie_keys} | |
87 | 88 | |
89 | + # Projects' labels is a dict of lists | |
90 | + # indexed by project name | |
91 | + # containing labels for that project | |
88 | 92 | # |
89 | - categorized_projects = {} | |
93 | + project_labels = {} | |
90 | 94 | |
95 | + # | |
96 | + # Parse the rows and fill in various lists | |
97 | + # | |
91 | 98 | for r in rows: |
92 | 99 | services.append(r[service_key]) |
93 | 100 | baps.append(r[bap_key]) |
... | ... | @@ -95,16 +102,20 @@ def feed_from_irap(csv_file_name): |
95 | 102 | companies.append(r[company_key]) |
96 | 103 | statuses.append(r[status_key]) |
97 | 104 | |
98 | - # categorized_projects | |
99 | - project = r[project_key] | |
100 | - categorized_projects[project] = [] | |
105 | + # the projet and its labels | |
106 | + project_name = r[project_key] | |
107 | + project_labels[project_name] = [] | |
101 | 108 | |
102 | - # fill in the category-labels dict | |
109 | + # now fill in both | |
110 | + # the labels list, | |
111 | + # the category-labels dict, | |
112 | + # and the project-labels dict | |
103 | 113 | for k in categorie_keys: |
104 | - categories[k].append(r[k]) | |
105 | - categorized_projects[project].append(r[k]) | |
114 | + labels.append(r[k]) | |
115 | + categorie_labels[k].append(r[k]) | |
116 | + project_labels[project_name].append(r[k]) | |
106 | 117 | |
107 | - # build agents list of dicts | |
118 | + # create the agents list of dicts | |
108 | 119 | agents.append({ |
109 | 120 | 'firstname': r[firstname_key], |
110 | 121 | 'secondname': r[secondname_key], |
... | ... | @@ -124,25 +135,25 @@ def feed_from_irap(csv_file_name): |
124 | 135 | # |
125 | 136 | # 1- first remove empty string with filter() |
126 | 137 | # 2- then keep only uniq item with set() |
127 | - # 3- at last alpha sort with sorted() | |
138 | + # 3- at last alpha sort with sorted() | |
128 | 139 | # |
129 | 140 | services = sorted(set(filter(None, services))) |
130 | 141 | baps = sorted(set(filter(None, baps))) |
131 | 142 | grades = sorted(set(filter(None, grades))) |
132 | 143 | companies = sorted(set(filter(None, companies))) |
133 | 144 | statuses = sorted(set(filter(None, statuses))) |
145 | + labels = sorted(set(filter(None, labels))) | |
134 | 146 | |
135 | - # Do the same for the projects, that are keys of categorized_projects | |
147 | + # Do the same for the projects, that are keys of project_labels | |
136 | 148 | # |
137 | - projects = sorted(set(categorized_projects.keys())) | |
149 | + projects = sorted(set(project_labels.keys())) | |
138 | 150 | |
139 | - # Do the same for the category labels stored as a dict | |
151 | + # Do the same for the labels inside each category | |
152 | + # c is the category name containing the labels list | |
140 | 153 | # |
141 | - # k is category name | |
142 | - # v is labels list for that category | |
143 | - for k in categorie_keys: | |
144 | - labels = sorted(set(filter(None, categories[k]))) | |
145 | - categories[k] = labels | |
154 | + for c in categorie_keys: | |
155 | + c_labels = sorted(set(filter(None, categorie_labels[c]))) | |
156 | + categorie_labels[c] = c_labels | |
146 | 157 | |
147 | 158 | # At least, as agents is a list of dicts, sorting is a bit tricky |
148 | 159 | # |
... | ... | @@ -153,6 +164,12 @@ def feed_from_irap(csv_file_name): |
153 | 164 | agents = list({(a['firstname'], a['secondname']): a for a in agents}.values()) |
154 | 165 | agents = sorted(agents, key=lambda a: (a['firstname'], a['secondname'])) |
155 | 166 | |
167 | + # | |
168 | + # We are done with collecting data | |
169 | + # | |
170 | + # Now we write to database | |
171 | + # | |
172 | + | |
156 | 173 | # Feed baps from column |
157 | 174 | # |
158 | 175 | for b in baps: |
... | ... | @@ -188,6 +205,20 @@ def feed_from_irap(csv_file_name): |
188 | 205 | db.session.add(n_p) |
189 | 206 | db.session.commit() |
190 | 207 | |
208 | + # Feed labels from column | |
209 | + # | |
210 | + for _l in labels: | |
211 | + n_l = Label(name=_l) | |
212 | + db.session.add(n_l) | |
213 | + db.session.commit() | |
214 | + | |
215 | + # Feed categories from initial list | |
216 | + # | |
217 | + for _c in categorie_keys: | |
218 | + n_c = Category(name=_c) | |
219 | + db.session.add(n_c) | |
220 | + db.session.commit() | |
221 | + | |
191 | 222 | # Feed services from column |
192 | 223 | # |
193 | 224 | for s in services: |
... | ... | @@ -212,25 +243,25 @@ def feed_from_irap(csv_file_name): |
212 | 243 | |
213 | 244 | # Feed categories and labels |
214 | 245 | # |
215 | - for c, l in categories.items(): | |
216 | - current_app.logger.debug("Adding category " + c) | |
217 | - n_c = Category(name=c.capitalize()) | |
218 | - db.session.add(n_c) | |
219 | - # db.session.commit() | |
220 | - for label in l: | |
221 | - current_app.logger.debug("Adding label {} for id {}".format(label, n_c.id)) | |
222 | - n_l = Label(name=label, category=n_c) | |
223 | - db.session.add(n_l) | |
246 | + for category, labels in categorie_labels.items(): | |
247 | + print(category) | |
248 | + n_c = Category.query.filter_by(name=category).one() | |
249 | + for label in labels: | |
250 | + print(label) | |
251 | + n_l = Label.query.filter(Label.name == label).one() | |
252 | + current_app.logger.debug(f"Adding label {label} to category {category}") | |
253 | + n_cl = CategoryLabel(label=n_l, category=n_c) | |
254 | + db.session.add(n_cl) | |
224 | 255 | db.session.commit() |
225 | 256 | |
226 | 257 | # Feed project's labels |
227 | 258 | # |
228 | - for p, labels in categorized_projects.items(): | |
229 | - n_p = Project.query.filter(Project.name == p).one() | |
230 | - for l in labels: | |
231 | - n_l = Label.query.filter(Label.name == l).one() | |
232 | - n_c = n_l.category | |
233 | - n_pl = ProjectLabel(project=n_p, category=n_c, label=n_l) | |
259 | + for project, labels in project_labels.items(): | |
260 | + print(f"Project {project}") | |
261 | + n_p = Project.query.filter(Project.name == project).one() | |
262 | + for label in labels: | |
263 | + n_l = Label.query.filter(Label.name == label).one() | |
264 | + n_pl = ProjectLabel(project=n_p, label=n_l) | |
234 | 265 | db.session.add(n_pl) |
235 | 266 | db.session.commit() |
236 | 267 | |
... | ... | @@ -314,7 +345,7 @@ def feed_from_lesia(): |
314 | 345 | domains = lesia_session.query(lesia_domains) |
315 | 346 | for d in domains: |
316 | 347 | n_l = Label(name=d.nom) |
317 | - n_cl = CategoryLabel(category=domain_category, label=n_l ) | |
348 | + n_cl = CategoryLabel(category=domain_category, label=n_l) | |
318 | 349 | db.session.add(n_cl) |
319 | 350 | db.session.commit() |
320 | 351 | ... | ... |