From ed453065626393e53b2a2c6b705a10879be50ec5 Mon Sep 17 00:00:00 2001 From: Richard Hitier Date: Tue, 27 Apr 2021 14:07:03 +0200 Subject: [PATCH] Remove empty values, and upper() some lists --- app/commands/commands.py | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/app/commands/commands.py b/app/commands/commands.py index 34f5198..99bd8b9 100644 --- a/app/commands/commands.py +++ b/app/commands/commands.py @@ -57,8 +57,6 @@ def feed_from_irap(csv_file_name): with open(csv_file_name, newline='') as csvfile: csvreader = csv.DictReader(csvfile, delimiter=',', quotechar='"') for row in csvreader: - # print('\n'.join(row.keys())) - # break # Remove any leading/trailing spaces row = {k: v.strip() for k, v in row.items()} rows.append(row) @@ -105,16 +103,26 @@ def feed_from_irap(csv_file_name): 'grade': r[grade_key]} agents.append(agent_dict) - # Now, uniq/sort the lists + # Uppercase the small tables + # + baps = [x.upper() for x in baps] + grades = [x.upper() for x in grades] + statuses = [x.upper() for x in statuses] + + # Now, sort the lists + # + # 1- first remove empty string with filter() + # 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(projects)) - services = sorted(set(services)) - baps = sorted(set(baps)) - grades = sorted(set(grades)) - companies = sorted(set(companies)) - statuses = sorted(set(statuses)) + projects = sorted(set(filter(None, projects))) + 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))) # as agents is a list of dicts, sorting is a bit tricky # @@ -169,7 +177,8 @@ def feed_from_irap(csv_file_name): # Feed periods names # Todo: are statically built, - # should come from year column name. + # should come from year column name. + # also see later # for p in range(2011, 2030): n_p = Period(name=f"{p}") @@ -201,7 +210,7 @@ def feed_from_irap(csv_file_name): status_id=status.id if status else None, company_id=company.id if company else None, bap_id=bap.id if bap else None, - grade_id=grade.id if bap else None, + grade_id=grade.id if grade else None, virtual=virtual) db.session.add(n_a) db.session.commit() @@ -216,6 +225,7 @@ def feed_from_irap(csv_file_name): a = Agent.query.filter(Agent.firstname == r[firstname_key], Agent.secondname == r[secondname_key]).one() s = Service.query.filter(Service.name == r[service_key]).one() c = Capacity.query.first() + # TODO: period names should come from db request for period_name in range(2011, 2030): t = Period.query.filter(Period.name == period_name).one() charge = r[f"{period_name}"] -- libgit2 0.21.2