py-accounts-management


Namepy-accounts-management JSON
Version 0.1.5 PyPI version JSON
download
home_pagehttps://github.com/asasking/py-accounts-management
SummaryA reusable Django app for managing flexible user account levels and access control.
upload_time2025-07-25 00:12:03
maintainerNone
docs_urlNone
authorDavidi Amoni Bilikwija
requires_python>=3.9
licenseMIT
keywords django account levels user management roles permissions access control
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Py Accounts Management

![PyPI - Version](https://img.shields.io/pypi/v/django-account-levels.svg)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/py-accounts-management.svg)
![PyPI - Django Version](https://img.shields.io/pypi/djversions/django-account-levels.svg)
![License](https://img.shields.io/badge/license-MIT-blue.svg)

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](#features)
* [Installation](#installation)
* [Configuration](#configuration)
* [Usage](#usage)
    * [Defining Account Types](#defining-account-types)
    * [Assigning Account Types to Users](#assigning-account-types-to-users)
    * [Using the Decorators](#using-the-decorator)
    * [Integrating with User Upgrade Form](#integrating-with-user-upgrade-form)
* [Signals](#signals)
* [Contributing](#contributing)
* [License](#license)

---

## 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:**
    ```bash
    pip  install py-accounts-management
    ```

2.  **Add `django-accounts-management` to your `INSTALLED_APPS` in `settings.py`:**
    ```python
    # 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.
    ```bash
    python manage.py migrate
    ```

## Configuration

Add the following settings to your `settings.py` file:

```python
# 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

'''

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/asasking/py-accounts-management",
    "name": "py-accounts-management",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": null,
    "keywords": "django account levels user management roles permissions access control",
    "author": "Davidi Amoni Bilikwija",
    "author_email": "atugonzabilikwija@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/5c/91/fa81faa04b5882de7a9565661af5f53ba5a045e31efd89c982a8b4ef12ae/py_accounts_management-0.1.5.tar.gz",
    "platform": null,
    "description": "# Py Accounts Management\r\n\r\n![PyPI - Version](https://img.shields.io/pypi/v/django-account-levels.svg)\r\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/py-accounts-management.svg)\r\n![PyPI - Django Version](https://img.shields.io/pypi/djversions/django-account-levels.svg)\r\n![License](https://img.shields.io/badge/license-MIT-blue.svg)\r\n\r\nA 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.\r\n\r\nAlso 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,\r\n\r\nManage roles of different users in the same group but with different status or account level,\r\n\r\nan 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\r\n\r\n---\r\n\r\n## Table of Contents\r\n\r\n* [Features](#features)\r\n* [Installation](#installation)\r\n* [Configuration](#configuration)\r\n* [Usage](#usage)\r\n    * [Defining Account Types](#defining-account-types)\r\n    * [Assigning Account Types to Users](#assigning-account-types-to-users)\r\n    * [Using the Decorators](#using-the-decorator)\r\n    * [Integrating with User Upgrade Form](#integrating-with-user-upgrade-form)\r\n* [Signals](#signals)\r\n* [Contributing](#contributing)\r\n* [License](#license)\r\n\r\n---\r\n\r\n## Features\r\n\r\n* **Flexible Account Types:** Define custom account levels (e.g., 'Free', 'Pro', 'Elite') with numerical hierarchy directly in your Django admin.\r\n* **Dedicated User Account Level Model:** A `UserAccountLevel` model automatically created and linked to each Django `User`, storing their current account type.\r\n* **Automatic User Sync:** Automatically assigns a default account type to new users and syncs existing users upon installation/migration.\r\n* **`@account_level_required` Decorator:** Easily restrict access to views based on a minimum required account level.\r\n* **'@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.\r\n* **Configurable Redirects:** Customize the redirect URL for unauthorized access directly in your Django `settings.py` or per decorator instance.\r\n* **Clear Error Handling:** Provides informative error messages for developers if configuration is incorrect.\r\n\r\n## Installation for django development\r\n\r\n1.  **Install the package via pip:**\r\n    ```bash\r\n    pip  install py-accounts-management\r\n    ```\r\n\r\n2.  **Add `django-accounts-management` to your `INSTALLED_APPS` in `settings.py`:**\r\n    ```python\r\n    # myproject/settings.py\r\n\r\n    INSTALLED_APPS = [\r\n        # ... other Django apps\r\n        'django_accounts_management',\r\n        # ... your project's apps\r\n    ]\r\n    ```\r\n\r\n3.  **Run migrations:**\r\n    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.\r\n    ```bash\r\n    python manage.py migrate\r\n    ```\r\n\r\n## Configuration\r\n\r\nAdd the following settings to your `settings.py` file:\r\n\r\n```python\r\n# myproject/settings.py\r\n\r\n# --- Django Account Levels Settings ---\r\n\r\n# The name of the default AccountType for new and existing users upon migration.\r\n# This MUST match the 'name' field of an AccountType instance you create in the admin.\r\n# Example: 'Free'\r\nACCOUNT_LEVELS_DEFAULT_TYPE = 'Free' \r\n\r\n# The URL name (from your project's urls.py) to redirect users to for upgrading their account.\r\n# This is used by the @account_level_required decorator when a user's level is insufficient.\r\n# Example: 'myapp:upgrade_account' (if your upgrade view is in 'myapp' app with namespace)\r\nACCOUNT_LEVELS_UPGRADE_URL_NAME = 'upgrade_account' \r\n\r\n# The URL name to redirect users to if their UserAccountLevel profile is missing.\r\n# This is a robust fallback for unexpected data issues.\r\n# Example: 'home' or 'myapp:home'\r\nACCOUNT_LEVELS_PROFILE_MISSING_REDIRECT_URL = 'home' \r\n\r\n# --- Standard Django Auth Settings (ensure these are also set) ---\r\nLOGIN_URL = '/login/' # Your project's login URL\r\nLOGIN_REDIRECT_URL = '/' # Where to redirect after successful login\r\nLOGOUT_REDIRECT_URL = '/' # Where to redirect after logout\r\n\r\n# using the decorator\r\n\r\n# import the decorator in your views.py\r\n\r\n'''\r\nfrom django.contrib.auth.decorators import login_required\r\nfrom django_accounts_management.decorators import account_level_required, group_required\r\n\r\n@login_required\r\n@group_required('consumers', redirect_url='EluxProcessor:buy_elux')\r\n@account_level_required('Basic', redirect_url='EluxProcessor:buy_elux')\r\n\r\ndef test_view(request):\r\n    \"\"\"\r\n    A simple view to test the setup.\r\n    \"\"\"\r\n    return HttpResponse(\"This is a test view to ensure the Django setup is working correctly.\")\r\n# Create your views here.\r\n\r\n\r\n'''\r\n\r\n# 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\r\n\r\n# @group_required(['consumers', 'sme', ], redirect_url='EluxProcessor:buy_elux')\r\n\r\n'''\r\n\r\nthe decorators for the account level and group are independent on each other, can be used separately to match the context of yor demand\r\n\r\n'''\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A reusable Django app for managing flexible user account levels and access control.",
    "version": "0.1.5",
    "project_urls": {
        "Homepage": "https://github.com/asasking/py-accounts-management"
    },
    "split_keywords": [
        "django",
        "account",
        "levels",
        "user",
        "management",
        "roles",
        "permissions",
        "access",
        "control"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "c54a1738d456c60baf93fdd5da6cc41f786cf5fee9dac0ad1fb6b79ea10b57bc",
                "md5": "207824a77a193c061d696b6819d41ba3",
                "sha256": "52dea931037be2320813bae86944bf0dfefad235c7db62c5d11ac8f8caf58086"
            },
            "downloads": -1,
            "filename": "py_accounts_management-0.1.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "207824a77a193c061d696b6819d41ba3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 14864,
            "upload_time": "2025-07-25T00:12:02",
            "upload_time_iso_8601": "2025-07-25T00:12:02.518932Z",
            "url": "https://files.pythonhosted.org/packages/c5/4a/1738d456c60baf93fdd5da6cc41f786cf5fee9dac0ad1fb6b79ea10b57bc/py_accounts_management-0.1.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "5c91fa81faa04b5882de7a9565661af5f53ba5a045e31efd89c982a8b4ef12ae",
                "md5": "1cbec212fa0d65592e949ebbdf8b9ced",
                "sha256": "0cf4374174e1e9ee160bba29ba26069b6b6fd0605dbe0ad814f4b7b254485c9a"
            },
            "downloads": -1,
            "filename": "py_accounts_management-0.1.5.tar.gz",
            "has_sig": false,
            "md5_digest": "1cbec212fa0d65592e949ebbdf8b9ced",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 15363,
            "upload_time": "2025-07-25T00:12:03",
            "upload_time_iso_8601": "2025-07-25T00:12:03.925024Z",
            "url": "https://files.pythonhosted.org/packages/5c/91/fa81faa04b5882de7a9565661af5f53ba5a045e31efd89c982a8b4ef12ae/py_accounts_management-0.1.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-25 00:12:03",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "asasking",
    "github_project": "py-accounts-management",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "py-accounts-management"
}
        
Elapsed time: 1.24167s