From 5c910343a7109a0a3a2678b2cd7ffbac86605485 Mon Sep 17 00:00:00 2001 From: Elisha Fitch-Cook Date: Sun, 2 Nov 2014 10:23:05 -0500 Subject: [PATCH] Updated json schema generator to use new API --- cellardoor/api/__init__.py | 7 ++++++- cellardoor/api/interface.py | 1 - cellardoor/model/model.py | 8 +++++++- cellardoor/spec/jsonschema.py | 18 ++++++++++-------- cellardoor/wsgi/__init__.py | 1 - tests/test_api.py | 1 + 6 files changed, 24 insertions(+), 12 deletions(-) diff --git a/cellardoor/api/__init__.py b/cellardoor/api/__init__.py index ff804ed..136afdb 100644 --- a/cellardoor/api/__init__.py +++ b/cellardoor/api/__init__.py @@ -1,6 +1,7 @@ from functools import partial from .interface import Interface from .. import errors +from ..spec.jsonschema import to_jsonschema class StandardOptionsMixin(object): @@ -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)) @@ -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))) diff --git a/cellardoor/api/interface.py b/cellardoor/api/interface.py index b1d35af..609d134 100644 --- a/cellardoor/api/interface.py +++ b/cellardoor/api/interface.py @@ -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: diff --git a/cellardoor/model/model.py b/cellardoor/model/model.py index f012bf9..a9938c4 100644 --- a/cellardoor/model/model.py +++ b/cellardoor/model/model.py @@ -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): @@ -247,4 +251,6 @@ def freeze(self): if not self.is_frozen: self.is_frozen = True self.storage.setup(self) - \ No newline at end of file + for entity in self.entities.values(): + for link_name in entity.links: + entity.get_link(link_name) \ No newline at end of file diff --git a/cellardoor/spec/jsonschema.py b/cellardoor/spec/jsonschema.py index 70a0166..ecf8adb 100644 --- a/cellardoor/spec/jsonschema.py +++ b/cellardoor/spec/jsonschema.py @@ -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', @@ -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'] = { @@ -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): diff --git a/cellardoor/wsgi/__init__.py b/cellardoor/wsgi/__init__.py index bd80cdd..e69de29 100644 --- a/cellardoor/wsgi/__init__.py +++ b/cellardoor/wsgi/__init__.py @@ -1 +0,0 @@ -from falcon_integration import add_to_falcon diff --git a/tests/test_api.py b/tests/test_api.py index 26feed3..d3e564a 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -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