accounts package
Submodules
accounts.admin module
- class accounts.admin.CustomUserAdmin(model, admin_site)[source]
Bases:
UserAdminAccounts (CustomUser) Administration customization
- add_form
alias of
CustomUserCreationForm
- form
alias of
CustomUserChangeForm
- list_display = ['email', 'username', 'is_staff', 'is_active']
- property media
- model
alias of
CustomUser
accounts.apps module
Accounts App (CustomUser records) Configuration
accounts.forms module
- class accounts.forms.CustomUserChangeForm(*args, **kwargs)[source]
Bases:
UserChangeFormForm fields:
email: Email address (EmailField)username: Username (CharField)password: Password (ReadOnlyPasswordHashField)
- property media
Return all media required to render the widgets on this form.
- class accounts.forms.CustomUserCreationForm(*args, **kwargs)[source]
Bases:
AdminUserCreationFormForm fields:
email: Email address (EmailField)username: Username (CharField)password1: Password (CharField)password2: Password confirmation (CharField)usable_password: Password-based authentication (ChoiceField)
- property media
Return all media required to render the widgets on this form.
accounts.models module
Accounts model (CustomUser records) for storing users customized to allow login by email, etc.
Mix in SafeDeleteManager into CustomUserManager for Soft Deletes using safedelete
https://django-safedelete.readthedocs.io/en/latest/managers.html
safe delete of custom users example found at;
https://codeberg.org/mvlaev/Cars/src/branch/main/cars/users_app/models.py”
- class accounts.models.CustomUser(*args, **kwargs)[source]
Bases:
BaseModel,AbstractUserDatabase table:
accounts_customuserCustomUser model - Abstract User customized to allow login by email
Mix in BaseModel to provide: - soft deletes using django-safedelete
- record history / versioning through django-auditlog
- Parameters:
id (BigAutoField) – Primary key: ID
password (CharField) – Password
last_login (DateTimeField) – Last login
is_superuser (BooleanField) – Superuser status. Designates that this user has all permissions without explicitly assigning them.
username (CharField) – Username. Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.
first_name (CharField) – First name
last_name (CharField) – Last name
email (EmailField) – Email address
is_staff (BooleanField) – Staff status. Designates whether the user can log into this admin site.
is_active (BooleanField) – Active. Designates whether this user should be treated as active. Unselect this instead of deleting accounts.
date_joined (DateTimeField) – Date joined
deleted (DateTimeField) – Deleted
deleted_by_cascade (BooleanField) – Deleted by cascade
created_at (DateTimeField) – Created at
updated_at (DateTimeField) – Updated at
Relationship fields:
- Parameters:
groups (
ManyToManyFieldtoGroup) – Groups. The groups this user belongs to. A user will get all permissions granted to each of their groups. (related name:user_set)user_permissions (
ManyToManyFieldtoPermission) – User permissions. Specific permissions for this user. (related name:user_set)history (
AuditlogHistoryFieldtoLogEntry) – History (related name:+)
Reverse relationships:
- Parameters:
logentry (Reverse
ForeignKeyfromLogEntry) – All log entries of this custom user (related name ofuser)emailaddress (Reverse
ForeignKeyfromEmailAddress) – All email addresses of this custom user (related name ofuser)
- exception DoesNotExist
Bases:
ObjectDoesNotExist
- exception MultipleObjectsReturned
Bases:
MultipleObjectsReturned
- created_at
Type:
DateTimeFieldCreated at
A wrapper for a deferred-loading field. When the value is read from this
- date_joined
Type:
DateTimeFieldDate joined
A wrapper for a deferred-loading field. When the value is read from this
- deleted
Type:
DateTimeFieldDeleted
A wrapper for a deferred-loading field. When the value is read from this
- deleted_by_cascade
Type:
BooleanFieldDeleted by cascade
A wrapper for a deferred-loading field. When the value is read from this
- email
Type:
EmailFieldEmail address
A wrapper for a deferred-loading field. When the value is read from this
- emailaddress_set
Type: Reverse
ForeignKeyfromEmailAddressAll email addresses of this custom user (related name of
user)Accessor to the related objects manager on the reverse side of a many-to-one relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
Parent.childrenis aReverseManyToOneDescriptorinstance.Most of the implementation is delegated to a dynamically defined manager
- first_name
Type:
CharFieldFirst name
A wrapper for a deferred-loading field. When the value is read from this
- get_next_by_created_at(*, field=<django.db.models.DateTimeField: created_at>, is_next=True, **kwargs)
Finds next instance based on
created_at. Seeget_next_by_FOO()for more information.
- get_next_by_date_joined(*, field=<django.db.models.DateTimeField: date_joined>, is_next=True, **kwargs)
Finds next instance based on
date_joined. Seeget_next_by_FOO()for more information.
- get_next_by_updated_at(*, field=<django.db.models.DateTimeField: updated_at>, is_next=True, **kwargs)
Finds next instance based on
updated_at. Seeget_next_by_FOO()for more information.
- get_previous_by_created_at(*, field=<django.db.models.DateTimeField: created_at>, is_next=False, **kwargs)
Finds previous instance based on
created_at. Seeget_previous_by_FOO()for more information.
- get_previous_by_date_joined(*, field=<django.db.models.DateTimeField: date_joined>, is_next=False, **kwargs)
Finds previous instance based on
date_joined. Seeget_previous_by_FOO()for more information.
- get_previous_by_updated_at(*, field=<django.db.models.DateTimeField: updated_at>, is_next=False, **kwargs)
Finds previous instance based on
updated_at. Seeget_previous_by_FOO()for more information.
- groups
Type:
ManyToManyFieldtoGroupGroups. The groups this user belongs to. A user will get all permissions granted to each of their groups. (related name:
user_set)Accessor to the related objects manager on the forward and reverse sides of a many-to-many relation.
In the example:
class Pizza(Model): toppings = ManyToManyField(Topping, related_name='pizzas')
Pizza.toppingsandTopping.pizzasareManyToManyDescriptorinstances.Most of the implementation is delegated to a dynamically defined manager
- history
Type: Reverse
AuditlogHistoryFieldfromCustomUserAll + of this log entry (related name of
history)Accessor to the related objects manager on the one-to-many relation created by GenericRelation.
In the example:
class Post(Model): comments = GenericRelation(Comment)
- id
Type:
BigAutoFieldPrimary key: ID
A wrapper for a deferred-loading field. When the value is read from this
- is_active
Type:
BooleanFieldActive. Designates whether this user should be treated as active. Unselect this instead of deleting accounts.
A wrapper for a deferred-loading field. When the value is read from this
- is_staff
Type:
BooleanFieldStaff status. Designates whether the user can log into this admin site.
A wrapper for a deferred-loading field. When the value is read from this
- is_superuser
Type:
BooleanFieldSuperuser status. Designates that this user has all permissions without explicitly assigning them.
A wrapper for a deferred-loading field. When the value is read from this
- last_login
Type:
DateTimeFieldLast login
A wrapper for a deferred-loading field. When the value is read from this
- last_name
Type:
CharFieldLast name
A wrapper for a deferred-loading field. When the value is read from this
- logentry_set
Type: Reverse
ForeignKeyfromLogEntryAll log entries of this custom user (related name of
user)Accessor to the related objects manager on the reverse side of a many-to-one relation.
In the example:
class Child(Model): parent = ForeignKey(Parent, related_name='children')
Parent.childrenis aReverseManyToOneDescriptorinstance.Most of the implementation is delegated to a dynamically defined manager
- objects = <accounts.models.CustomUserManager object>
- password
Type:
CharFieldPassword
A wrapper for a deferred-loading field. When the value is read from this
- updated_at
Type:
DateTimeFieldUpdated at
A wrapper for a deferred-loading field. When the value is read from this
- user_permissions
Type:
ManyToManyFieldtoPermissionUser permissions. Specific permissions for this user. (related name:
user_set)Accessor to the related objects manager on the forward and reverse sides of a many-to-many relation.
In the example:
class Pizza(Model): toppings = ManyToManyField(Topping, related_name='pizzas')
Pizza.toppingsandTopping.pizzasareManyToManyDescriptorinstances.Most of the implementation is delegated to a dynamically defined manager
- class accounts.models.CustomUserManager(*args, **kwargs)[source]
Bases:
SafeDeleteManager,UserManagerCustom User model Manager class (‘objects’).
Manager class for CustomUsers (Accounts). Access to this class is through the ‘objects’ instance attribute of the CustomUser Class.
Soft Delete of Users are implemented through SafeDelete. See: https://django-safedelete.readthedocs.io/en/latest/managers.html
- Parameters:
param1 (class) – SafeDelete manager class mixin
param2 (class) – UserManager for CustomUser Abstract Class
- all_deleted()[source]
Returns all soft deleted customuser records.
Todo
replace accounts.models.CustomUserManager.all_deleted with SafeDeleteManager.deleted_only()
see: https://django-safedelete.readthedocs.io/en/latest/managers.html
note: these functions are only found in model manager classes
thus: all models must declare their custom manager based off of SafeDeleteManager
No arguments are passed to this function when calling it
- Returns:
The soft deleted custom user records.
- Return type:
recordset
Example
# Print out all deleted records for rec in Account.all_deleted(): print(rec)
accounts.signals module
- accounts.signals.email_is_also_username(sender, instance, **kwargs)[source]
Force the username field to be the same as the email field
Prevent duplication of email addresses in the CustomUser model at the database level.
there could be duplicate emails in the database, thus leaving a potential issue with duplicate emails in the database
We do not want duplicate emails in the database, because we are logging in by email address
Warning: do not use this CustomUser model if you wish to have usernames that are other than the user’s email addresssee: https://docs.allauth.org/en/latest/account/configuration.html
We are using a pre_save signal decorator to force username field to be set to the records email field value for all CustomUser records