Example #1
0
    def test_rest_applications(self):
        from ptahcms.rest import cmsApplications

        ApplicationRoot = self._make_app()

        self.init_ptah()

        info = cmsApplications(self.request)
        self.assertEqual(info, [])

        factory = ptahcms.ApplicationFactory(ApplicationRoot,
                                             '/test',
                                             'root',
                                             'Root App',
                                             config=self.config)

        info = cmsApplications(self.request)
        self.assertEqual(len(info), 1)
        self.assertEqual(info[0]['__name__'], 'root')
        self.assertEqual(info[0]['__mount__'], 'test')
        self.assertEqual(
            info[0]['__link__'], 'http://example.com/content:%s/%s/' %
            (info[0]['__mount__'], info[0]['__uri__']))

        ptahcms.ApplicationFactory(ApplicationRoot,
                                   '/test2',
                                   'root2',
                                   'Root App',
                                   config=self.config)
        self.assertEqual(len(cmsApplications(self.request)), 2)

        self._allow = False
        self.assertEqual(len(cmsApplications(self.request)), 0)
Example #2
0
    def test_app_factory_mutiple_same_name(self):
        import ptahcms

        factory1 = ptahcms.ApplicationFactory(
            TestApplicationRoot, '/', 'root1', 'Root App1')
        factory2 = ptahcms.ApplicationFactory(
            TestApplicationRoot, '/', 'root2', 'Root App2')

        self.assertRaises(ConfigurationConflictError, self.init_ptah)
Example #3
0
    def test_app_factory_with_parent(self):
        import ptahcms

        factory1 = ptahcms.ApplicationFactory(
            TestApplicationRoot, '/app1', 'app1', 'Root App1')
        factory2 = ptahcms.ApplicationFactory(
            TestApplicationRoot, '/app2', 'app2', 'Root App2',
            parent_factory = factory1)

        root1 = factory1()
        root2 = factory2()
        self.assertTrue(root2.__parent__.__parent__.__uri__ == root1.__uri__)
Example #4
0
    def test_app_factory_mutiple(self):
        import ptahcms

        factory1 = ptahcms.ApplicationFactory(
            TestApplicationRoot, '/app1', 'app1', 'Root App1')
        factory2 = ptahcms.ApplicationFactory(
            TestApplicationRoot, '/app2', 'app2', 'Root App2')

        root1 = factory1()
        root2 = factory2()

        self.assertTrue(root1.__root_path__ == '/app1/')
        self.assertTrue(root2.__root_path__ == '/app2/')
        self.assertTrue(root1.__uri__ != root2.__uri__)
Example #5
0
    def test_rest_content_default(self):
        from ptahcms.rest import cmsContent
        ApplicationRoot = self._make_app()
        self.init_ptah()

        request = DummyRequest(subpath=('content', ), environ=self._environ)

        factory = ptahcms.ApplicationFactory(ApplicationRoot,
                                             '/',
                                             'root',
                                             'Root App',
                                             config=self.config)
        root = factory(request)
        root.__uri__ = 'type-app:test'
        transaction.commit()

        self._allow = False
        self.assertRaises(ptahcms.Forbidden, cmsContent, request)

        self._allow = True

        root = factory(request)

        info = cmsContent(request)
        self.assertEqual(info['__uri__'], root.__uri__)

        self.assertRaises(ptahcms.NotFound,
                          cmsContent,
                          request,
                          action='unknown')

        info = cmsContent(request, root.__uri__)
        self.assertEqual(info['__uri__'], root.__uri__)
Example #6
0
    def _create_content(self):
        import ptahcms

        factory = ptahcms.ApplicationFactory(ApplicationRoot, '/test', 'root',
                                             'Root App')
        self.factory = factory

        class MyContent(ptahcms.Content):
            __mapper_args__ = {'polymorphic_identity': 'mycontent'}
            __uri_factory__ = ptah.UriFactory('test')

        root = factory()

        folder = MyContent(__name__='folder',
                           __parent__=root,
                           __path__='%sfolder/' % root.__path__)
        self.folder_uri = folder.__uri__

        content = MyContent(__name__='content',
                            __parent__=folder,
                            __path__='%scontent/' % folder.__path__)
        self.content_uri = content.__uri__

        ptah.get_session().add(folder)
        ptah.get_session().add(content)
        transaction.commit()
Example #7
0
    def test_app_factory_policy(self):
        import ptahcms

        class CustomPolicy(ptahcms.ApplicationPolicy):
            pass

        factory = ptahcms.ApplicationFactory(
            TestApplicationRoot, '/app1', 'root', 'Root App', CustomPolicy)

        root = factory(self.request)
        self.assertTrue(isinstance(root.__parent__, CustomPolicy))
Example #8
0
    def test_app_factory_mutiple_same_applications(self):
        import ptahcms
        self.init_ptah()

        factory1 = ptahcms.ApplicationFactory(
            TestApplicationRoot, '/app1', 'root', 'Root App')
        factory2 = ptahcms.ApplicationFactory(
            TestApplicationRoot, '/app2', 'root', 'Root App')

        root1 = factory1()
        uri1 = root1.__uri__
        self.assertTrue(root1.__root_path__ == '/app1/')
        self.assertTrue(root1.__resource_url__(None, {}) == '/app1/')
        transaction.commit()

        root2 = factory2()

        self.assertTrue(root2.__uri__ == uri1)
        self.assertTrue(root2.__root_path__ == '/app2/')
        self.assertTrue(root2.__resource_url__(None, {}) == '/app2/')
Example #9
0
    def _make_app(self, request=None):
        from ptah.manage.manage import PtahManageRoute

        class Principal(object):
            id = 'test-user'
            uri = 'test:user'
            login = '******'

        principal = Principal()

        @ptah.resolver('test')
        def principalResolver(uri):
            return principal

        @ptah.principal_searcher('test')
        def principalSearcher(term):
            return (principal, )

        cms.ApplicationFactory(TestApp1, '/test1', 'app1', 'Root App 1')

        cms.ApplicationFactory(TestApp2, '/test2', 'app2', 'Root App 2')

        self.TestRole = ptah.Role('test', 'Test role')

        self.init_ptah()

        def trusted(*args):
            return True

        ptah.manage.set_access_manager(trusted, self.registry)

        if request is None:
            request = DummyRequest()

        ptah.auth_service.set_userid(ptah.SUPERUSER_URI)
        cfg = ptah.get_settings(ptah.CFG_ID_PTAH, self.registry)
        cfg['managers'] = ('*', )
        mr = PtahManageRoute(request)
        mod = mr['apps']
        return mod['app1']
Example #10
0
    def test_apps_view(self):
        from ptahcms.manage.apps import ApplicationsModule
        from ptahcms.manage.apps import ApplicationsModuleView

        cms.ApplicationFactory(TestApp1, '/test1', 'app1', 'Root App 1')

        cms.ApplicationFactory(TestApp2, '/test2', 'app2', 'Root App 2')

        self.init_ptah()

        request = DummyRequest()
        request.request_iface = IRequest

        mod = ApplicationsModule(None, request)

        res = render_view_to_response(mod, request, '', False)

        self.assertEqual(res.status, '200 OK')
        self.assertIn('/test1/', res.text)
        self.assertIn('/test2/', res.text)
        self.assertIn('App1', res.text)
        self.assertIn('App2', res.text)
Example #11
0
    def test_content_path(self):
        import ptahcms

        class MyContent(ptahcms.Content):

            __mapper_args__ = {'polymorphic_identity': 'mycontent'}
            __uri_factory__ = ptah.UriFactory('mycontent')

        ApplicationRoot = self._make_app()

        factory = ptahcms.ApplicationFactory(ApplicationRoot, '/app1', 'root',
                                             'Root App')

        root = factory(self.request)

        content = MyContent(__name__='test',
                            __parent__=root,
                            __path__='%stest/' % root.__path__)
        c_uri = content.__uri__
        ptah.get_session().add(content)

        self.assertTrue(content.__name__ == 'test')

        self.assertTrue(
            content.__resource_url__(self.request, {}) == '/app1/test/')
        transaction.commit()

        # same content inside same root but in different app factory

        factory2 = ptahcms.ApplicationFactory(ApplicationRoot, '/app2', 'root',
                                              'Root App')
        root = factory2(self.request)

        c = ptah.get_session().query(MyContent).filter(
            MyContent.__uri__ == c_uri).one()

        self.assertTrue(c.__resource_url__(self.request, {}) == '/app2/test/')
Example #12
0
    def test_apps_traverse(self):
        from ptahcms.manage.apps import ApplicationsModule

        factory1 = cms.ApplicationFactory(TestApp1, '/test1', 'app1',
                                          'Root App 1')

        factory2 = cms.ApplicationFactory(TestApp2, '/test2', 'app2',
                                          'Root App 2')

        self.init_ptah()

        request = DummyRequest()

        mod = ApplicationsModule(None, request)

        item = mod['app1']
        self.assertIsInstance(item, TestApp1)
        self.assertIs(request.app_factory, factory1)

        item = mod['app2']
        self.assertIsInstance(item, TestApp2)
        self.assertIs(request.app_factory, factory2)

        self.assertRaises(KeyError, mod.__getitem__, 'app3')
Example #13
0
    def test_app_factory(self):
        import ptahcms
        ApplicationRoot = self._make_app()

        self.init_ptah()

        factory = ptahcms.ApplicationFactory(
            ApplicationRoot, '/test', 'root', 'Root App')

        self.assertTrue(factory.id == 'test')
        self.assertTrue(factory.path == '/test/')
        self.assertTrue(factory.name == 'root')
        self.assertTrue(factory.title == 'Root App')
        self.assertTrue(factory.policy is ptahcms.ApplicationPolicy)

        root = factory(self.request)
        r_uri = root.__uri__

        self.assertTrue(isinstance(root, ApplicationRoot))
        self.assertTrue(root.title == 'Root App')
        self.assertTrue(root.__name__ == 'root')
        self.assertTrue(root.__root_path__ == '/test/')
        self.assertTrue(root.__resource_url__(None, {}) == '/test/')
        self.assertTrue(self.request.root is root)
        transaction.commit()

        root = factory(self.request)
        self.assertEqual(root.__uri__, r_uri)
        transaction.commit()

        root = ptah.resolve(r_uri)
        self.assertEqual(root.__uri__, r_uri)

        factory = ptahcms.ApplicationFactory(
            ApplicationRoot, '', 'root', 'Root App')
        self.assertTrue(factory.default_root)
Example #14
0
    def test_traverser_default_root(self):
        import ptahcms

        request = self.make_request(environ={
            'PATH_INFO': '/test/index.html',
            'bfg.routes.route': {}
        })

        factory = ptahcms.ApplicationFactory(ApplicationRoot, '', 'root',
                                             'Root App')
        root = factory(request)

        traverser = self.registry.getAdapter(root, ITraverser)

        info = traverser(request)
        self.assertTrue(info['context'] is root)
        self.assertEqual(info['view_name'], 'test')
Example #15
0
    def test_apps_app_view(self):
        from ptahcms.manage.apps import ApplicationsModule

        factory1 = cms.ApplicationFactory(TestApp1, '/test1', 'app1',
                                          'Root App 1')

        self.init_ptah()

        request = DummyRequest()
        request.request_iface = self.registry.getUtility(
            IRouteRequest, name=ptahcms.manage.MANAGE_APP_ROUTE)
        interface.directlyProvides(request, request.request_iface)
        mod = ApplicationsModule(None, request)

        item = mod['app1']

        res = render_view_to_response(item, request)
        self.assertIn('<h1>Application: Root App 1</h1>', res.text)
        self.assertIn('<td>Root App 1</td>', res.text)
Example #16
0
    def test_app_factory_custom_app2(self):
        import ptahcms

        class CustomApplication2(ptahcms.ApplicationRoot):

            __type__ = ptahcms.Type('customapp', 'Custom app')

        CustomApplication2.__type__.cls = CustomApplication2

        factory = ptahcms.ApplicationFactory(
            CustomApplication2, '/', 'root', 'Root App')

        root = factory()
        self.assertTrue(isinstance(root, CustomApplication2))

        u_root = root.__uri__
        transaction.commit()

        root = factory()
        self.assertEqual(root.__uri__, u_root)
Example #17
0
    def test_rest_applications_default(self):
        from ptahcms.rest import cmsApplications

        ApplicationRoot = self._make_app()

        self.init_ptah()

        info = cmsApplications(self.request)
        self.assertEqual(info, [])

        ptahcms.ApplicationFactory(ApplicationRoot,
                                   '/',
                                   'root',
                                   'Root App',
                                   config=self.config)

        info = cmsApplications(self.request)
        self.assertEqual(len(info), 1)
        self.assertEqual(info[0]['__name__'], 'root')
        self.assertEqual(info[0]['__mount__'], '')
        self.assertEqual(
            info[0]['__link__'],
            'http://example.com/content/%s/' % (info[0]['__uri__'], ))
Example #18
0
import ptah
import ptahcms
from pyramid.view import view_config


class SiteRoot(ptahcms.ApplicationRoot):
    """
    Application model which subclasses ptahcms.ApplicationRoot
    """

    __type__ = ptahcms.Type(
        'ptah_minicms-app',
        title='CMS Site Root',
        description='A root for the ptah_minicms Application')


APP_FACTORY = ptahcms.ApplicationFactory(
    SiteRoot, '/',
    name='root', title='Ptah mini cms')


@view_config(context=SiteRoot, wrapper=ptah.wrap_layout(),
             renderer='ptah_minicms:templates/homepage.pt')
class RootView(ptah.View):

    def __call__(self):
        return {'user': ptah.auth_service.get_current_principal()}