permission-manager-drf


Namepermission-manager-drf JSON
Version 0.3.1 PyPI version JSON
download
home_pagehttps://github.com/kindlycat/permission-manager-drf
SummaryA simple way to manage object permissions in drf.
upload_time2024-08-04 19:20:09
maintainerNone
docs_urlNone
authorGrigory Mishchenko
requires_python>=3.10
licenseMIT
keywords permissions drf rest framework
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Permission Manager for django rest framework

![example workflow](https://github.com/kindlycat/permission-manager-drf/actions/workflows/tests.yml/badge.svg)
[![codecov](https://codecov.io/gh/kindlycat/permission-manager-drf/graph/badge.svg?token=5XAFJZS6A8)](https://codecov.io/gh/kindlycat/permission-manager-drf)

Use [permission_manager](https://github.com/kindlycat/permission-manager) for 
django rest framework.

Full documentation on [read the docs](https://permission-manager-drf.readthedocs.io/).

## Install

```bash
pip install permission-manager-drf
```

## Example
```python
from django.db import models
from rest_framework.permissions import IsAuthenticated
from rest_framework.viewsets import ModelViewSet
from rest_framework.decorators import action

from permission_manager_drf import DRFPermissionManager, ManagerPermission
from permission_manager import PermissionResult


# Define permission manager
class SomeModelPermissionManager(DRFPermissionManager):
    def has_create_permission(self) -> bool:
        return self.user.is_staff

    def has_update_permission(self) -> bool:
        return self.user.is_staff

    def has_delete_permission(self) -> bool:
        return self.user.is_staff

    def has_view_permission(self) -> bool:
        return True

    def has_list_permission(self) -> bool:
        return True

    def has_custom_permission(self) -> bool:
        return PermissionResult(
            message="You can't do it",
            value=self.user.is_staff,
        )


# Define model with permission manager attribute
class SomeModel(models.Model):
    permission_manager = SomeModelPermissionManager
    ...


# ViewSet
class TestModelViewSet(ModelViewSet):
    permission_classes = [IsAuthenticated, ManagerPermission]
    ...

    @action(detail=True)
    def custom(self, request, **kwargs):
        ...
```
That's all. Now every drf action will be checked by the permission manager.

Also, you can use the serializer field for retrieve permissions you need.

```python
from permission_manager_drf import PermissionField
from rest_framework.serializers import ModelSerializer

class SomeModelSerializer(ModelSerializer):
    permissions = PermissionField(actions=('view', 'custom'),)
    ...

"""
Example output:
{
    ...,
    'permissions': {
        'view': {
            'allow': True,
            'messages': None,
        },
        'custom': {
            'allow': False,
            'messages': ["You can't do it"],
        },
    }
}
"""
```


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/kindlycat/permission-manager-drf",
    "name": "permission-manager-drf",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "permissions, drf, rest framework",
    "author": "Grigory Mishchenko",
    "author_email": "grishkokot@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/6d/56/ee19e5196d18f7d807b1ed66a4091bdbeb32fcddf995bf8369efefb777d5/permission_manager_drf-0.3.1.tar.gz",
    "platform": null,
    "description": "# Permission Manager for django rest framework\n\n![example workflow](https://github.com/kindlycat/permission-manager-drf/actions/workflows/tests.yml/badge.svg)\n[![codecov](https://codecov.io/gh/kindlycat/permission-manager-drf/graph/badge.svg?token=5XAFJZS6A8)](https://codecov.io/gh/kindlycat/permission-manager-drf)\n\nUse [permission_manager](https://github.com/kindlycat/permission-manager) for \ndjango rest framework.\n\nFull documentation on [read the docs](https://permission-manager-drf.readthedocs.io/).\n\n## Install\n\n```bash\npip install permission-manager-drf\n```\n\n## Example\n```python\nfrom django.db import models\nfrom rest_framework.permissions import IsAuthenticated\nfrom rest_framework.viewsets import ModelViewSet\nfrom rest_framework.decorators import action\n\nfrom permission_manager_drf import DRFPermissionManager, ManagerPermission\nfrom permission_manager import PermissionResult\n\n\n# Define permission manager\nclass SomeModelPermissionManager(DRFPermissionManager):\n    def has_create_permission(self) -> bool:\n        return self.user.is_staff\n\n    def has_update_permission(self) -> bool:\n        return self.user.is_staff\n\n    def has_delete_permission(self) -> bool:\n        return self.user.is_staff\n\n    def has_view_permission(self) -> bool:\n        return True\n\n    def has_list_permission(self) -> bool:\n        return True\n\n    def has_custom_permission(self) -> bool:\n        return PermissionResult(\n            message=\"You can't do it\",\n            value=self.user.is_staff,\n        )\n\n\n# Define model with permission manager attribute\nclass SomeModel(models.Model):\n    permission_manager = SomeModelPermissionManager\n    ...\n\n\n# ViewSet\nclass TestModelViewSet(ModelViewSet):\n    permission_classes = [IsAuthenticated, ManagerPermission]\n    ...\n\n    @action(detail=True)\n    def custom(self, request, **kwargs):\n        ...\n```\nThat's all. Now every drf action will be checked by the permission manager.\n\nAlso, you can use the serializer field for retrieve permissions you need.\n\n```python\nfrom permission_manager_drf import PermissionField\nfrom rest_framework.serializers import ModelSerializer\n\nclass SomeModelSerializer(ModelSerializer):\n    permissions = PermissionField(actions=('view', 'custom'),)\n    ...\n\n\"\"\"\nExample output:\n{\n    ...,\n    'permissions': {\n        'view': {\n            'allow': True,\n            'messages': None,\n        },\n        'custom': {\n            'allow': False,\n            'messages': [\"You can't do it\"],\n        },\n    }\n}\n\"\"\"\n```\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A simple way to manage object permissions in drf.",
    "version": "0.3.1",
    "project_urls": {
        "Homepage": "https://github.com/kindlycat/permission-manager-drf",
        "Repository": "https://github.com/kindlycat/permission-manager-drf"
    },
    "split_keywords": [
        "permissions",
        " drf",
        " rest framework"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "20e3d88060831a8cab873afd155e62072d6857b057401527c481a8b2064287e6",
                "md5": "c6087b1c409e7abab8607159173d4870",
                "sha256": "3e61dcb2a13012802ca7997b8ed12c1a899f2a91f7774d2da6bbff9772f93c8c"
            },
            "downloads": -1,
            "filename": "permission_manager_drf-0.3.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c6087b1c409e7abab8607159173d4870",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 8709,
            "upload_time": "2024-08-04T19:20:07",
            "upload_time_iso_8601": "2024-08-04T19:20:07.719464Z",
            "url": "https://files.pythonhosted.org/packages/20/e3/d88060831a8cab873afd155e62072d6857b057401527c481a8b2064287e6/permission_manager_drf-0.3.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "6d56ee19e5196d18f7d807b1ed66a4091bdbeb32fcddf995bf8369efefb777d5",
                "md5": "51e4d9c763f58bdc656d4a3346566827",
                "sha256": "918e23fb06d49f9e579f66239d86f2bae6bb17300685f73f90c2f3d5a7368bb1"
            },
            "downloads": -1,
            "filename": "permission_manager_drf-0.3.1.tar.gz",
            "has_sig": false,
            "md5_digest": "51e4d9c763f58bdc656d4a3346566827",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 7521,
            "upload_time": "2024-08-04T19:20:09",
            "upload_time_iso_8601": "2024-08-04T19:20:09.128401Z",
            "url": "https://files.pythonhosted.org/packages/6d/56/ee19e5196d18f7d807b1ed66a4091bdbeb32fcddf995bf8369efefb777d5/permission_manager_drf-0.3.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-04 19:20:09",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "kindlycat",
    "github_project": "permission-manager-drf",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "permission-manager-drf"
}
        
Elapsed time: 0.30084s