Skip to content
This repository has been archived by the owner on Apr 27, 2020. It is now read-only.

Commit

Permalink
Initialize children dict at registration of each collection and item …
Browse files Browse the repository at this point in the history
…rather than at their instantciation
  • Loading branch information
hadrien committed Mar 5, 2015
1 parent fb35833 commit 02d15e9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions royal/directives.py
Expand Up @@ -97,6 +97,7 @@ def register(config, intr):
parent = find_parent_intr(config.introspector, intr)
cls = intr['cls']
name = intr['name']
cls.children = {}

if parent is None:
# root resource: (/users, /bob)
Expand Down
3 changes: 2 additions & 1 deletion royal/ext/sqla.py
Expand Up @@ -69,7 +69,8 @@ def __init__(self, name, parent, request, entity=None):

def load_entity(self):
if self.entity is None:

if self.entity_cls is None:
raise royal.exceptions.NotFound(self)
# FIXME Naively assume that entity's PK is the list of resource
# __name__ in reversed lineage so PK of /users/123/photos/456 is
# (123, 456). Should also be adapted to support resources
Expand Down
19 changes: 14 additions & 5 deletions royal/resource.py
Expand Up @@ -19,7 +19,7 @@ def includeme(config):
@implementer(IBase)
class Base(object):

children = {}
children = None

def __init__(self, name, parent, request):
self.__name__ = unicode(name)
Expand All @@ -29,7 +29,10 @@ def __init__(self, name, parent, request):
def __getitem__(self, key):
key = unicode(key)
self.on_traversing(key)
return self.children[key](key, self, self.request)
try:
return self.children[key](key, self, self.request)
except TypeError:
raise KeyError(key)

def _not_allowed(self, name):
raise exceptions.MethodNotAllowed(self, name)
Expand Down Expand Up @@ -79,8 +82,11 @@ def name(self):

@property
def links(self):
links = {name: self.resource_url(cls(name, self, self.request))
for name, cls in self.children.iteritems()}
links = ({name: self.resource_url(cls(name, self, self.request))
for name, cls in self.children.iteritems()}
if self.children
else {}
)
links['self'] = self.url()
return links

Expand Down Expand Up @@ -112,4 +118,7 @@ def __getitem__(self, key):
self.on_traversing(key)
if hasattr(self, 'item_cls'):
return self.item_cls(key, self, self.request)
return self.children[key](key, self, self.request)
try:
return self.children[key](key, self, self.request)
except TypeError:
raise KeyError(key)

0 comments on commit 02d15e9

Please sign in to comment.