Skip to main content

A reusable Django app for managing flexible user account levels and access control.

Project description

Py Accounts Management

PyPI - Version PyPI - Python Version PyPI - Django Version License

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 UserAccountLevel model automatically created and linked to each Django User, 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_required Decorator: 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.py or per decorator instance.
  • Clear Error Handling: Provides informative error messages for developers if configuration is incorrect.

Installation for django development

  1. Install the package via pip:

    pip  install py-accounts-management
    
  2. Add django-accounts-management to your INSTALLED_APPS in settings.py:

    # myproject/settings.py
    
    INSTALLED_APPS = [
        # ... other Django apps
        'django_accounts_management',
        # ... your project's apps
    ]
    
  3. Run migrations: This will create the necessary database tables for AccountType and UserAccountLevel models, 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py_accounts_management-0.1.5.tar.gz (15.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

py_accounts_management-0.1.5-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file py_accounts_management-0.1.5.tar.gz.

File metadata

  • Download URL: py_accounts_management-0.1.5.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

Hashes for py_accounts_management-0.1.5.tar.gz
Algorithm Hash digest
SHA256 0cf4374174e1e9ee160bba29ba26069b6b6fd0605dbe0ad814f4b7b254485c9a
MD5 1cbec212fa0d65592e949ebbdf8b9ced
BLAKE2b-256 5c91fa81faa04b5882de7a9565661af5f53ba5a045e31efd89c982a8b4ef12ae

See more details on using hashes here.

File details

Details for the file py_accounts_management-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for py_accounts_management-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 52dea931037be2320813bae86944bf0dfefad235c7db62c5d11ac8f8caf58086
MD5 207824a77a193c061d696b6819d41ba3
BLAKE2b-256 c54a1738d456c60baf93fdd5da6cc41f786cf5fee9dac0ad1fb6b79ea10b57bc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page