Created
April 17, 2012 17:44
-
-
Save rca/2407724 to your computer and use it in GitHub Desktop.
Adding URI to TastyPie resource
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NotebookResource(BackboneJSResource): | |
user = fields.ForeignKey(UserResource, 'user') | |
class Meta: | |
queryset = Notebook.objects.all() | |
authentication = MULTI_AUTH | |
authorization = OwnerAuthorization() | |
# allow to list posts for this notebook | |
posts_allowed_methods = ['get', 'post'] | |
filtering = {'id': ALL} | |
def get_posts(self, request, **kwargs): | |
kwargs['notebook__id'] = kwargs['pk'] | |
return PostResource().get_list(request, **kwargs) | |
def post_posts(self, request, **kwargs): | |
""" | |
Creates a new post | |
""" | |
kwargs['notebook'] = Notebook.objects.get(pk=kwargs.pop('pk')) | |
return PostResource().post_list(request, **kwargs) | |
def dispatch_posts(self, request, **kwargs): | |
""" | |
A view for handling the various HTTP methods (GET/POST/PUT/DELETE) on | |
a single resource. | |
Relies on ``Resource.dispatch`` for the heavy-lifting. | |
""" | |
return self.dispatch('posts', request, **kwargs) | |
def override_urls(self): | |
return [ | |
url(r"^(?P<resource_name>%s)/(?P<pk>\w[\w/-]*)/posts%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('dispatch_posts'), name="api_dispatch_list_posts"), | |
] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment