diff --git a/app/commands/commands.py b/app/commands/commands.py index abcd95d..0d63d8f 100644 --- a/app/commands/commands.py +++ b/app/commands/commands.py @@ -79,15 +79,22 @@ def feed_from_irap(csv_file_name): grades = [] companies = [] statuses = [] + labels = [] - # build a category dict of lists + # Build a category dict of lists # key being the category name, # list being filled with corresponding labels - categories = {k: [] for k in categorie_keys} + categorie_labels = {k: [] for k in categorie_keys} + # Projects' labels is a dict of lists + # indexed by project name + # containing labels for that project # - categorized_projects = {} + project_labels = {} + # + # Parse the rows and fill in various lists + # for r in rows: services.append(r[service_key]) baps.append(r[bap_key]) @@ -95,16 +102,20 @@ def feed_from_irap(csv_file_name): companies.append(r[company_key]) statuses.append(r[status_key]) - # categorized_projects - project = r[project_key] - categorized_projects[project] = [] + # the projet and its labels + project_name = r[project_key] + project_labels[project_name] = [] - # fill in the category-labels dict + # now fill in both + # the labels list, + # the category-labels dict, + # and the project-labels dict for k in categorie_keys: - categories[k].append(r[k]) - categorized_projects[project].append(r[k]) + labels.append(r[k]) + categorie_labels[k].append(r[k]) + project_labels[project_name].append(r[k]) - # build agents list of dicts + # create the agents list of dicts agents.append({ 'firstname': r[firstname_key], 'secondname': r[secondname_key], @@ -124,25 +135,25 @@ def feed_from_irap(csv_file_name): # # 1- first remove empty string with filter() # 2- then keep only uniq item with set() - # 3- at last alpha sort with sorted() + # 3- at last alpha sort with sorted() # services = sorted(set(filter(None, services))) baps = sorted(set(filter(None, baps))) grades = sorted(set(filter(None, grades))) companies = sorted(set(filter(None, companies))) statuses = sorted(set(filter(None, statuses))) + labels = sorted(set(filter(None, labels))) - # Do the same for the projects, that are keys of categorized_projects + # Do the same for the projects, that are keys of project_labels # - projects = sorted(set(categorized_projects.keys())) + projects = sorted(set(project_labels.keys())) - # Do the same for the category labels stored as a dict + # Do the same for the labels inside each category + # c is the category name containing the labels list # - # k is category name - # v is labels list for that category - for k in categorie_keys: - labels = sorted(set(filter(None, categories[k]))) - categories[k] = labels + for c in categorie_keys: + c_labels = sorted(set(filter(None, categorie_labels[c]))) + categorie_labels[c] = c_labels # At least, as agents is a list of dicts, sorting is a bit tricky # @@ -153,6 +164,12 @@ def feed_from_irap(csv_file_name): agents = list({(a['firstname'], a['secondname']): a for a in agents}.values()) agents = sorted(agents, key=lambda a: (a['firstname'], a['secondname'])) + # + # We are done with collecting data + # + # Now we write to database + # + # Feed baps from column # for b in baps: @@ -188,6 +205,20 @@ def feed_from_irap(csv_file_name): db.session.add(n_p) db.session.commit() + # Feed labels from column + # + for _l in labels: + n_l = Label(name=_l) + db.session.add(n_l) + db.session.commit() + + # Feed categories from initial list + # + for _c in categorie_keys: + n_c = Category(name=_c) + db.session.add(n_c) + db.session.commit() + # Feed services from column # for s in services: @@ -212,25 +243,25 @@ def feed_from_irap(csv_file_name): # Feed categories and labels # - for c, l in categories.items(): - current_app.logger.debug("Adding category " + c) - n_c = Category(name=c.capitalize()) - db.session.add(n_c) - # db.session.commit() - for label in l: - current_app.logger.debug("Adding label {} for id {}".format(label, n_c.id)) - n_l = Label(name=label, category=n_c) - db.session.add(n_l) + for category, labels in categorie_labels.items(): + print(category) + n_c = Category.query.filter_by(name=category).one() + for label in labels: + print(label) + n_l = Label.query.filter(Label.name == label).one() + current_app.logger.debug(f"Adding label {label} to category {category}") + n_cl = CategoryLabel(label=n_l, category=n_c) + db.session.add(n_cl) db.session.commit() # Feed project's labels # - for p, labels in categorized_projects.items(): - n_p = Project.query.filter(Project.name == p).one() - for l in labels: - n_l = Label.query.filter(Label.name == l).one() - n_c = n_l.category - n_pl = ProjectLabel(project=n_p, category=n_c, label=n_l) + for project, labels in project_labels.items(): + print(f"Project {project}") + n_p = Project.query.filter(Project.name == project).one() + for label in labels: + n_l = Label.query.filter(Label.name == label).one() + n_pl = ProjectLabel(project=n_p, label=n_l) db.session.add(n_pl) db.session.commit() @@ -314,7 +345,7 @@ def feed_from_lesia(): domains = lesia_session.query(lesia_domains) for d in domains: n_l = Label(name=d.nom) - n_cl = CategoryLabel(category=domain_category, label=n_l ) + n_cl = CategoryLabel(category=domain_category, label=n_l) db.session.add(n_cl) db.session.commit() -- libgit2 0.21.2