Création des users d'un seul coup

This commit is contained in:
Tom Hubrecht 2021-08-20 15:07:36 +02:00
parent d210fc88c4
commit 2e1051bb2e

View file

@ -322,13 +322,23 @@ def create_users(election, csv_file):
"""Crée les votant·e·s pour l'élection donnée, en remplissant les champs
`username`, `election` et `full_name`.
"""
User = get_user_model()
dialect = csv.Sniffer().sniff(csv_file.readline().decode("utf-8"))
csv_file.seek(0)
reader = csv.reader(io.StringIO(csv_file.read().decode("utf-8")), dialect)
for (username, full_name, email) in reader:
election.registered_voters.create(
username=f"{election.id}__{username}", email=email, full_name=full_name
users = [
User(
election=election,
username=f"{election.id}__{username}",
email=email,
full_name=full_name,
)
for (username, full_name, email) in reader
]
User.objects.bulk_create(users)
def check_csv(csv_file):