{ "info": { "author": "Francis Mitra", "author_email": "francismitra@gmail.com", "bugtrack_url": null, "classifiers": [], "description": "[![PyPI version](https://badge.fury.io/py/django-rest-secureview.svg)](https://badge.fury.io/py/django-rest-secureview)\n[![Build Status](https://travis-ci.org/fmitra/django-rest-secureview.svg?branch=master)](https://travis-ci.org/fmitra/django-rest-secureview)\n\n\n# django-rest-secureview\n\nA collection of decorators to enforce conditions on [Django REST Framework](http://www.django-rest-framework.org/) API routes that are accessed by the User. At the moment, it focuses on enforcing specific model keys in POST params, or requiring that a User has a foreign key relation to a model they are accessing. \n\n## Requirements\n\n1. Python 2 or Python 3\n2. Django\n3. Django REST Framework\n\n## Usuage\n\n1. Install package\n\n\t`pip install django-rest-secureview`\n\n2. Import decorators and viewset rules\n\n\t``` python\n\tfrom django_rest_secureview.decorators import require\n\tfrom django_rest_secureview.view_rules import Params\n\t```\n\n3. Decorate ViewSets\n\n\t``` python\n\tfrom rest_framework.viewsets import ModelViewSet\n\tfrom django_rest_secureview.decorators import require\n\tfrom django_rest_secureview.view_rule import Params\n\t\n\tclass ApiEndpoint(ModelViewSet):\n\t @require(Params, params=['dog', 'cat'])\n\t def create(self, request):\n\t pass\n\t```\n\n## ViewRules\n\nViewRules contain the logic behind the API conditions being enforced. Using the `require` decorator requires a ViewRule at the minimum. The default ViewRules in this package all expect a certain keyword arguments to compliment them. If you need additional functionality, you can create your own ViewRule (explained below)\n\n1. `Params`: Requires specific POST params to be submitted in reqeusts\n * kwargs: `params` list\n * Usuage: `@require(Params, params=['animal', 'name'])`\n\n2. `Owner`: Requires authenticated User to have a foreign key relationship with the requested Model instance in a detailed endpint \n\t* kwargs: `model` Django Model\n\t* Usuage: `@require(Owner, model=Pet)`\n\t* Note: This only works in detailed endpoints using the default `pk` attribute \n\n3. `OwnerParams`: Combinds both `Params` and `Owner` conditions\n\t* kwargs: `model` Django Model, `params` list\n\t* Usuage: `@require(Owner, model=Pet, params=['animal'])`\n\t* Note: This only works in detailed endpoints using the default `pk` attribute \n\n## Decorators\n\n1. `require`: Accepts a ViewRule detailing API conditions and an arbitrary number of keyword values. For more information, reference the ViewRule documentation.\n\n\t``` python\n\t# Expects an animal and name param to be provided in the POST request\n\t@require(Params, params=['animal', 'name'])\n\tdef create(self, request):\n\t pass\n\n\t# Expects the accessing User to have a foreign key relation to \n\t# the Pet model\n\t@require(Owner, model=Pet)\n\tdef retrieve(self, request, pk=None):\n\t pass\n\t\n\t# Expects the accessing User to have a foreign key relation to \n\t# the Pet model as well as name and color values in the POST params\n\t@require_owner(OwnerParams, model=Pet, params=['name', 'color'])\n\tdef update(self, request, pk=None):\n\t pass\n\t```\n\n## Creating Your Own ViewRules\n\nWe can create our own ViewRules by extending the ViewRule abstract class supplied in the view_rules module and implementing an `enforce` method. For convenience, HTTP status codes from the Django REST Framework are available in the class's `status` attribute and arguments in the request object are available in the class's `request` attribute.\n\nAll ViewRules are expected to have an `enforce` method that returns a JSON-serializable response if specific conditions are not met. If conditions are not met, the ViewSet will deliver this response instead of the response defined within the ViewSet's action method. \n\nMore status codes: [REST Framework Status Codes](http://www.django-rest-framework.org/api-guide/status-codes/)\n\n``` python\nfrom django_rest_secureview.view_rules import ViewRule\n\nclass MyCondition(ViewRule):\n def enforce(self, params=None):\n # Check who the User is if necessary\n # user = self.request.user\n # Some rules here\n condition_met = False\n if not condition_met:\n\t code = self.status.HTTP_400_BAD_REQUEST\n\t notice = {\"detail\":\"Whoops something went wrong\"}\n return self.response(data=notice, status=code)\n```\n\n## Testing\n\n[Tox](https://tox.testrun.org) should be in your virtual environment in order to run tests. Other dependencies, such as the Django REST Framework will be packaged separately in Tox's virtual testing environment. \n\n``` sh\n./run_tests.sh\n```", "description_content_type": null, "docs_url": null, "download_url": "https://github.com/fmitra/django-rest-secureview/tarball/1.0.0", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/fmitra/django-rest-secureview", "keywords": "django,django-restframework", "license": "MIT", "maintainer": null, "maintainer_email": null, "name": "django-rest-secureview", "package_url": "https://pypi.org/project/django-rest-secureview/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/django-rest-secureview/", "project_urls": { "Download": "https://github.com/fmitra/django-rest-secureview/tarball/1.0.0", "Homepage": "https://github.com/fmitra/django-rest-secureview" }, "release_url": "https://pypi.org/project/django-rest-secureview/1.0.0/", "requires_dist": null, "requires_python": null, "summary": "Enforces client ViewSet requirements for Django Rest Framework", "version": "1.0.0" }, "last_serial": 2551622, "releases": { "0.0.1": [ { "comment_text": "", "digests": { "md5": "5891bee5814b9ad34a9184ce81ec087b", "sha256": "0ed7ab01900950a43f79bb16a0793b70cff497f53aff3aae8193cf90de177b62" }, "downloads": -1, "filename": "django-rest-secureview-0.0.1.tar.gz", "has_sig": false, "md5_digest": "5891bee5814b9ad34a9184ce81ec087b", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4003, "upload_time": "2016-12-27T05:30:31", "url": "https://files.pythonhosted.org/packages/8f/6f/c7d9284c5a08f5cab073acafc6d646d75cda42f8b51a84a16fd767de083c/django-rest-secureview-0.0.1.tar.gz" } ], "0.0.2": [ { "comment_text": "", "digests": { "md5": "5958863ac83053dc3ce12a486e192419", "sha256": "094de203278fb79ac2002d6446b6b682018ccccd0100281bd8cf0b37b0834ed8" }, "downloads": -1, "filename": "django-rest-secureview-0.0.2.tar.gz", "has_sig": false, "md5_digest": "5958863ac83053dc3ce12a486e192419", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5355, "upload_time": "2016-12-28T09:04:29", "url": "https://files.pythonhosted.org/packages/91/b2/5880e230e54c940045a576a902a09fa4795b05972b37f9ac6dc8bf6fbf25/django-rest-secureview-0.0.2.tar.gz" } ], "0.0.3": [ { "comment_text": "", "digests": { "md5": "991f2139532e71a8faa5bd2c5aa02d21", "sha256": "92f2fd3c26325267cc40a3c24aafdba3bf6d3b1062c9d859ceb040428e80e815" }, "downloads": -1, "filename": "django-rest-secureview-0.0.3.tar.gz", "has_sig": false, "md5_digest": "991f2139532e71a8faa5bd2c5aa02d21", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5360, "upload_time": "2016-12-28T09:15:56", "url": "https://files.pythonhosted.org/packages/cd/9d/a7ea42cb44d4dbb690d70948fafd72cf8d3882fdd4725ed2200e4ab09381/django-rest-secureview-0.0.3.tar.gz" } ], "0.0.4": [ { "comment_text": "", "digests": { "md5": "9567a2011d37836c3aa23e9d5c51c73c", "sha256": "2089bc5e2e31a805034a5063b9c2946e3fa5690a490742adbb64603844568f06" }, "downloads": -1, "filename": "django-rest-secureview-0.0.4.tar.gz", "has_sig": false, "md5_digest": "9567a2011d37836c3aa23e9d5c51c73c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5580, "upload_time": "2017-01-01T10:01:04", "url": "https://files.pythonhosted.org/packages/9d/c2/b0aa65919a20821cbe762a46890f9d806c46d91f743447aeb0a0c2e1191b/django-rest-secureview-0.0.4.tar.gz" } ], "1.0.0": [ { "comment_text": "", "digests": { "md5": "78f602120abd698923202002ae277fb9", "sha256": "b1b9db2d9a9f2e50920c86128f7ec2a6b8c78873e700d01fbc75f7d2607fc8d7" }, "downloads": -1, "filename": "django-rest-secureview-1.0.0.tar.gz", "has_sig": false, "md5_digest": "78f602120abd698923202002ae277fb9", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5684, "upload_time": "2017-01-03T14:05:05", "url": "https://files.pythonhosted.org/packages/f8/b1/dd0af475874d3c412d6a7a01002198a6841811ac32c1e5efa4d5d30d2f96/django-rest-secureview-1.0.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "78f602120abd698923202002ae277fb9", "sha256": "b1b9db2d9a9f2e50920c86128f7ec2a6b8c78873e700d01fbc75f7d2607fc8d7" }, "downloads": -1, "filename": "django-rest-secureview-1.0.0.tar.gz", "has_sig": false, "md5_digest": "78f602120abd698923202002ae277fb9", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5684, "upload_time": "2017-01-03T14:05:05", "url": "https://files.pythonhosted.org/packages/f8/b1/dd0af475874d3c412d6a7a01002198a6841811ac32c1e5efa4d5d30d2f96/django-rest-secureview-1.0.0.tar.gz" } ] }