Commit d6b9daca80dc8cfbe1348cf48458f197eb5ff4ac
1 parent
2d944bab
Exists in
master
and in
4 other branches
Feed categories and labels
Showing
2 changed files
with
42 additions
and
10 deletions
Show diff stats
app/commands/commands.py
@@ -12,7 +12,8 @@ from sqlalchemy.ext.automap import automap_base | @@ -12,7 +12,8 @@ from sqlalchemy.ext.automap import automap_base | ||
12 | from sqlalchemy.orm import Session | 12 | from sqlalchemy.orm import Session |
13 | from sqlalchemy import create_engine | 13 | from sqlalchemy import create_engine |
14 | 14 | ||
15 | -from app.models import db, Agent, Service, Project, Capacity, Period, Charge, AgentStatus, Company, AgentBap, AgentGrade | 15 | +from app.models import db, Agent, Service, Project, Capacity, Period, Charge, AgentStatus, Company, AgentBap, \ |
16 | + AgentGrade, Category, CategoryLabel | ||
16 | from app.auth.models import User, _nameToRole, _roleToName | 17 | from app.auth.models import User, _nameToRole, _roleToName |
17 | 18 | ||
18 | from . import bp | 19 | from . import bp |
@@ -70,7 +71,7 @@ def feed_from_irap(csv_file_name): | @@ -70,7 +71,7 @@ def feed_from_irap(csv_file_name): | ||
70 | grade_key = 'GRADE CORPS' | 71 | grade_key = 'GRADE CORPS' |
71 | project_key = 'PROJETS' | 72 | project_key = 'PROJETS' |
72 | service_key = 'GROUPE MÉTIER' | 73 | service_key = 'GROUPE MÉTIER' |
73 | - categorie_keys = ['TYPOLOGIE', 'THÉMATIQUE'] | 74 | + categorie_keys = ['TYPOLOGIE', 'THÉMATIQUE'] |
74 | 75 | ||
75 | # Get the columns values | 76 | # Get the columns values |
76 | # | 77 | # |
@@ -81,24 +82,25 @@ def feed_from_irap(csv_file_name): | @@ -81,24 +82,25 @@ def feed_from_irap(csv_file_name): | ||
81 | grades = [] | 82 | grades = [] |
82 | companies = [] | 83 | companies = [] |
83 | statuses = [] | 84 | statuses = [] |
85 | + categories = {k: [] for k in categorie_keys} | ||
84 | for r in rows: | 86 | for r in rows: |
85 | - # thematics.push( r[thematic_title]) | ||
86 | - # typologies.push( r[typology_title]) | ||
87 | projects.append(r[project_key]) | 87 | projects.append(r[project_key]) |
88 | services.append(r[service_key]) | 88 | services.append(r[service_key]) |
89 | baps.append(r[bap_key]) | 89 | baps.append(r[bap_key]) |
90 | grades.append(r[grade_key]) | 90 | grades.append(r[grade_key]) |
91 | companies.append(r[company_key]) | 91 | companies.append(r[company_key]) |
92 | statuses.append(r[status_key]) | 92 | statuses.append(r[status_key]) |
93 | - agent_dict = { | 93 | + # build a dict of lists, key being the category name |
94 | + for k in categorie_keys: | ||
95 | + categories[k].append(r[k]) | ||
96 | + agents.append({ | ||
94 | 'firstname': r[firstname_key], | 97 | 'firstname': r[firstname_key], |
95 | 'secondname': r[secondname_key], | 98 | 'secondname': r[secondname_key], |
96 | 'status': r[status_key], | 99 | 'status': r[status_key], |
97 | 'virtual': r[virtual_key], | 100 | 'virtual': r[virtual_key], |
98 | 'company': r[company_key], | 101 | 'company': r[company_key], |
99 | 'bap': r[bap_key], | 102 | 'bap': r[bap_key], |
100 | - 'grade': r[grade_key]} | ||
101 | - agents.append(agent_dict) | 103 | + 'grade': r[grade_key]}) |
102 | 104 | ||
103 | # Uppercase the small tables | 105 | # Uppercase the small tables |
104 | # | 106 | # |
@@ -112,8 +114,6 @@ def feed_from_irap(csv_file_name): | @@ -112,8 +114,6 @@ def feed_from_irap(csv_file_name): | ||
112 | # 2- then keep only uniq item with set() | 114 | # 2- then keep only uniq item with set() |
113 | # 3- at last alpha sort with sorted() | 115 | # 3- at last alpha sort with sorted() |
114 | # | 116 | # |
115 | - # thematics = sorted(set(thematics)) | ||
116 | - # typologies = sorted(set(typologies)) | ||
117 | projects = sorted(set(filter(None, projects))) | 117 | projects = sorted(set(filter(None, projects))) |
118 | services = sorted(set(filter(None, services))) | 118 | services = sorted(set(filter(None, services))) |
119 | baps = sorted(set(filter(None, baps))) | 119 | baps = sorted(set(filter(None, baps))) |
@@ -121,7 +121,15 @@ def feed_from_irap(csv_file_name): | @@ -121,7 +121,15 @@ def feed_from_irap(csv_file_name): | ||
121 | companies = sorted(set(filter(None, companies))) | 121 | companies = sorted(set(filter(None, companies))) |
122 | statuses = sorted(set(filter(None, statuses))) | 122 | statuses = sorted(set(filter(None, statuses))) |
123 | 123 | ||
124 | - # as agents is a list of dicts, sorting is a bit tricky | 124 | + # Do the same for the category labels stored as a dict |
125 | + # | ||
126 | + # k is category name | ||
127 | + # v is labels list for that category | ||
128 | + for k in categorie_keys: | ||
129 | + labels = sorted(set(filter(None, categories[k]))) | ||
130 | + categories[k] = labels | ||
131 | + | ||
132 | + # At least, as agents is a list of dicts, sorting is a bit tricky | ||
125 | # | 133 | # |
126 | # the first one liner will store the last agent's line only | 134 | # the first one liner will store the last agent's line only |
127 | # then we alpha sort on the name | 135 | # then we alpha sort on the name |
@@ -186,6 +194,19 @@ def feed_from_irap(csv_file_name): | @@ -186,6 +194,19 @@ def feed_from_irap(csv_file_name): | ||
186 | db.session.add(Capacity(name="Agent")) | 194 | db.session.add(Capacity(name="Agent")) |
187 | db.session.commit() | 195 | db.session.commit() |
188 | 196 | ||
197 | + # Feed categories and labels | ||
198 | + # | ||
199 | + for c, l in categories.items(): | ||
200 | + print("Adding category ", c) | ||
201 | + n_c = Category(name=c) | ||
202 | + db.session.add(n_c) | ||
203 | + db.session.commit() | ||
204 | + for label in l: | ||
205 | + print("Adding label {} for id {}".format(label, n_c.id)) | ||
206 | + n_l = CategoryLabel(name=label, category_id=n_c.id) | ||
207 | + db.session.add(n_l) | ||
208 | + db.session.commit() | ||
209 | + | ||
189 | # Feed agents from columns | 210 | # Feed agents from columns |
190 | # | 211 | # |
191 | for a in agents: | 212 | for a in agents: |
app/models.py
@@ -3,6 +3,17 @@ from flask_sqlalchemy import SQLAlchemy | @@ -3,6 +3,17 @@ from flask_sqlalchemy import SQLAlchemy | ||
3 | db = SQLAlchemy() | 3 | db = SQLAlchemy() |
4 | 4 | ||
5 | 5 | ||
6 | +class Category(db.Model): | ||
7 | + id = db.Column(db.Integer, primary_key=True) | ||
8 | + name = db.Column(db.String(32)) | ||
9 | + | ||
10 | + | ||
11 | +class CategoryLabel(db.Model): | ||
12 | + id = db.Column(db.Integer, primary_key=True) | ||
13 | + name = db.Column(db.String(32)) | ||
14 | + category_id = db.Column(db.Integer, db.ForeignKey('category.id')) | ||
15 | + | ||
16 | + | ||
6 | class AgentBap(db.Model): | 17 | class AgentBap(db.Model): |
7 | id = db.Column(db.Integer, primary_key=True) | 18 | id = db.Column(db.Integer, primary_key=True) |
8 | name = db.Column(db.String(16)) | 19 | name = db.Column(db.String(16)) |