Пример #1
0
    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)
Пример #2
0
    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)
Пример #3
0
    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)
Пример #4
0
    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)
Пример #5
0
    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)
Пример #6
0
 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__)
Пример #7
0
 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__)
Пример #8
0
    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)
Пример #9
0
    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
Пример #10
0
    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)
Пример #11
0
 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
Пример #12
0
 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)
Пример #13
0
    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()
Пример #14
0
    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)
Пример #15
0
    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)
Пример #16
0
 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
Пример #17
0
    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
Пример #18
0
 def __setitem__(self, key, value):
     if ILocation.providedBy(value):
         value.__name__ = key
         value.__parent__ = self
     return self._data.__setitem__(key, value)
Пример #19
0
def is_locateable(ob):
    if ILocation.providedBy(ob):
        return True
    if hasattr(ob, '__name__') and hasattr(ob, '__parent__'):
        return True
Пример #20
0
 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)