A reusable Django app for managing flexible user account levels and access control.
Project description
Py Accounts Management
A reusable Django application for managing flexible user account levels and controlling access to features based on these levels. Easily define custom account types (e.g., Free, Premium, Gold) and integrate level-based access control into your views.
Also categorize the users of those accounts into various groups depending on the number of gates or portals you have in your application and the access algorithm you want,
Manage roles of different users in the same group but with different status or account level,
an account level based view can be acccessed bhy various users of the same account level but in different multiple groups based on your system requirements
Table of Contents
Features
- Flexible Account Types: Define custom account levels (e.g., 'Free', 'Pro', 'Elite') with numerical hierarchy directly in your Django admin.
- Dedicated User Account Level Model: A
UserAccountLevelmodel automatically created and linked to each DjangoUser, storing their current account type. - Automatic User Sync: Automatically assigns a default account type to new users and syncs existing users upon installation/migration.
@account_level_requiredDecorator: Easily restrict access to views based on a minimum required account level.- '@group_required' Decorator: Easily restricts access from users not in the specified groups if yo are using the django's built-in group model. It specifies either a name of a single group or the list of group names assosiated with that view.
- Configurable Redirects: Customize the redirect URL for unauthorized access directly in your Django
settings.pyor per decorator instance. - Clear Error Handling: Provides informative error messages for developers if configuration is incorrect.
Installation for django development
-
Install the package via pip:
pip install py-accounts-management
-
Add
django-accounts-managementto yourINSTALLED_APPSinsettings.py:# myproject/settings.py INSTALLED_APPS = [ # ... other Django apps 'django-accounts-management', # ... your project's apps ]
-
Run migrations: This will create the necessary database tables for
AccountTypeandUserAccountLevelmodels, and automatically assign the default account type to existing users based on your settings.python manage.py migrate
Configuration
Add the following settings to your settings.py file:
# myproject/settings.py
# --- Django Account Levels Settings ---
# The name of the default AccountType for new and existing users upon migration.
# This MUST match the 'name' field of an AccountType instance you create in the admin.
# Example: 'Free'
ACCOUNT_LEVELS_DEFAULT_TYPE = 'Free'
# The URL name (from your project's urls.py) to redirect users to for upgrading their account.
# This is used by the @account_level_required decorator when a user's level is insufficient.
# Example: 'myapp:upgrade_account' (if your upgrade view is in 'myapp' app with namespace)
ACCOUNT_LEVELS_UPGRADE_URL_NAME = 'upgrade_account'
# The URL name to redirect users to if their UserAccountLevel profile is missing.
# This is a robust fallback for unexpected data issues.
# Example: 'home' or 'myapp:home'
ACCOUNT_LEVELS_PROFILE_MISSING_REDIRECT_URL = 'home'
# --- Standard Django Auth Settings (ensure these are also set) ---
LOGIN_URL = '/login/' # Your project's login URL
LOGIN_REDIRECT_URL = '/' # Where to redirect after successful login
LOGOUT_REDIRECT_URL = '/' # Where to redirect after logout
# using the decorator
# import the decorator in your views.py
'''
from django.contrib.auth.decorators import login_required
from django_accounts_management.decorators import account_level_required, group_required
@login_required
@group_required('consumers', redirect_url='EluxProcessor:buy_elux')
@account_level_required('Basic', redirect_url='EluxProcessor:buy_elux')
def test_view(request):
"""
A simple view to test the setup.
"""
return HttpResponse("This is a test view to ensure the Django setup is working correctly.")
# Create your views here.
'''
# or the group decolators can be used as the list of groups if you need to set the view appropriate to more than one view
# @group_required(['consumers', 'sme', ], redirect_url='EluxProcessor:buy_elux')
'''
the decorators for the account level and group are independent on each other, can be used separately to match the context of yor demand
'''
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py_accounts_management-0.1.0.tar.gz.
File metadata
- Download URL: py_accounts_management-0.1.0.tar.gz
- Upload date:
- Size: 15.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c285a04dff812ee96b85fb1d5ac27ffd08dc3ef519ab153f3239e82b94013170
|
|
| MD5 |
7038fba3b71fad3c89bf181c0dac0867
|
|
| BLAKE2b-256 |
c2914d80c1d79cc37c41e3522776c7eff16dbec915e3f6e5c704d35f5523ac21
|
File details
Details for the file py_accounts_management-0.1.0-py3-none-any.whl.
File metadata
- Download URL: py_accounts_management-0.1.0-py3-none-any.whl
- Upload date:
- Size: 14.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e67789e7eb2306545467200a85fa03da5266e5d02664ce361b3f14d0c9f7473
|
|
| MD5 |
a8f783e789b7a99684a032d0d06e5be5
|
|
| BLAKE2b-256 |
467271f60bc769039a36b6c42db905eedc72a6b6b6c0acf9d4f89e682e07625b
|