Navigation Menu

Skip to content

Commit

Permalink
Added basic canonical support.
Browse files Browse the repository at this point in the history
  • Loading branch information
toastdriven committed Apr 8, 2010
1 parent 7c41e88 commit 3af852d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 9 deletions.
4 changes: 3 additions & 1 deletion TODO
Expand Up @@ -12,7 +12,9 @@ Short Term
- Implement throttling.
- Documentation.
- Filtering
- Pagination
- A ``ResourceURI`` field.
- A ``readonly`` attribute on fields.


Long Term
---------
Expand Down
6 changes: 6 additions & 0 deletions example/api/representations.py
Expand Up @@ -3,5 +3,11 @@


class NoteRepresentation(ModelRepresentation):
resource_uri = CharField()

class Meta:
queryset = Note.objects.all()

# FIXME: This should probably get significantly more automated.
def dehydrate_resource_uri(self, obj):
return self.get_resource_uri(obj)
2 changes: 1 addition & 1 deletion example/api/urls.py
Expand Up @@ -3,7 +3,7 @@
from api.resources import NoteResource

api = Api(api_name='v1')
api.register(NoteResource())
api.register(NoteResource(), canonical=True)

urlpatterns = patterns('',
(r'^', include(api.urls)),
Expand Down
17 changes: 11 additions & 6 deletions tastypie/api.py
Expand Up @@ -2,7 +2,7 @@
from django.core.exceptions import ImproperlyConfigured
from django.core.urlresolvers import reverse
from django.http import HttpResponse
from tastypie.exceptions import TastyPieError
from tastypie.exceptions import TastyPieError, NotRegistered
from tastypie.serializers import Serializer


Expand All @@ -27,16 +27,21 @@ def register(self, resource, canonical=False):

self._registry[resource_name] = resource

# FIXME: Not sure about this yet.
# if canonical is True:
# self._canonicals[resource_name] = resource
if canonical is True:
self._canonicals[resource_name] = resource

def unregister(self, resource_name):
if resource_name in self._registry:
del(self._registry[resource_name])
return True

return False
if resource_name in self._canonicals:
del(self._canonicals[resource_name])

def canonical_resource_for(self, resource_name):
if resource_name in self._canonicals:
return self._canonicals[resource_name]

raise NotRegistered("No resource was registered as canonical for '%s'." % resource_name)

def wrap_view(self, view):
def wrapper(request, *args, **kwargs):
Expand Down
4 changes: 4 additions & 0 deletions tastypie/exceptions.py
Expand Up @@ -2,6 +2,10 @@ class TastyPieError(Exception):
pass


class NotRegistered(TastyPieError):
pass


class NotFound(TastyPieError):
pass

Expand Down
20 changes: 19 additions & 1 deletion tests/core/tests/api.py
Expand Up @@ -2,6 +2,7 @@
from django.http import HttpRequest
from django.test import TestCase
from tastypie.api import Api
from tastypie.exceptions import NotRegistered
from tastypie.resources import Resource
from tastypie.representations.models import ModelRepresentation
from core.models import Note
Expand Down Expand Up @@ -45,16 +46,24 @@ def test_register(self):
api.register(UserResource())
self.assertEqual(len(api._registry), 2)
self.assertEqual(sorted(api._registry.keys()), ['notes', 'users'])

self.assertEqual(len(api._canonicals), 0)
api.register(UserResource(), canonical=True)
self.assertEqual(len(api._registry), 2)
self.assertEqual(sorted(api._registry.keys()), ['notes', 'users'])
self.assertEqual(len(api._canonicals), 1)

def test_unregister(self):
api = Api()
api.register(NoteResource())
api.register(UserResource())
api.register(UserResource(), canonical=True)
self.assertEqual(sorted(api._registry.keys()), ['notes', 'users'])

self.assertEqual(len(api._canonicals), 1)
api.unregister('users')
self.assertEqual(len(api._registry), 1)
self.assertEqual(sorted(api._registry.keys()), ['notes'])
self.assertEqual(len(api._canonicals), 0)

api.unregister('notes')
self.assertEqual(len(api._registry), 0)
Expand All @@ -64,6 +73,15 @@ def test_unregister(self):
self.assertEqual(len(api._registry), 0)
self.assertEqual(sorted(api._registry.keys()), [])

def test_canonical_resource_for(self):
api = Api()
api.register(NoteResource())
api.register(UserResource(), canonical=True)
self.assertEqual(len(api._canonicals), 1)

self.assertRaises(NotRegistered, api.canonical_resource_for, 'notes')
self.assertEqual(isinstance(api.canonical_resource_for('users'), UserResource), True)

def test_urls(self):
api = Api()
api.register(NoteResource())
Expand Down

0 comments on commit 3af852d

Please sign in to comment.