EmailAddress
Manages email addresses associated with user accounts, including verification status and primary email designation.Fields
Reference to the user model (settings.AUTH_USER_MODEL). On delete: CASCADE.
The email address. Indexed for performance. Max length determined by
ACCOUNT_EMAIL_MAX_LENGTH setting.Whether the email address has been verified.
Whether this is the user’s primary email address. Only one email per user can be primary.
Methods
clean()
Normalizes the email address to lowercase before saving.can_set_verified()
Checks whether the email address can be marked as verified.Returns
True if the email can be verified, False if there’s a conflict with UNIQUE_EMAIL setting.set_verified(commit=True)
Marks the email address as verified.Whether to save the change to the database immediately.
Returns
True if the email was successfully verified, False otherwise.set_as_primary(conditional=False)
Marks the email address as the user’s primary email.If
True, only sets as primary if no other primary email exists.Returns
True if successfully set as primary, False if conditional and another primary exists.send_confirmation(request=None, signup=False)
Creates and sends an email confirmation.The HTTP request object.
Whether this confirmation is for signup.
Returns the created confirmation object.
remove()
Deletes the email address and updates the user’s email field if necessary.Constraints
- unique_together:
(user, email)- Each user can have each email address only once - unique_primary_email: Only one primary email per user (enforced via UniqueConstraint)
- unique_verified_email: If
UNIQUE_EMAILis enabled, verified emails are unique across all users
EmailConfirmation
Represents an email confirmation request with a unique key.Fields
Reference to the EmailAddress being confirmed. On delete: CASCADE.
When the confirmation was created. Defaults to current time.
When the confirmation email was sent.
Unique confirmation key (max length 64).
Class Methods
create(email_address)
Creates a new confirmation for an email address.The email address to create confirmation for.
Returns the created confirmation object.
from_key(key)
Retrieves a valid confirmation by its key.The confirmation key.
Returns the confirmation object if valid, None otherwise.
Instance Methods
key_expired()
Checks if the confirmation key has expired.Returns
True if expired, based on EMAIL_CONFIRMATION_EXPIRE_DAYS setting.confirm(request)
Confirms the email address if the key hasn’t expired.The HTTP request object.
Returns the confirmed EmailAddress, or None if expired.
send(request=None, signup=False)
Sends the confirmation email and updates the sent timestamp.The HTTP request object.
Whether this is for signup.
EmailConfirmationHMAC
HMAC-based email confirmation (no database storage). Used whenEMAIL_CONFIRMATION_HMAC is enabled.
Class Methods
create(email_address)
Creates an HMAC-based confirmation.The email address to confirm.
Returns the confirmation object.
from_key(key)
Retrieves and validates an HMAC confirmation key.The HMAC-signed key.
Returns the confirmation if valid, None if expired or invalid.
Properties
The HMAC-signed confirmation key (read-only property).
Instance Methods
key_expired()
Always returnsFalse as expiration is checked during signature validation.
confirm(request)
Confirms the email address.The HTTP request object.
Returns the confirmed EmailAddress.
Login
Represents a user in the process of logging in. Used to track login state across requests.Attributes
The user being logged in. Optional to prevent user enumeration.
Email verification method to use for this login.
URL to redirect to after login.
Additional kwargs to pass to signals.
Whether this login is part of signup.
Email address used for login.
Phone number used for login.
Additional state dictionary.
Unix timestamp when login was initiated.
Constructor
Methods
serialize()
Serializes the login state to a dictionary for session storage.Dictionary containing all login state.
deserialize(data)
Class method to reconstruct a Login object from serialized data.Serialized login data.
Reconstructed Login object.
Utility Functions
get_emailconfirmation_model()
Returns the appropriate email confirmation model class based on settings.Returns
EmailConfirmation, EmailConfirmationHMAC, or raises NotImplementedError for code-based verification.