create_user
This module provides a Django management command to create or update a user for a given account.
It handles user creation, setting password, sending notification emails, creating UserProfile, and updating or creating AccountContact as needed.
Classes
- Command
Implements the logic for the
manage.py create_usercommand.
Command-line Arguments
- –account_numberstr, required
The Smarter account number the user should belong to.
- –usernamestr, required
The username for the new user.
- –emailstr, required
The email address for the new user.
- –first_namestr, required
The first name of the new user.
- –last_namestr, required
The last name of the new user.
- –passwordstr, optional
The password for the new user. If not provided, a random password is generated.
- –adminbool (flag), optional
Set to true to mark the new user as an admin.
Functionality
Creates or updates a
Userfor the provided account.Validates the provided email address.
Sets password (either provided or randomly generated for new users).
Optionally emails credentials if the ENABLE_NEW_USER_PASSWORD_EMAIL Waffle switch is enabled.
Creates or updates corresponding
UserProfileandAccountContactrecords as needed.All creation is wrapped in an atomic transaction for integrity.
Usage Example
python manage.py create_user –account_number=<number> –username=<name> –email=<email> –first_name=<first> –last_name=<last> [–password=<pwd>] [–admin]
- class smarter.apps.account.management.commands.create_user.Command(*args, **kwargs)[source]
Bases:
SmarterCommandDjango manage.py create_user command.
This command is used to create a new user for an account.
- create_user(account_number, username, email, first_name, last_name, password=None, is_admin=False)[source]
Create a new user for the specified account.
If the user already exists, update the user’s information. The basic workflow is as follows:
- Get the account based on the provided account number. If the account does
not exist, log a failure and return False.
- Attempt to get or create the user based on the provided username. If there
is an error during user creation, log a failure and return False.
- If the user already exists, log a notice that the existing user will be
updated. Set the user’s staff status based on the is_admin flag. Validate the provided email address and if it is invalid, log a failure and return False. Update the user’s email, first name, last name, and active status. If a password is provided, set the user’s password to the provided password. If no password is provided and the user was created, generate a random password, set it for the user, and log the generated password. Attempt to save the user and if there is an error during saving, log a failure and return False. Log a success message that the user has been created or updated.
- If the user was created and the Waffle switch
ENABLE_NEW_USER_PASSWORD_EMAIL is active, attempt to send an email to the user with their account credentials. If there is an error during email sending, log a failure but do not return False since the user account has been created successfully.
- Attempt to get or create a UserProfile for the user and account. If there
is an error during this process, log a failure and return False. If the UserProfile was created, log a success message.
- Attempt to get an AccountContact for the account and email. If it exists,
update the first name and last name and save it. If it does not exist, create a new AccountContact. If there is an error during this process, log a failure and return False.
- If all steps are successful, log a success message that the create_user
command completed successfully and return True.
- Parameters:
account_number (str) – The account number of the account to which the user belongs.
username (str) – The username for the new user.
email (str) – The email address for the new user.
first_name (str) – The first name of the new user.
last_name (str) – The last name of the new user.
password (str) – The password for the new user. If not provided, a random password will be generated. Defaults to None.
is_admin (bool) – Whether the new user should be an admin. Defaults to False.
- Returns:
True if the user was created or updated successfully, False otherwise.
- Return type: