{ "info": { "author": "Alan Descoins, Mart\u00edn Santos", "author_email": "alan@tryolabs.com, santos@tryolabs.com", "bugtrack_url": null, "classifiers": [], "description": "==========================================\n Django TastyPie's Extended ModelResource\n==========================================\n\nThe ``ExtendedModelResource`` is an extension for TastyPie's ``ModelResource`` that adds some interesting features:\n\n* Supports easily using resources as *nested* of another resource, with proper authorization checks for each case.\n* [This feature has already been included in the official TastyPie] Supports using a custom identifier attribute for resources in uris (not only the object's pk!)\n\n\nRequirements\n============\n\nRequired\n--------\n* Latest django-tastypie from repository (0.9.12-alpha or hopefully greater) and its requirements.\n\nOptional\n--------\n* Django 1.4.1 for the sample project.\n\n\nInstallation\n============\n\nClone repository and do:\n\n python setup.py install\n\nOr just do\n\n pip install django-tastypie-extendedmodelresource\n\nto get the latest version from `pypi `_.\n\n\n*Nested* resources\n==================\n\nHere we explain what we mean by *nested resources* and what a simple use case would be.\n\nRationale\n---------\n\nImagine you have a simple application which has users, each of which can write any number of entries. Every entry is associated to a user. For example ::\n\n from django.contrib.auth.models import User\n from django.db import models\n\n\n class Entry(models.Model):\n user = models.ForeignKey(User, related_name='entries')\n # ... more fields\n\nThe 'standard' TastyPie models for this would be ::\n\n from django.contrib.auth.models import User\n from tastypie.resources import ModelResource\n \n from myapp.models import Entry\n\n\n class UserResource(ModelResource):\n class Meta:\n queryset = User.objects.all()\n \n class EntryResource(ModelResource):\n class Meta:\n queryset = Entry.objects.all()\n\n\nThis gives you full CRUD ability over users and entries, with uris such as ``/api/user/`` and ``/api/entry/``.\n\nNow imagine you want to be able to easily list all the entries authored by a given user, with a uri such as ``/api/user//entries``. Additionally, you would like to be able to POST to this uri and create an entry automatically associated to this user. This is why nested resources are for.\n\nIf a resource such as the ``EntryResource`` is to be accessed as ``/api/user//`` where ```` is custom-defined (for example ``entries``), then we say the ``EntryResource`` is being used as **nested** of the ``UserResource``. We also say that ``UserResource`` is the **parent** of ``EntryResource``.\n\nThe standard TastyPie's ``ModelResource`` would force you to write a function overriding the urls of the ``UserResource`` and adding a method to handle the entry resource (see `Nested Resources `_). Using ``ExtendedModelResource`` it is as easy as ::\n\n from django.contrib.auth.models import User\n from tastypie import fields\n\n from extendedmodelresource import ExtendedModelResource\n from myapp.models import Entry\n\n\n class UserResource(ExtendedModelResource):\n class Meta:\n queryset = User.objects.all()\n\n class Nested:\n entries = fields.ToManyField('api.resources.EntryResource', 'entries')\n\n\n class EntryResource(ExtendedModelResource):\n user = fields.ForeignKey(UserResource, 'user')\n\n class Meta:\n queryset = Entry.objects.all()\n \nAnd that's it!\n\n\nHow authorization is handled\n----------------------------\nIf a resource does not have a nested resource, the authorization is handled the same way as in the standard TastyPie. You define an ``Authorization`` class and associate it to the resource. This class may implement the ``is_authorized`` and ``apply_limits`` methods.\n\nFor an ``ExtendedModelResource`` with nesteds, all the authorization when using the nested as such is handled from the authorization class **of the parent resource**. For each resource used as nested, the ``Authorization`` class of the parent can implement two methods:\n\n* ``is_authorized_nested_``\n* ``apply_limits_nested_``\n\nwhere ```` is the name of the attribute parameter in the ``ApiField`` that declares the resource as nested. These functions work identically to the original ones, except that they also receive a ``parent_object`` parameter which will contain the parent object.\n\nFor our users and entries example, an ``Authorization`` can be something like::\n\n from tastypie.authorization import Authorization\n \n \n class UserResourceAuthorization(Authorization):\n \"\"\"\n Our Authorization class for UserResource and its nested.\n \"\"\"\n \n def is_authorized(self, request, object=None):\n # Only 'newton' is authorized to view the users\n if 'newton' in request.user.username:\n return True\n \n return False\n \n def apply_limits(self, request, object_list):\n return object_list.all()\n \n def is_authorized_nested_entries(self, request,\n parent_object, object=None):\n # Is request.user authorized to access the EntryResource as\n # nested?\n return True\n \n def apply_limits_nested_entries(self, request, parent_object,\n object_list):\n # Advanced filtering.\n # Note that object_list already only contains the objects that\n # are associated to parent_object.\n return object_list.all()\n\nCaveats\n-------\n* ``ExtendedModelResource`` only supports one level nesting.\n* Resources used as nested can also be registered in an **Api** instance, but need not to. That is, there can be resources used **only** as nested and not exposed otherwise in the urls.\n\n\nChanging object's identifier attribute in urls\n==============================================\n\nUsing the latest TastyPie you can define a ``detail_uri_name`` attribute\nin the ``Meta`` class, to use a different attribute than the object's ``pk`` ::\n\n class UserResource(ExtendedModelResource):\n class Meta:\n queryset = User.objects.all()\n detail_uri_name = 'username'\n\nWith ``ExtendedModelResource`` you can change the regular expression used for your identifier attribute in the urls, you can override the method ``get_url_id_attribute_regex`` and return it, like the following example ::\n\n def get_detail_uri_name_regex(self):\n return r'[aA-zZ][\\w-]*'\n\nMore information\n================\n\n:Date: 08-20-2012\n:Version: 0.2\n:Authors:\n - Alan Descoins - Tryolabs \n - Mart\u00edn Santos - Tryolabs \n\n:Website:\n https://github.com/tryolabs/django-tastypie-extendedmodelresource", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/tryolabs/django-tastypie-extendedmodelresource", "keywords": "REST RESTful tastypie django resource nested extension", "license": "BSD", "maintainer": null, "maintainer_email": null, "name": "django-tastypie-extendedmodelresource", "package_url": "https://pypi.org/project/django-tastypie-extendedmodelresource/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/django-tastypie-extendedmodelresource/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/tryolabs/django-tastypie-extendedmodelresource" }, "release_url": "https://pypi.org/project/django-tastypie-extendedmodelresource/0.22/", "requires_dist": null, "requires_python": null, "summary": "An extension of TastyPie's ModelResource to easily support nested resources, and more.", "version": "0.22" }, "last_serial": 790826, "releases": { "0.1": [ { "comment_text": "", "digests": { "md5": "eb28081a9d19240cb66f8548467e0303", "sha256": "52b6d1e128ad1e5004621594eb8764c35bd5fa6dbdf60e247cee4534c5d03495" }, "downloads": -1, "filename": "django-tastypie-extendedmodelresource-0.1.tar.gz", "has_sig": false, "md5_digest": "eb28081a9d19240cb66f8548467e0303", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 12761, "upload_time": "2012-04-19T06:38:08", "url": "https://files.pythonhosted.org/packages/e4/2d/0297f7c6694b2666d79d8e31eb0c115fa3726e5724750a9990e5048a8be2/django-tastypie-extendedmodelresource-0.1.tar.gz" } ], "0.2": [ { "comment_text": "", "digests": { "md5": "21910c53302a98dc794a41e95976081d", "sha256": "d601c43f6a1e45d2be7ec2ea101b3fed79a9129b06780e46b8c2e64c341e458c" }, "downloads": -1, "filename": "django-tastypie-extendedmodelresource-0.2.tar.gz", "has_sig": false, "md5_digest": "21910c53302a98dc794a41e95976081d", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 11974, "upload_time": "2012-08-20T15:01:12", "url": "https://files.pythonhosted.org/packages/53/d4/00b0f2348152476aa8a162bb89ba78b489b6ed765e34ea639050b76991e6/django-tastypie-extendedmodelresource-0.2.tar.gz" } ], "0.21": [ { "comment_text": "", "digests": { "md5": "c459ad84ace650a0ba2cb3c21a115e9d", "sha256": "69866322b19c0fbf719507aa73767b9a3ce4b8caa797552fd25ea1ce2951ed15" }, "downloads": -1, "filename": "django-tastypie-extendedmodelresource-0.21.tar.gz", "has_sig": false, "md5_digest": "c459ad84ace650a0ba2cb3c21a115e9d", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 12090, "upload_time": "2012-08-20T22:44:01", "url": "https://files.pythonhosted.org/packages/24/b3/fad0050efad7cc2de1722532e4926c63d58f2661997465a02e0138dc8a3d/django-tastypie-extendedmodelresource-0.21.tar.gz" } ], "0.22": [ { "comment_text": "", "digests": { "md5": "38172a12fa6a44999621cba08edc38b1", "sha256": "99f5478d519d4ecc5b6f56e8a116d6ba79d64bc580b553bb6418fbaa67325633" }, "downloads": -1, "filename": "django-tastypie-extendedmodelresource-0.22.tar.gz", "has_sig": false, "md5_digest": "38172a12fa6a44999621cba08edc38b1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 12071, "upload_time": "2012-08-20T22:45:40", "url": "https://files.pythonhosted.org/packages/15/dc/2de81545af7bfbbc8b5f12a2323459934be70de7d2ec35eee62c990cc179/django-tastypie-extendedmodelresource-0.22.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "38172a12fa6a44999621cba08edc38b1", "sha256": "99f5478d519d4ecc5b6f56e8a116d6ba79d64bc580b553bb6418fbaa67325633" }, "downloads": -1, "filename": "django-tastypie-extendedmodelresource-0.22.tar.gz", "has_sig": false, "md5_digest": "38172a12fa6a44999621cba08edc38b1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 12071, "upload_time": "2012-08-20T22:45:40", "url": "https://files.pythonhosted.org/packages/15/dc/2de81545af7bfbbc8b5f12a2323459934be70de7d2ec35eee62c990cc179/django-tastypie-extendedmodelresource-0.22.tar.gz" } ] }