Commit ed453065626393e53b2a2c6b705a10879be50ec5

Authored by hitier
1 parent 854e5e73

Remove empty values, and upper() some lists

Showing 1 changed file with 21 additions and 11 deletions   Show diff stats
app/commands/commands.py
@@ -57,8 +57,6 @@ def feed_from_irap(csv_file_name): @@ -57,8 +57,6 @@ def feed_from_irap(csv_file_name):
57 with open(csv_file_name, newline='') as csvfile: 57 with open(csv_file_name, newline='') as csvfile:
58 csvreader = csv.DictReader(csvfile, delimiter=',', quotechar='"') 58 csvreader = csv.DictReader(csvfile, delimiter=',', quotechar='"')
59 for row in csvreader: 59 for row in csvreader:
60 - # print('\n'.join(row.keys()))  
61 - # break  
62 # Remove any leading/trailing spaces 60 # Remove any leading/trailing spaces
63 row = {k: v.strip() for k, v in row.items()} 61 row = {k: v.strip() for k, v in row.items()}
64 rows.append(row) 62 rows.append(row)
@@ -105,16 +103,26 @@ def feed_from_irap(csv_file_name): @@ -105,16 +103,26 @@ def feed_from_irap(csv_file_name):
105 'grade': r[grade_key]} 103 'grade': r[grade_key]}
106 agents.append(agent_dict) 104 agents.append(agent_dict)
107 105
108 - # Now, uniq/sort the lists 106 + # Uppercase the small tables
  107 + #
  108 + baps = [x.upper() for x in baps]
  109 + grades = [x.upper() for x in grades]
  110 + statuses = [x.upper() for x in statuses]
  111 +
  112 + # Now, sort the lists
  113 + #
  114 + # 1- first remove empty string with filter()
  115 + # 2- then keep only uniq item with set()
  116 + # 3- at last alpha sort with sorted()
109 # 117 #
110 # thematics = sorted(set(thematics)) 118 # thematics = sorted(set(thematics))
111 # typologies = sorted(set(typologies)) 119 # typologies = sorted(set(typologies))
112 - projects = sorted(set(projects))  
113 - services = sorted(set(services))  
114 - baps = sorted(set(baps))  
115 - grades = sorted(set(grades))  
116 - companies = sorted(set(companies))  
117 - statuses = sorted(set(statuses)) 120 + projects = sorted(set(filter(None, projects)))
  121 + services = sorted(set(filter(None, services)))
  122 + baps = sorted(set(filter(None, baps)))
  123 + grades = sorted(set(filter(None, grades)))
  124 + companies = sorted(set(filter(None, companies)))
  125 + statuses = sorted(set(filter(None, statuses)))
118 126
119 # as agents is a list of dicts, sorting is a bit tricky 127 # as agents is a list of dicts, sorting is a bit tricky
120 # 128 #
@@ -169,7 +177,8 @@ def feed_from_irap(csv_file_name): @@ -169,7 +177,8 @@ def feed_from_irap(csv_file_name):
169 177
170 # Feed periods names 178 # Feed periods names
171 # Todo: are statically built, 179 # Todo: are statically built,
172 - # should come from year column name. 180 + # should come from year column name.
  181 + # also see later
173 # 182 #
174 for p in range(2011, 2030): 183 for p in range(2011, 2030):
175 n_p = Period(name=f"{p}") 184 n_p = Period(name=f"{p}")
@@ -201,7 +210,7 @@ def feed_from_irap(csv_file_name): @@ -201,7 +210,7 @@ def feed_from_irap(csv_file_name):
201 status_id=status.id if status else None, 210 status_id=status.id if status else None,
202 company_id=company.id if company else None, 211 company_id=company.id if company else None,
203 bap_id=bap.id if bap else None, 212 bap_id=bap.id if bap else None,
204 - grade_id=grade.id if bap else None, 213 + grade_id=grade.id if grade else None,
205 virtual=virtual) 214 virtual=virtual)
206 db.session.add(n_a) 215 db.session.add(n_a)
207 db.session.commit() 216 db.session.commit()
@@ -216,6 +225,7 @@ def feed_from_irap(csv_file_name): @@ -216,6 +225,7 @@ def feed_from_irap(csv_file_name):
216 a = Agent.query.filter(Agent.firstname == r[firstname_key], Agent.secondname == r[secondname_key]).one() 225 a = Agent.query.filter(Agent.firstname == r[firstname_key], Agent.secondname == r[secondname_key]).one()
217 s = Service.query.filter(Service.name == r[service_key]).one() 226 s = Service.query.filter(Service.name == r[service_key]).one()
218 c = Capacity.query.first() 227 c = Capacity.query.first()
  228 + # TODO: period names should come from db request
219 for period_name in range(2011, 2030): 229 for period_name in range(2011, 2030):
220 t = Period.query.filter(Period.name == period_name).one() 230 t = Period.query.filter(Period.name == period_name).one()
221 charge = r[f"{period_name}"] 231 charge = r[f"{period_name}"]