{ "info": { "author": "Ryan P Kilby", "author_email": "kilbyr@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 5 - Production/Stable", "Environment :: Web Environment", "Framework :: Django", "Framework :: Django :: 2.1", "Framework :: Django :: 2.2", "Intended Audience :: Developers", "License :: OSI Approved :: BSD License", "Operating System :: OS Independent", "Programming Language :: Python", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Topic :: Internet :: WWW/HTTP" ], "description": "# django-rest-framework-guardian\n\n[![CircleCI](https://circleci.com/gh/rpkilby/django-rest-framework-guardian.svg?style=shield)](https://circleci.com/gh/rpkilby/django-rest-framework-guardian)\n[![Codecov](https://codecov.io/gh/rpkilby/django-rest-framework-guardian/branch/master/graph/badge.svg)](https://codecov.io/gh/rpkilby/django-rest-framework-guardian)\n[![License](https://img.shields.io/pypi/l/djangorestframework-guardian.svg)](https://pypi.org/project/djangorestframework-guardian)\n[![Version](https://img.shields.io/pypi/v/djangorestframework-guardian.svg)](https://pypi.org/project/djangorestframework-guardian)\n[![Python](https://img.shields.io/pypi/pyversions/djangorestframework-guardian.svg)](https://pypi.org/project/djangorestframework-guardian/)\n\ndjango-rest-framework-guardian provides django-guardian integrations for Django REST Framework.\nCurrently, this only includes the `ObjectPermissionsFilter`.\n\n\n## Installation & Setup\n\nTo use django-rest-framework-guardian, install it into your environment.\n\n```sh\n$ pip install djangorestframework-guardian\n```\n\nEnsure both Django REST Framework and django-guardian are configured and added to your `INSTALLED_APPS` setting.\n\n```python\nINSTALLED_APPS = [\n 'rest_framework',\n 'guardian',\n]\n\nAUTHENTICATION_BACKENDS = [\n 'django.contrib.auth.backends.ModelBackend',\n 'guardian.backends.ObjectPermissionBackend',\n]\n```\n\n\n## ObjectPermissionsFilter\n\nThe filter will ensure that querysets only returns objects for which the user has the appropriate view permission.\n\nIf you're using `ObjectPermissionsFilter`, you'll probably also want to add an appropriate object permissions\nclass, to ensure that users can only operate on instances if they have the appropriate object permissions. The easiest\nway to do this is to subclass `DjangoObjectPermissions` and add `'view'` permissions to the `perms_map` attribute.\n\nAn example using both `ObjectPermissionsFilter` and `DjangoObjectPermissions` might look like the following:\n\n**permissions.py**:\n\n```python\nfrom rest_framework import permissions\n\n\nclass CustomObjectPermissions(permissions.DjangoObjectPermissions):\n \"\"\"\n Similar to `DjangoObjectPermissions`, but adding 'view' permissions.\n \"\"\"\n perms_map = {\n 'GET': ['%(app_label)s.view_%(model_name)s'],\n 'OPTIONS': ['%(app_label)s.view_%(model_name)s'],\n 'HEAD': ['%(app_label)s.view_%(model_name)s'],\n 'POST': ['%(app_label)s.add_%(model_name)s'],\n 'PUT': ['%(app_label)s.change_%(model_name)s'],\n 'PATCH': ['%(app_label)s.change_%(model_name)s'],\n 'DELETE': ['%(app_label)s.delete_%(model_name)s'],\n }\n```\n\n**views.py**:\n\n```python\nfrom rest_framework import viewsets\nfrom rest_framework_guardian import filters\n\nfrom myapp.models import Event\nfrom myapp.permissions import CustomObjectPermissions\nfrom myapp.serializers import EventSerializer\n\n\nclass EventViewSet(viewsets.ModelViewSet):\n \"\"\"\n Viewset that only lists events if user has 'view' permissions, and only\n allows operations on individual events if user has appropriate 'view', 'add',\n 'change' or 'delete' permissions.\n \"\"\"\n queryset = Event.objects.all()\n serializer_class = EventSerializer\n permission_classes = [CustomObjectPermissions]\n filter_backends = [filters.ObjectPermissionsFilter]\n```\n\n\n## ObjectPermissionsAssignmentMixin\n\nA serializer mixin that allows permissions to be easily assigned to users and/or groups.\nSo each time an object is created or updated, the `permissions_map` returned by `Serializer.get_permissions_map` will be used to assign permission(s) to that object.\n\nPlease note that the existing permissions will remain intact.\n\nA usage example might look like the following:\n\n```python\nfrom rest_framework_guardian.serializers import ObjectPermissionsAssignmentMixin\n\nfrom blog.models import Post\n\n\nclass PostSerializer(ObjectPermissionsAssignmentMixin, serializers.ModelSerializer):\n class Meta:\n model = Post\n fields = '__all__'\n\n def get_permissions_map(self, created):\n current_user = self.context['request'].user\n readers = Group.objects.get(name='readers')\n supervisors = Group.objects.get(name='supervisors')\n\n return {\n 'view_post': [current_user, readers],\n 'change_post': [current_user],\n 'delete_post': [current_user, supervisors]\n }\n\n```\n\n\n## Release Process\n\n- Update changelog\n- Update package version in setup.py\n- Create git tag for version\n- Build & upload release to PyPI\n ```bash\n $ pip install -U pip setuptools wheel twine\n $ rm -rf dist/ build/\n $ python setup.py bdist_wheel\n $ twine upload dist/*\n ```\n\n## License\n\nSee: [LICENSE](https://github.com/rpkilby/django-rest-framework-guardian/blob/master/LICENSE)\n\nBSD 3-Clause License\n\nCopyright (c) 2018, Ryan P Kilby\nAll rights reserved.\n\nRedistribution and use in source and binary forms, with or without\nmodification, are permitted provided that the following conditions are met:\n\n* Redistributions of source code must retain the above copyright notice, this\n list of conditions and the following disclaimer.\n\n* Redistributions in binary form must reproduce the above copyright notice,\n this list of conditions and the following disclaimer in the documentation\n and/or other materials provided with the distribution.\n\n* Neither the name of the copyright holder nor the names of its\n contributors may be used to endorse or promote products derived from\n this software without specific prior written permission.\n\nTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"\nAND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\nIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\nDISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\nFOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\nDAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\nSERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\nCAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\nOR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\nOF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/rpkilby/django-rest-framework-guardian", "keywords": "", "license": "BSD", "maintainer": "", "maintainer_email": "", "name": "djangorestframework-guardian", "package_url": "https://pypi.org/project/djangorestframework-guardian/", "platform": "", "project_url": "https://pypi.org/project/djangorestframework-guardian/", "project_urls": { "Homepage": "https://github.com/rpkilby/django-rest-framework-guardian", "Source": "https://github.com/rpkilby/django-rest-framework-guardian", "Tracker": "https://github.com/rpkilby/django-rest-framework-guardian/issues" }, "release_url": "https://pypi.org/project/djangorestframework-guardian/0.3.0/", "requires_dist": [ "django", "djangorestframework", "django-guardian" ], "requires_python": "", "summary": "django-guardian support for Django REST Framework", "version": "0.3.0" }, "last_serial": 5969836, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "5b8e48eca00471a528fb7df5da59dafd", "sha256": "1ce44acfce6f170cb1543ceebd8fccb78bdac1061d8570fc9b29899c4137f38d" }, "downloads": -1, "filename": "djangorestframework_guardian-0.1.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "5b8e48eca00471a528fb7df5da59dafd", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 4844, "upload_time": "2018-07-10T01:34:26", "url": "https://files.pythonhosted.org/packages/38/24/b74714b5c0f34a49ef7d20ff3f8fea3c90e681982f17153e115c530156db/djangorestframework_guardian-0.1.0-py2.py3-none-any.whl" } ], "0.1.1": [ { "comment_text": "", "digests": { "md5": "06e1d637943a439368d814ec7aa5821b", "sha256": "32d723a5c62f75b72c618312358b1c186ec1c1fa95ffc2169e125df62ef20015" }, "downloads": -1, "filename": "djangorestframework_guardian-0.1.1-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "06e1d637943a439368d814ec7aa5821b", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 4850, "upload_time": "2018-11-03T04:53:51", "url": "https://files.pythonhosted.org/packages/bf/b8/c369c08637f32523f05560f48381cb19f1f2a3dceeb68ff4f33508c39400/djangorestframework_guardian-0.1.1-py2.py3-none-any.whl" } ], "0.2.0": [ { "comment_text": "", "digests": { "md5": "65b26ca82edf76452cbd64bb8c358b3b", "sha256": "e9c027e36f262fec9dc4c38877e818bf6a4815f56e181a3fb0357c3c52dd8088" }, "downloads": -1, "filename": "djangorestframework_guardian-0.2.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "65b26ca82edf76452cbd64bb8c358b3b", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 5522, "upload_time": "2019-07-20T01:26:31", "url": "https://files.pythonhosted.org/packages/a5/aa/0db7fd598ab237fab2f770e0a18c35ea0d495eaba6d0f26b18e289468ab6/djangorestframework_guardian-0.2.0-py2.py3-none-any.whl" } ], "0.3.0": [ { "comment_text": "", "digests": { "md5": "e6c1c3eee9218838aa754b22527e2d65", "sha256": "3bd3dd6ea58e1bceca5048faf6f8b1a93bb5dcff30ba5eb91b9a0e190a48a0c7" }, "downloads": -1, "filename": "djangorestframework_guardian-0.3.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "e6c1c3eee9218838aa754b22527e2d65", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 6931, "upload_time": "2019-08-02T01:00:39", "url": "https://files.pythonhosted.org/packages/9b/cc/35c1d8fb99172b2646f29e270e9ec443ffe09e0b63e61cd528d4fb4b8b07/djangorestframework_guardian-0.3.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "094e87bbeb9c28878636f5452980fd1e", "sha256": "1883756452d9bfcc2a51fb4e039a6837a8f6697c756447aa83af085749b59330" }, "downloads": -1, "filename": "djangorestframework-guardian-0.3.0.tar.gz", "has_sig": false, "md5_digest": "094e87bbeb9c28878636f5452980fd1e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8647, "upload_time": "2019-10-14T04:24:25", "url": "https://files.pythonhosted.org/packages/e5/80/0f2190bacfe7c7b2e22d0e1e695882ec3123f9e58817c8392a258cd46442/djangorestframework-guardian-0.3.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "e6c1c3eee9218838aa754b22527e2d65", "sha256": "3bd3dd6ea58e1bceca5048faf6f8b1a93bb5dcff30ba5eb91b9a0e190a48a0c7" }, "downloads": -1, "filename": "djangorestframework_guardian-0.3.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "e6c1c3eee9218838aa754b22527e2d65", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": null, "size": 6931, "upload_time": "2019-08-02T01:00:39", "url": "https://files.pythonhosted.org/packages/9b/cc/35c1d8fb99172b2646f29e270e9ec443ffe09e0b63e61cd528d4fb4b8b07/djangorestframework_guardian-0.3.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "094e87bbeb9c28878636f5452980fd1e", "sha256": "1883756452d9bfcc2a51fb4e039a6837a8f6697c756447aa83af085749b59330" }, "downloads": -1, "filename": "djangorestframework-guardian-0.3.0.tar.gz", "has_sig": false, "md5_digest": "094e87bbeb9c28878636f5452980fd1e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8647, "upload_time": "2019-10-14T04:24:25", "url": "https://files.pythonhosted.org/packages/e5/80/0f2190bacfe7c7b2e22d0e1e695882ec3123f9e58817c8392a258cd46442/djangorestframework-guardian-0.3.0.tar.gz" } ] }