Esempio n. 1
0
 def test_single_route_one_view_registered(self):
     from zope.interface import Interface
     from pyramid.registry import Registry
     from pyramid.interfaces import IRouteRequest
     from pyramid.interfaces import IViewClassifier
     from pyramid.interfaces import IView
     registry = Registry()
     def view():pass
     class IMyRoute(Interface):
         pass
     registry.registerAdapter(view,
                              (IViewClassifier, IMyRoute, Interface),
                              IView, '')
     registry.registerUtility(IMyRoute, IRouteRequest, name='a')
     command = self._makeOne()
     route = dummy.DummyRoute('a', '/a')
     mapper = dummy.DummyMapper(route)
     command._get_mapper = lambda *arg: mapper
     L = []
     command.out = L.append
     command.bootstrap = (dummy.DummyBootstrap(registry=registry),)
     result = command.run()
     self.assertEqual(result, 0)
     self.assertEqual(len(L), 3)
     compare_to = L[-1].split()[:3]
     self.assertEqual(
         compare_to,
         ['a', '/a', 'pyramid.tests.test_scripts.test_proutes.view']
     )
Esempio n. 2
0
 def test__find_view_route_no_multiview(self):
     from zope.interface import Interface
     from zope.interface import implementer
     from pyramid.interfaces import IRouteRequest
     from pyramid.interfaces import IViewClassifier
     from pyramid.interfaces import IView
     from pyramid.registry import Registry
     registry = Registry()
     def view():pass
     class IMyRoot(Interface):
         pass
     class IMyRoute(Interface):
         pass
     registry.registerAdapter(view,
                              (IViewClassifier, IMyRoute, IMyRoot),
                              IView, '')
     registry.registerUtility(IMyRoute, IRouteRequest, name='a')
     @implementer(IMyRoot)
     class Factory(object):
         def __init__(self, request):
             pass
     routes = [dummy.DummyRoute('a', '/a', factory=Factory, matchdict={}),
               dummy.DummyRoute('b', '/b', factory=Factory)]
     self._register_mapper(registry, routes)
     command = self._makeOne(registry=registry)
     result = command._find_view('/a', registry)
     self.assertEqual(result, view)
Esempio n. 3
0
    def test_single_route_one_view_registered(self):
        from zope.interface import Interface
        from pyramid.registry import Registry
        from pyramid.interfaces import IRouteRequest
        from pyramid.interfaces import IViewClassifier
        from pyramid.interfaces import IView

        registry = Registry()

        def view():
            pass

        class IMyRoute(Interface):
            pass

        registry.registerAdapter(view, (IViewClassifier, IMyRoute, Interface), IView, "")
        registry.registerUtility(IMyRoute, IRouteRequest, name="a")
        command = self._makeOne()
        route = DummyRoute("a", "/a")
        mapper = DummyMapper(route)
        command._get_mapper = lambda *arg: mapper
        L = []
        command.out = L.append
        app = DummyApp()
        app.registry = registry
        loadapp = DummyLoadApp(app)
        command.loadapp = (loadapp,)
        command.args = ("/foo/bar/myapp.ini", "myapp")
        result = command.command()
        self.assertEqual(result, None)
        self.assertEqual(len(L), 3)
        self.assertEqual(L[-1].split()[:4], ["a", "/a", "<function", "view"])
Esempio n. 4
0
    def test__find_view_no_match_multiview_registered(self):
        from zope.interface import implementer
        from zope.interface import providedBy
        from pyramid.interfaces import IRequest
        from pyramid.interfaces import IViewClassifier
        from pyramid.interfaces import IMultiView
        from pyramid.traversal import DefaultRootFactory
        from pyramid.registry import Registry
        registry = Registry()

        @implementer(IMultiView)
        class View1(object):
            pass

        request = dummy.DummyRequest({'PATH_INFO': '/a'})
        root = DefaultRootFactory(request)
        root_iface = providedBy(root)
        registry.registerAdapter(View1(),
                                 (IViewClassifier, IRequest, root_iface),
                                 IMultiView)
        self._register_mapper(registry, [])
        command = self._makeOne(registry=registry)
        request = self._makeRequest('/x', registry)
        result = command._find_view(request)
        self.assertEqual(result, None)
Esempio n. 5
0
    def test_single_route_one_view_registered(self):
        from zope.interface import Interface
        from pyramid.registry import Registry
        from pyramid.interfaces import IRouteRequest
        from pyramid.interfaces import IViewClassifier
        from pyramid.interfaces import IView
        registry = Registry()

        def view():
            pass

        class IMyRoute(Interface):
            pass

        registry.registerAdapter(view, (IViewClassifier, IMyRoute, Interface),
                                 IView, '')
        registry.registerUtility(IMyRoute, IRouteRequest, name='a')
        command = self._makeOne()
        route = dummy.DummyRoute('a', '/a')
        mapper = dummy.DummyMapper(route)
        command._get_mapper = lambda *arg: mapper
        L = []
        command.out = L.append
        command.bootstrap = (dummy.DummyBootstrap(registry=registry), )
        result = command.run()
        self.assertEqual(result, None)
        self.assertEqual(len(L), 3)
        self.assertEqual(L[-1].split()[:4], ['a', '/a', '<function', 'view'])
Esempio n. 6
0
    def test__find_view_traversal_multiview(self):
        from zope.interface import implementer
        from zope.interface import providedBy
        from pyramid.interfaces import IRequest
        from pyramid.interfaces import IViewClassifier
        from pyramid.interfaces import IMultiView
        from pyramid.traversal import DefaultRootFactory
        from pyramid.registry import Registry

        registry = Registry()

        @implementer(IMultiView)
        class View1(object):
            pass

        request = dummy.DummyRequest()
        request.path_info = '/a'
        root = DefaultRootFactory(request)
        root_iface = providedBy(root)
        view = View1()
        registry.registerAdapter(
            view, (IViewClassifier, IRequest, root_iface), IMultiView, name='a'
        )
        self._register_mapper(registry, [])
        command = self._makeOne(registry=registry)
        request = self._makeRequest('/a', registry)
        result = command._find_view(request)
        self.assertEqual(result, view)
Esempio n. 7
0
 def test_single_route_one_view_registered_with_factory(self):
     from zope.interface import Interface
     from pyramid.registry import Registry
     from pyramid.interfaces import IRouteRequest
     from pyramid.interfaces import IViewClassifier
     from pyramid.interfaces import IView
     registry = Registry()
     def view():pass
     class IMyRoot(Interface):
         pass
     class IMyRoute(Interface):
         pass
     registry.registerAdapter(view,
                              (IViewClassifier, IMyRoute, IMyRoot),
                              IView, '')
     registry.registerUtility(IMyRoute, IRouteRequest, name='a')
     command = self._makeOne()
     def factory(request): pass
     route = DummyRoute('a', '/a', factory=factory)
     mapper = DummyMapper(route)
     command._get_mapper = lambda *arg: mapper
     L = []
     command.out = L.append
     app = DummyApp()
     app.registry = registry
     loadapp = DummyLoadApp(app)
     command.loadapp = (loadapp,)
     command.args = ('/foo/bar/myapp.ini', 'myapp')
     result = command.command()
     self.assertEqual(result, None)
     self.assertEqual(len(L), 3)
     self.assertEqual(L[-1].split()[:3], ['a', '/a', '<unknown>'])
Esempio n. 8
0
 def test_single_route_one_view_registered_with_factory(self):
     from zope.interface import Interface
     from pyramid.registry import Registry
     from pyramid.interfaces import IRouteRequest
     from pyramid.interfaces import IViewClassifier
     from pyramid.interfaces import IView
     registry = Registry()
     def view():pass
     class IMyRoot(Interface):
         pass
     class IMyRoute(Interface):
         pass
     registry.registerAdapter(view,
                              (IViewClassifier, IMyRoute, IMyRoot),
                              IView, '')
     registry.registerUtility(IMyRoute, IRouteRequest, name='a')
     command = self._makeOne()
     def factory(request): pass
     route = dummy.DummyRoute('a', '/a', factory=factory)
     mapper = dummy.DummyMapper(route)
     command._get_mapper = lambda *arg: mapper
     L = []
     command.out = L.append
     command.bootstrap = (dummy.DummyBootstrap(registry=registry),)
     result = command.run()
     self.assertEqual(result, None)
     self.assertEqual(len(L), 3)
     self.assertEqual(L[-1].split()[:3], ['a', '/a', '<unknown>'])
Esempio n. 9
0
    def test__find_view_traversal_multiview(self):
        from zope.interface import implementer
        from zope.interface import providedBy
        from pyramid.interfaces import IRequest
        from pyramid.interfaces import IViewClassifier
        from pyramid.interfaces import IMultiView
        from pyramid.traversal import DefaultRootFactory
        from pyramid.registry import Registry

        registry = Registry()

        @implementer(IMultiView)
        class View1:
            pass

        request = dummy.DummyRequest()
        request.path_info = '/a'
        root = DefaultRootFactory(request)
        root_iface = providedBy(root)
        view = View1()
        registry.registerAdapter(view, (IViewClassifier, IRequest, root_iface),
                                 IMultiView,
                                 name='a')
        self._register_mapper(registry, [])
        command = self._makeOne(registry=registry)
        request = self._makeRequest('/a', registry)
        result = command._find_view(request)
        self.assertEqual(result, view)
Esempio n. 10
0
    def test__find_view_route_no_multiview(self):
        from zope.interface import Interface
        from zope.interface import implementer
        from pyramid.interfaces import IRouteRequest
        from pyramid.interfaces import IViewClassifier
        from pyramid.interfaces import IView
        from pyramid.registry import Registry
        registry = Registry()

        def view():
            pass

        class IMyRoot(Interface):
            pass

        class IMyRoute(Interface):
            pass

        registry.registerAdapter(view, (IViewClassifier, IMyRoute, IMyRoot),
                                 IView, '')
        registry.registerUtility(IMyRoute, IRouteRequest, name='a')

        @implementer(IMyRoot)
        class Factory(object):
            def __init__(self, request):
                pass

        routes = [
            dummy.DummyRoute('a', '/a', factory=Factory, matchdict={}),
            dummy.DummyRoute('b', '/b', factory=Factory)
        ]
        self._register_mapper(registry, routes)
        command = self._makeOne(registry=registry)
        result = command._find_view('/a', registry)
        self.assertEqual(result, view)
Esempio n. 11
0
    def test_single_route_one_view_registered(self):
        from zope.interface import Interface
        from pyramid.registry import Registry
        from pyramid.interfaces import IRouteRequest
        from pyramid.interfaces import IViewClassifier
        from pyramid.interfaces import IView
        registry = Registry()

        def view():
            pass

        class IMyRoute(Interface):
            pass

        registry.registerAdapter(view, (IViewClassifier, IMyRoute, Interface),
                                 IView, '')
        registry.registerUtility(IMyRoute, IRouteRequest, name='a')
        command = self._makeOne()
        route = DummyRoute('a', '/a')
        mapper = DummyMapper(route)
        command._get_mapper = lambda *arg: mapper
        L = []
        command.out = L.append
        app = DummyApp()
        app.registry = registry
        loadapp = DummyLoadApp(app)
        command.loadapp = (loadapp, )
        command.args = ('/foo/bar/myapp.ini', 'myapp')
        result = command.command()
        self.assertEqual(result, None)
        self.assertEqual(len(L), 3)
        self.assertEqual(L[-1].split()[:4], ['a', '/a', '<function', 'view'])
Esempio n. 12
0
def register_cache_strategy(strategy_adapter: IHTTPCacheStrategy,
                            iresource: IInterface, registry: Registry,
                            view_or_method: str):
    """Register a cache strategy for a specific context interface and view."""
    if view_or_method in DISABLED_VIEWS_OR_METHODS:
        raise ConfigurationError('Setting cache strategy for this view_or_meth'
                                 'od is disabled: {0}'.format(view_or_method))
    registry.registerAdapter(strategy_adapter, (iresource, IRequest),
                             IHTTPCacheStrategy, view_or_method)
Esempio n. 13
0
    def test__find_view_route_multiview(self):
        from zope.interface import Interface, implementer

        from pyramid.interfaces import (
            IMultiView,
            IRootFactory,
            IRouteRequest,
            IView,
            IViewClassifier,
        )
        from pyramid.registry import Registry

        registry = Registry()

        def view1():  # pragma: no cover
            pass

        def view2():  # pragma: no cover
            pass

        class IMyRoot(Interface):
            pass

        class IMyRoute1(Interface):
            pass

        class IMyRoute2(Interface):
            pass

        registry.registerAdapter(
            view1, (IViewClassifier, IMyRoute1, IMyRoot), IView, ''
        )
        registry.registerAdapter(
            view2, (IViewClassifier, IMyRoute2, IMyRoot), IView, ''
        )
        registry.registerUtility(IMyRoute1, IRouteRequest, name='a')
        registry.registerUtility(IMyRoute2, IRouteRequest, name='b')

        @implementer(IMyRoot)
        class Factory:
            def __init__(self, request):
                pass

        registry.registerUtility(Factory, IRootFactory)
        routes = [
            dummy.DummyRoute('a', '/a', matchdict={}),
            dummy.DummyRoute('b', '/a', matchdict={}),
        ]
        self._register_mapper(registry, routes)
        command = self._makeOne(registry=registry)
        request = self._makeRequest('/a', registry)
        result = command._find_view(request)
        self.assertTrue(IMultiView.providedBy(result))
        self.assertEqual(len(result.views), 2)
        self.assertTrue((None, view1, None) in result.views)
        self.assertTrue((None, view2, None) in result.views)
Esempio n. 14
0
    def test__find_view_route_multiview(self):
        from zope.interface import Interface
        from zope.interface import implementer
        from pyramid.interfaces import IRouteRequest
        from pyramid.interfaces import IViewClassifier
        from pyramid.interfaces import IView
        from pyramid.interfaces import IMultiView
        from pyramid.interfaces import IRootFactory
        from pyramid.registry import Registry

        registry = Registry()

        def view1():  # pragma: no cover
            pass

        def view2():  # pragma: no cover
            pass

        class IMyRoot(Interface):
            pass

        class IMyRoute1(Interface):
            pass

        class IMyRoute2(Interface):
            pass

        registry.registerAdapter(
            view1, (IViewClassifier, IMyRoute1, IMyRoot), IView, ''
        )
        registry.registerAdapter(
            view2, (IViewClassifier, IMyRoute2, IMyRoot), IView, ''
        )
        registry.registerUtility(IMyRoute1, IRouteRequest, name='a')
        registry.registerUtility(IMyRoute2, IRouteRequest, name='b')

        @implementer(IMyRoot)
        class Factory(object):
            def __init__(self, request):
                pass

        registry.registerUtility(Factory, IRootFactory)
        routes = [
            dummy.DummyRoute('a', '/a', matchdict={}),
            dummy.DummyRoute('b', '/a', matchdict={}),
        ]
        self._register_mapper(registry, routes)
        command = self._makeOne(registry=registry)
        request = self._makeRequest('/a', registry)
        result = command._find_view(request)
        self.assertTrue(IMultiView.providedBy(result))
        self.assertEqual(len(result.views), 2)
        self.assertTrue((None, view1, None) in result.views)
        self.assertTrue((None, view2, None) in result.views)
Esempio n. 15
0
def register_cache_strategy(strategy_adapter: IHTTPCacheStrategy,
                            iresource: IInterface,
                            registry: Registry,
                            view_or_method: str):
    """Register a cache strategy for a specific context interface and view."""
    if view_or_method in DISABLED_VIEWS_OR_METHODS:
        raise ConfigurationError('Setting cache strategy for this view_or_meth'
                                 'od is disabled: {0}'.format(view_or_method))
    registry.registerAdapter(strategy_adapter,
                             (iresource, IRequest),
                             IHTTPCacheStrategy,
                             view_or_method)
Esempio n. 16
0
    def test__find_view_route_multiview(self):
        from zope.interface import Interface
        from zope.interface import implements
        from pyramid.interfaces import IRouteRequest
        from pyramid.interfaces import IViewClassifier
        from pyramid.interfaces import IView
        from pyramid.interfaces import IMultiView
        from pyramid.interfaces import IRootFactory
        from pyramid.registry import Registry
        registry = Registry()

        def view1():
            pass

        def view2():
            pass

        class IMyRoot(Interface):
            pass

        class IMyRoute1(Interface):
            pass

        class IMyRoute2(Interface):
            pass

        registry.registerAdapter(view1, (IViewClassifier, IMyRoute1, IMyRoot),
                                 IView, '')
        registry.registerAdapter(view2, (IViewClassifier, IMyRoute2, IMyRoot),
                                 IView, '')
        registry.registerUtility(IMyRoute1, IRouteRequest, name='a')
        registry.registerUtility(IMyRoute2, IRouteRequest, name='b')

        class Factory(object):
            implements(IMyRoot)

            def __init__(self, request):
                pass

        registry.registerUtility(Factory, IRootFactory)
        routes = [
            DummyRoute('a', '/a', matchdict={}),
            DummyRoute('b', '/a', matchdict={})
        ]
        self._register_mapper(registry, routes)
        command = self._makeOne()
        result = command._find_view('/a', registry)
        self.failUnless(IMultiView.providedBy(result))
        self.assertEqual(len(result.views), 2)
        self.failUnless((None, view1, None) in result.views)
        self.failUnless((None, view2, None) in result.views)
Esempio n. 17
0
 def test__find_view_traversal(self):
     from zope.interface import providedBy
     from pyramid.interfaces import IRequest
     from pyramid.interfaces import IViewClassifier
     from pyramid.interfaces import IView
     from pyramid.traversal import DefaultRootFactory
     from pyramid.registry import Registry
     registry = Registry()
     def view1(): pass
     request = dummy.DummyRequest({'PATH_INFO':'/a'})
     root = DefaultRootFactory(request)
     root_iface = providedBy(root)
     registry.registerAdapter(view1,
                              (IViewClassifier, IRequest, root_iface),
                              IView, name='a')
     self._register_mapper(registry, [])
     command = self._makeOne(registry=registry)
     result = command._find_view('/a', registry)
     self.assertEqual(result, view1)
Esempio n. 18
0
 def test__find_view_traversal(self):
     from zope.interface import providedBy
     from pyramid.interfaces import IRequest
     from pyramid.interfaces import IViewClassifier
     from pyramid.interfaces import IView
     from pyramid.traversal import DefaultRootFactory
     from pyramid.registry import Registry
     registry = Registry()
     def view1(): pass
     request = dummy.DummyRequest({'PATH_INFO':'/a'})
     root = DefaultRootFactory(request)
     root_iface = providedBy(root)
     registry.registerAdapter(view1,
                              (IViewClassifier, IRequest, root_iface),
                              IView, name='a')
     self._register_mapper(registry, [])
     command = self._makeOne(registry=registry)
     result = command._find_view('/a', registry)
     self.assertEqual(result, view1)
Esempio n. 19
0
 def test__find_view_no_match_multiview_registered(self):
     from zope.interface import implements
     from zope.interface import providedBy
     from pyramid.interfaces import IRequest
     from pyramid.interfaces import IViewClassifier
     from pyramid.interfaces import IMultiView
     from pyramid.traversal import DefaultRootFactory
     from pyramid.registry import Registry
     registry = Registry()
     class View1(object):
         implements(IMultiView)
     request = DummyRequest({'PATH_INFO':'/a'})
     root = DefaultRootFactory(request)
     root_iface = providedBy(root)
     registry.registerAdapter(View1(),
                              (IViewClassifier, IRequest, root_iface),
                              IMultiView)
     self._register_mapper(registry, [])
     command = self._makeOne()
     result = command._find_view('/x', registry)
     self.assertEqual(result, None)
Esempio n. 20
0
    def test__find_view_traversal_multiview(self):
        from zope.interface import implements
        from zope.interface import providedBy
        from pyramid.interfaces import IRequest
        from pyramid.interfaces import IViewClassifier
        from pyramid.interfaces import IMultiView
        from pyramid.traversal import DefaultRootFactory
        from pyramid.registry import Registry
        registry = Registry()

        class View1(object):
            implements(IMultiView)

        request = DummyRequest({'PATH_INFO': '/a'})
        root = DefaultRootFactory(request)
        root_iface = providedBy(root)
        view = View1()
        registry.registerAdapter(view, (IViewClassifier, IRequest, root_iface),
                                 IMultiView,
                                 name='a')
        self._register_mapper(registry, [])
        command = self._makeOne()
        result = command._find_view('/a', registry)
        self.assertEqual(result, view)
Esempio n. 21
0
def register_activity_renderer(registry: Registry, cls: type, name: str):
    registry.registerAdapter(factory=cls,
                             required=(IActivity, ),
                             provided=IActivityRenderer,
                             name=name)