def __getitem__(self, name): """ Any class attribute which is an instance providing :class:`pyramid.interfaces.ILocation` will be returned as is. Attributes which are constructors for implementing classes will be replaced with a constructed instance. Assignment to the sub-resources `__name__` and `__parent__` properties is handled automatically. """ if name in self: return super(InnerResource, self).__getitem__(name) factory_or_resource = getattr(self, name, None) if factory_or_resource: try: if ILocation.implementedBy(factory_or_resource): inst = factory_or_resource(self.request) inst.__name__ = name inst.__parent__ = self self.__dict__[name] = inst return inst except TypeError: pass try: if ILocation.providedBy(factory_or_resource): return factory_or_resource except TypeError: pass raise KeyError(name)
def __getitem__(self, name): """ Any class attribute which is an instance providing :class:`pyramid.interfaces.ILocation` will be returned as is. Attributes which are constructors for implementing classes will be replaced with a constructed instance by reifying the newly constructed resource in place of the attribute. Assignment to the sub-resources `__name__` and `__parent__` properties is handled automatically. """ factory_or_resource = getattr(self, name, None) if factory_or_resource: if ILocation.implementedBy(factory_or_resource): inst = factory_or_resource(self.request) inst.__name__ = name inst.__parent__ = self setattr(self, name, inst) return inst if ILocation.providedBy(factory_or_resource): return factory_or_resource raise KeyError(name)
def __getitem__(self, name): """ While individual resources may override this method to customize the construction of sub-resources returned from traversal, make the common case easy by treating any attribute whose value is a class implementing :class:`pyramid.interfaces.ILocation` as a factory function for a sub-resource whose path component (`__name__`) is the name of the attribute itself. Any attribute which is an instance providing :class:`pyramid.interfaces.ILocation` will be returned as is. Attributes which are used as factories will replace themselves on the instance by reifying the constructed resource as the new attribute. """ factory_or_resource = getattr(self, name, None) if factory_or_resource: if ILocation.implementedBy(factory_or_resource): inst = factory_or_resource(self.request) inst.__name__ = name inst.__parent__ = self setattr(self, name, inst) return inst if ILocation.providedBy(factory_or_resource): return factory_or_resource raise KeyError(name)
def test_it(self): from pyramid_traversalwrapper import LocationProxy from pyramid.interfaces import ILocation l = [1, 2, 3] self.assertEqual(ILocation.providedBy(l), False) p = LocationProxy(l, "Dad", "p") self.assertEqual(p, [1, 2, 3]) self.assertEqual(ILocation.providedBy(p), True) self.assertEqual(p.__parent__, 'Dad') self.assertEqual(p.__name__, 'p') import pickle self.assertRaises(TypeError, pickle.dumps, p) # Proxies should get their doc strings from the object they proxy: self.assertEqual(p.__doc__, l.__doc__)
def is_location(context): """Does `context` implement :class:`pyramid.interfaces.ILocation`? :param context: The context. :type context: kotti.interfaces.INode :rtype: bool :returns: True if Is the context object implements :class:`pyramid.interfaces.ILocation`. """ return ILocation.providedBy(context)
def locatable(self, context, key): """Make a context object locatable and return it.""" if not hasattr(context, '__name__'): context.__name__ = key context.__parent__ = self context.request = self.request if not ILocation.providedBy(context): self.alsoProvides(context, ILocation) return context
def _process_result(self, result, created=False, context=None): if result is None: return httpexceptions.HTTPNoContent() if created: self.request.response.status = 201 if ILocation.providedBy(result): self.request.response.headers[ 'Location'] = self.request.resource_url(result) _try_add_etag(self.request, result, context=context) return result
def serialize(self, node, appstruct): if not appstruct: return colander.null if not ILocation.providedBy(appstruct): raise colander.Invalid(node, colander._('"${val}" object has not provide ILocation', mapping={'val': appstruct})) bindings = node.bindings or {} request = bindings.get('request', None) if not request: raise RuntimeError('"request" has not found inside of schema node bindings') return request.resource_url(appstruct)
def root(self): """ The site root. :result: The root object of the site. :rtype: :class:`kotti.resources.Node` """ if ILocation.providedBy(self.context): return self.lineage[-1] else: return get_root()
def url(self, context=None, *elements, **kwargs): """ URL construction helper. Just a convenience wrapper for :func:`pyramid.request.resource_url` with the same signature. If ``context`` is ``None`` the current context is passed to ``resource_url``. """ if context is None: context = self.context if not ILocation.providedBy(context): return self.request.url return self.request.resource_url(context, *elements, **kwargs)
def locatable(self, context, key, provides=None): """Make a context object locatable and pass on the request.""" # Compose. if provides is None: provides = alsoProvides if not hasattr(context, '__name__'): context.__name__ = key context._located_parent = self context.request = self.request if not ILocation.providedBy(context): provides(context, ILocation) return context
def locatable(self, context, key, provides=None): """Make a context object locatable and return it.""" # Compose. if provides is None: provides = alsoProvides if not hasattr(context, '__name__'): context.__name__ = key context._located_parent = self context.request = self.request if not ILocation.providedBy(context): provides(context, ILocation) return context
def __setitem__(self, key, value): if ILocation.providedBy(value): value.__name__ = key value.__parent__ = self return self._data.__setitem__(key, value)
def is_locateable(ob): if ILocation.providedBy(ob): return True if hasattr(ob, '__name__') and hasattr(ob, '__parent__'): return True
def test_create(self): from pyramid.interfaces import ILocation from zope.interface.verify import verifyObject inst = self.make_one() assert ILocation.providedBy(inst) assert verifyObject(ILocation, inst)