From d6b9daca80dc8cfbe1348cf48458f197eb5ff4ac Mon Sep 17 00:00:00 2001 From: Richard Hitier Date: Tue, 27 Apr 2021 16:58:26 +0200 Subject: [PATCH] Feed categories and labels --- app/commands/commands.py | 41 +++++++++++++++++++++++++++++++---------- app/models.py | 11 +++++++++++ 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/app/commands/commands.py b/app/commands/commands.py index f62f68b..46d1998 100644 --- a/app/commands/commands.py +++ b/app/commands/commands.py @@ -12,7 +12,8 @@ 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, AgentStatus, Company, AgentBap, AgentGrade +from app.models import db, Agent, Service, Project, Capacity, Period, Charge, AgentStatus, Company, AgentBap, \ + AgentGrade, Category, CategoryLabel from app.auth.models import User, _nameToRole, _roleToName from . import bp @@ -70,7 +71,7 @@ def feed_from_irap(csv_file_name): grade_key = 'GRADE CORPS' project_key = 'PROJETS' service_key = 'GROUPE MÉTIER' - categorie_keys = ['TYPOLOGIE', 'THÉMATIQUE'] + categorie_keys = ['TYPOLOGIE', 'THÉMATIQUE'] # Get the columns values # @@ -81,24 +82,25 @@ def feed_from_irap(csv_file_name): grades = [] companies = [] statuses = [] + categories = {k: [] for k in categorie_keys} 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]) statuses.append(r[status_key]) - agent_dict = { + # build a dict of lists, key being the category name + for k in categorie_keys: + categories[k].append(r[k]) + 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]} - agents.append(agent_dict) + 'grade': r[grade_key]}) # Uppercase the small tables # @@ -112,8 +114,6 @@ def feed_from_irap(csv_file_name): # 2- then keep only uniq item with set() # 3- at last alpha sort with sorted() # - # thematics = sorted(set(thematics)) - # typologies = sorted(set(typologies)) projects = sorted(set(filter(None, projects))) services = sorted(set(filter(None, services))) baps = sorted(set(filter(None, baps))) @@ -121,7 +121,15 @@ def feed_from_irap(csv_file_name): companies = sorted(set(filter(None, companies))) statuses = sorted(set(filter(None, statuses))) - # as agents is a list of dicts, sorting is a bit tricky + # Do the same for the category labels stored as a dict + # + # 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 + + # At least, as agents is a list of dicts, sorting is a bit tricky # # the first one liner will store the last agent's line only # then we alpha sort on the name @@ -186,6 +194,19 @@ def feed_from_irap(csv_file_name): db.session.add(Capacity(name="Agent")) db.session.commit() + # Feed categories and labels + # + for c, l in categories.items(): + print("Adding category ", c) + n_c = Category(name=c) + db.session.add(n_c) + db.session.commit() + for label in l: + print("Adding label {} for id {}".format(label, n_c.id)) + n_l = CategoryLabel(name=label, category_id=n_c.id) + db.session.add(n_l) + db.session.commit() + # Feed agents from columns # for a in agents: diff --git a/app/models.py b/app/models.py index 385c164..d1443d7 100644 --- a/app/models.py +++ b/app/models.py @@ -3,6 +3,17 @@ from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() +class Category(db.Model): + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String(32)) + + +class CategoryLabel(db.Model): + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String(32)) + category_id = db.Column(db.Integer, db.ForeignKey('category.id')) + + class AgentBap(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(16)) -- libgit2 0.21.2