Accounts API Reference
accounts.models
Models for the accounts application.
This module defines the custom user model and its associated manager. The system uses email as the primary identifier for authentication.
User
Bases: AbstractUser
Custom User model that uses email as the unique identifier.
Attributes:
| Name | Type | Description |
|---|---|---|
email |
EmailField
|
The unique email of the user (primary identifier). |
first_name |
CharField
|
The user's first name. |
last_name |
CharField
|
The user's last name. |
currency |
CharField
|
The preferred currency code (e.g., 'EGP', 'USD'). |
status |
CharField
|
Current user status (e.g., 'Onboarding', 'Active'). |
language |
CharField
|
Preferred interface language. |
is_superuser |
BooleanField
|
Designates that this user has all permissions. |
is_staff |
BooleanField
|
Designates whether the user can log into this admin site. |
is_active |
BooleanField
|
Designates whether this user should be treated as active. |
Source code in accounts/models.py
Meta
UserManager
Bases: BaseUserManager
Custom manager for the User model where email is the unique identifier.
Provides methods to create regular users and superusers using email instead of a username.
Source code in accounts/models.py
create_superuser(email, password=None, **extra_fields)
Create and return a superuser with elevated permissions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
email
|
str
|
The unique email address of the superuser. |
required |
password
|
str
|
The raw password for the superuser. |
None
|
**extra_fields
|
Additional fields to be saved in the User model. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
User |
The newly created superuser instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If is_staff or is_superuser is not set to True. |
Source code in accounts/models.py
create_user(email, password=None, **extra_fields)
Create and return a regular user with an email and password.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
email
|
str
|
The unique email address of the user. |
required |
password
|
str
|
The raw password for the user. |
None
|
**extra_fields
|
Additional fields to be saved in the User model. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
User |
The newly created user instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the email field is not provided. |
Source code in accounts/models.py
accounts.serializers
Serializers for the accounts application.
Handles data validation and transformation for user registration, profile retrieval, and partial updates.
UserSerializer
Bases: ModelSerializer
Serializer for user registration and profile retrieval.
Includes basic profile information and ensures the password is write-only.
Source code in accounts/serializers.py
Meta
create(validated_data)
Create a new user using the custom manager.
This method ensures that the password is hashed correctly by calling User.objects.create_user instead of the default ModelSerializer.create.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
validated_data
|
dict
|
The data validated by the serializer. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
User |
The newly created user instance. |
Source code in accounts/serializers.py
UserUpdateSerializer
Bases: ModelSerializer
Serializer for partial profile updates.
Allows users to update specific fields like names, currency, and language without touching core authentication data like email or password.
Source code in accounts/serializers.py
accounts.views
Views for handling user authentication and profile management.
This module provides the AuthViewSet which consolidates registration, login, logout, and profile operations into a single endpoint set.
AuthViewSet
Bases: CreateModelMixin, GenericViewSet
ViewSet for authentication operations: register, login, logout, and profile management.
This viewset handles: - User registration (POST /auth/) - Login (POST /auth/login/) - Logout (GET/POST /auth/logout/) - Current User Profile (GET /auth/me/) - Profile Update (PATCH /auth/update_profile/)
Source code in accounts/views.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
login(request)
Authenticate a user by email and password and start a session.
Expected Data
email (str): The user's email. password (str): The user's password.
Returns:
| Name | Type | Description |
|---|---|---|
Response |
Success message and user data if valid, or error message. |
Source code in accounts/views.py
logout(request)
End the current user session.
Requires Authentication.
Returns:
| Name | Type | Description |
|---|---|---|
Response |
Successfully logged out message. |
Source code in accounts/views.py
me(request)
Return the profile data of the currently authenticated user.
Requires Authentication.
Returns:
| Name | Type | Description |
|---|---|---|
Response |
Authenticated user's profile data. |
Source code in accounts/views.py
perform_create(serializer)
Register a new user using the custom user manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
serializer
|
UserSerializer
|
The validated serializer instance. |
required |
update_profile(request)
Partially update the authenticated user's profile.
Allows changing first_name, last_name, currency, and language.
Returns:
| Name | Type | Description |
|---|---|---|
Response |
Updated profile data or validation errors. |