Skip to content

Commit

Permalink
Updated json schema generator to use new API
Browse files Browse the repository at this point in the history
  • Loading branch information
leahfitch committed Nov 2, 2014
1 parent 889b876 commit 5c91034
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 12 deletions.
7 changes: 6 additions & 1 deletion cellardoor/api/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from functools import partial
from .interface import Interface
from .. import errors
from ..spec.jsonschema import to_jsonschema


class StandardOptionsMixin(object):
Expand Down Expand Up @@ -102,7 +103,7 @@ def __init__(self, interface, options=None):
self._interface = interface
self._api_options = options

for when, events in self._interface.hooks.listeners.keys():
for when, events in self._interface.hooks.listeners.items():
for event in events.keys():
method_name = '%s_%s' % (when, event)
setattr(self, method_name, getattr(self._interface.hooks, method_name))
Expand Down Expand Up @@ -169,6 +170,10 @@ def __init__(self, interface, options, filter):
self._options['filter'] = filter


def list(self):
return list(iter(self))


def __iter__(self):
return iter(self._interface.list(**self._merge_options(self._base_options)))

Expand Down
1 change: 0 additions & 1 deletion cellardoor/api/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,6 @@ def get(self, id, **kwargs):


def update(self, id, fields, _replace=False, _method=UPDATE, **kwargs):
print self.storage
options = self.options_factory.create(kwargs)

if not options.bypass_authorization:
Expand Down
8 changes: 7 additions & 1 deletion cellardoor/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,10 @@ def get_links(cls):
return dict(zip(link_names, map(cls.get_link, link_names)))


def is_multiple_link(cls, link):
return isinstance(link, ListOf) or isinstance(link, InverseLink) and link.multiple



class Entity(object):

Expand Down Expand Up @@ -247,4 +251,6 @@ def freeze(self):
if not self.is_frozen:
self.is_frozen = True
self.storage.setup(self)

for entity in self.entities.values():
for link_name in entity.links:
entity.get_link(link_name)
18 changes: 10 additions & 8 deletions cellardoor/spec/jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,12 @@ class APISerializer(object):
def create_schema(self, api, base_url, entity_serializer):
self.base_url = base_url
definitions = {}
for e in api.entities:
definitions[e.__name__] = entity_serializer.create_schema(e)
for name, entity in api.model.entities.items():
definitions[name] = entity_serializer.create_schema(entity)

resources = {}
for interface in api.interfaces:
entity_links = definitions[interface.entity.__class__.__name__]['links']
for name, interface in api.interfaces.items():
entity_links = definitions[interface.entity.__name__]['links']
if 'resource' not in entity_links:
entity_links['resource'] = {
'rel': 'resource',
Expand Down Expand Up @@ -277,11 +277,13 @@ def get_link_link(self, interface, link_name, link_interface):
link = None
entities = [interface.entity] + interface.entity.children
for entity in entities:
if hasattr(entity, link_name):
link = getattr(entity, link_name)
link = entity.fields.get(link_name)
if not link:
link = entity.links.get(link_name)
if link:
break
if link is None:
raise Exception, "%s has no link %s" % (interface.entity.__class__.__name__, link_name)
raise Exception, "%s has no link %s" % (interface.entity.__name__, link_name)

if interface.entity.is_multiple_link(link):
schema_link['targetSchema'] = {
Expand All @@ -295,7 +297,7 @@ def get_link_link(self, interface, link_name, link_interface):


def entity_schema_ref(self, interface):
return '#/definitions/%s' % interface.entity.__class__.__name__
return '#/definitions/%s' % interface.entity.__name__


def to_jsonschema(api, base_url, api_cls=APISerializer, entity_cls=EntitySerializer):
Expand Down
1 change: 0 additions & 1 deletion cellardoor/wsgi/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
from falcon_integration import add_to_falcon
1 change: 1 addition & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def test_merge(self):
def get_fake_interface():
interface = Mock()
interface.hooks.listeners.keys = Mock(return_value=[])
interface.hooks.listeners.items = Mock(return_value=[])
return interface


Expand Down

0 comments on commit 5c91034

Please sign in to comment.