Django custom user
Project description
Django custom user model and abstract user model basic token authentication.
Documentation
The full documentation is at https://django_custom_auth_user.readthedocs.io.
Quickstart
Install Django custom user:
pip install django_custom_auth_user
Add it to your INSTALLED_APPS:
INSTALLED_APPS = ( ... 'custom_auth_user', ... )
Set your AUTH_USER_MODEL setting to use custom_auth_user.User:
AUTH_USER_MODEL = 'custom_auth_user.User'
Create the database tables:
python manage.py migrate
Usage
Custom user
Use get_user_model() to get user. For example:
from django.contrib.auth import get_user_model user = get_user_model().objects.get(email="user@cloud.com")
Use AUTH_USER_MODEL for model relations. For example:
from django.conf import settings from django.db import models class Post(models.Model): author = models.ForeignKey(settings.AUTH_USER_MODEL)
Or you can use custom_auth_user.models.AbstractUser to extend custom user. For example:
from custom_auth_user.models import AbstractUser class CustomUser(AbstractUser): """ User model extends AbstractUser """ pass
Then change the AUTH_USER_MODEL in settings to use new custom user:
AUTH_USER_MODEL = 'app.CustomUser'
User registration service
Use RegistrationService to register new user:
from custom_auth_user.user.registration import RegistrationService from custom_auth_user.user.exceptions import InvalidInput # Initialize registration service registration_service = RegistrationService( email=request.POST.get('email', ''), username=request.POST.get('username', ''), first_name=request.POST.get('first_name', ''), last_name=request.POST.get('last_name', ''), password=request.POST.get('password', '')) # Catch errors here try: user = registration_service.run() except InvalidInput: errors = registration_service.get_registration_form_errors()
Generate auth token
Use AuthenticateUserService to generate new auth token:
from custom_auth_user.auth_token.authenticate_user import AuthenticateUserService from custom_auth_user.auth_token.exceptions import AuthenticationFailed authentication_service = AuthenticateUserService( email_or_username=request.POST.get('email_or_username', ''), password=request.POST.get('password', '')) try: token = authentication_service.run() except AuthenticationFailed: # Authentication failed pass
Token authentication
Use token_required decorator to authenticate header token. Get authenticated user at request.user.
Authorization header must have token at the begining. Example token 5KxXkJYwWBsN9Zne87ncoQYYuggDBdYY.
Return json response if authentication failed:
from custom_auth_user.auth_token.decorators import token_required @token_required def my_view(request): # get authenticated user at request.user pass
Or you can use AuthenticateTokenService to authenticate token:
from custom_auth_user.auth_token.authenticate_token import AuthenticateTokenService from custom_auth_user.auth_token.exceptions import AuthenticationFailed authentication_service = AuthenticateTokenService( auth_token=request.POST.get('token', '')) try: user = authentication_service.run() except AuthenticationFailed: # Authentication failed pass
Delete Token
Use DeleteTokenService to delete token. You can use this when user logs out:
from custom_auth_user.auth_token.delete_token import DeleteTokenService from custom_auth_user.auth_token.exceptions import TokenNotFound delete_token_service = DeleteTokenService( token=request.POST.get('token', '')) try: delete_token_service.run() except TokenNotFound: # Token not found pass
Extra features
User QuerySets
# Get all users users = User.objects.get_all() # Get find user by id user = User.objects.find_by_id(id=1) # Get find usermame by id user = User.objects.find_by_username(username='user') # Get find email by id user = User.objects.find_by_email(email='user@cloud.com') # Filter active users users = User.objects.filter_by_active() # Filter inactive users users = User.objects.filter_by_inactive() # Filter enabled users users = User.objects.filter_by_enabled() # Filter disabled users users = User.objects.filter_by_disabled()
AuthToken QuerySets
# Get all auth token token = AuthToken.objects.get_all() # Find by id token = AuthToken.objects.find_by_id(id=1) # Find by token token = AuthToken.objects.find_by_token(token='token') # Filter by active or unexpired tokens token = AuthToken.objects.filter_by_active() # Filter by expired token token = AuthToken.objects.filter_by_expired()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-custom-auth-user-0.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9a5ab5b66dd04d46e343f9e507581d3abeff0fb60c3758dcc7d1db283e98796d |
|
MD5 | 647a88e9616770d3fff58d8062125ce6 |
|
BLAKE2b-256 | 33acb5b633d30925fd48e9e55b3345a21da200e82ef0b8e0d65d21c14b27b876 |
Hashes for django_custom_auth_user-0.1.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 572ef9e0a190642a53b24ca8b4fe4bb251b7674ca00403ef7c89d86fde75899d |
|
MD5 | 2f62a4d0250ca73d429c4275a780e5d5 |
|
BLAKE2b-256 | d463bcc01bbbcb928f8513c1ffb875361fc70dd852749abad27263486a97ce67 |