Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make bulk_insert_voters work with parent elections #190

Merged
merged 1 commit into from
Mar 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions authapi/api/management/commands/bulk_insert_voters.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ class Command(BaseCommand):
'''
Inserts in bulk a CSV list of voters in an election. It's made using COPY
command and a temporal table to make it fast.

CSV format is used, with ';' as separator. Data cannot be escaped with quotes,
so the separator cannot appear in the data.

It supports parent-children elections. An example CSV is:

email;tlf;Número de colegiado;Nombre;children_event_id_list
john@example.com;+34777444111;50010;DOE JOHN;[11222,11223]
'''
# URL of sources of inspiration:
#
Expand Down Expand Up @@ -100,7 +108,7 @@ def init(self, event_id, voters_csv, iterations):
assert(self.iterations > 0)

with open(voters_csv, 'r') as csv_file:
csv_reader = csv.reader(csv_file)
csv_reader = csv.reader(csv_file, delimiter=';')
self.columns = [column.strip() for column in csv_reader.__next__()]
assert(len(self.columns) > 0)

Expand Down Expand Up @@ -135,9 +143,9 @@ def load_voters_csv_table(self):
# ignore the first line, which contains the column headers
csv_file.readline()
self.exec_sql(
sql="self.cursor.copy_from(csv_file, 'voters_csv_table', sep=',')",
sql="self.cursor.copy_from(csv_file, 'voters_csv_table', sep=';')",
exec_lambda=lambda:
self.cursor.copy_from(csv_file, 'voters_csv_table', sep=',')
self.cursor.copy_from(csv_file, 'voters_csv_table', sep=';')
)

def load_password_function(self):
Expand Down Expand Up @@ -264,20 +272,23 @@ def get_loader_sql_options(self):

# metadata will be a collection of data from the CSV fields, just without
# tlf or email fields
sql_options['metadata'] = "concat('{'"
first = True
if 'children_event_id_list' in self.columns:
sql_options['metadata'] = """
concat(
'{"children_event_id_list": ',
csv_data.children_event_id_list"""
else:
sql_options['metadata'] = """
concat(
'{"children_event_id_list": []'"""

for column in self.columns:
if column in ['tlf', 'email', 'password', 'children_event_id_list']:
continue
if first:
first = False
sql_options['metadata'] += """
, '"%(column)s": "', csv_data."%(column)s", '"'
""" % dict(column=column)
else:
sql_options['metadata'] += """
, ', "%(column)s": "', csv_data."%(column)s", '"'
""" % dict(column=column)
sql_options['metadata'] += """,
', "%(column)s": "',
csv_data."%(column)s",
'"' """ % dict(column=column)

sql_options['metadata'] += ", '}')::jsonb AS metadata"

Expand Down