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

Update Candidate Account Provision to Use Bulk Creation #567

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
33 changes: 19 additions & 14 deletions hknweb/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,30 +236,35 @@ def generate_password() -> str:

return password

email_information = []
# Process user data into useable structs
user_data = []
user_pwrds = []
for row in rows:
# If username is None or already exists, skip provisioning
if (row["username"] is None) or (row["username"] in existing_usernames):
if row["username"] is None or row["username"] in existing_usernames:
continue

# Generate a password
password = generate_password()

# Construct user object
user = User.objects.create_user(
user = User(
username=row["username"],
first_name=row["First name"],
last_name=row["Last name"],
email=row["Berkeley email"],
password=password,
)
user.save()

# Add user to the candidates group
group.user_set.add(user)
password = generate_password()

# Add information for sending emails
email_information.append((user, password))
user_pwrds.append(password)
user.set_password(password)
user_data.append(user)

users = User.objects.bulk_create(user_data) # Bulk process accounts into database
group.user_set.add(*users)

# Save each user, add to candidate group set, and add information for emails
email_information = []
for i in range(len(users)):
user = users[i]
pwrd = user_pwrds[i]
email_information.append((user, pwrd))

self.email_information = email_information

Expand Down