Ejemplo n.º 1
0
    def test_customize_layers_with_custom_name(self):
        self.dir3 = tempfile.mkdtemp()

        self._mkfile1(self.file1)
        f = open(os.path.join(self.dir1, 'file2.pt'), 'wb')
        f.write(self.file2)
        f.close()

        tmpl1 = view.template(os.path.join(self.dir1, 'file.pt'),
                              layer = 'layer')
        tmpl2 = view.template(os.path.join(self.dir1, 'file2.pt'),
                              layer = 'layer')
        self.assertEqual(tmpl1(), '<div>Test template 1</div>')
        self.assertEqual(tmpl2(), '<div>Test template 2</div>')

        # layers
        view.layer('layer', self.dir2)
        view.layer('layer', self.dir3)

        # override file.pt
        f = open(os.path.join(self.dir2, 'file.pt'), 'wb')
        f.write(self.file2)
        f.close()

        # override file2.pt
        f = open(os.path.join(self.dir3, 'file2.pt'), 'wb')
        f.write(self.file1)
        f.close()

        # initialize layers
        customize._Manager.initialize()

        self.assertEqual(tmpl1(), '<div>Test template 2</div>')
        self.assertEqual(tmpl2(), '<div>Test template 1</div>')
Ejemplo n.º 2
0
 def test_tmpl_multiple_disable_packages(self):
     tmpl = view.template('memphis.view.tests:templates/test.pt',
                          nolayer = True)
     tmpl = view.template('memphis.view.tests:templates/test.pt',
                          nolayer = True)
     self.assertEqual(
         tapi.registry.keys(), [])
Ejemplo n.º 3
0
    def test_tmpl(self):
        tmpl = view.template('memphis.view.tests:templates/test.pt')
        self.assertTrue('PageTemplateFile' in repr(tmpl))

        tmpl = view.template('memphis.view.tests:templates/test.txt')
        self.assertTrue('PageTextTemplateFile' in repr(tmpl))

        self.assertEqual(tapi.registry['memphis.view.tests'].keys(),
                         ['test.pt', 'test.txt'])
Ejemplo n.º 4
0
    def test_customize_global_createfolder_and_reloadpackage(self):
        self._mkfile1(self.file1)

        tmpl = view.template(os.path.join(self.dir1, 'file.pt'))
        self.assertEqual(tmpl(), '<div>Test template 1</div>')

        self.dir2 = os.path.join(self.dir2, 'test')

        # enable custom folder
        self._init_memphis({'template.custom': self.dir2})

        self.assertTrue(os.path.isdir(self.dir2))
        self.assertEqual(tmpl(), '<div>Test template 1</div>')

        # create new custom resource
        os.mkdir(os.path.join(self.dir2, 'memphis.view.tests'))
        self._mkfile2(self.file2)
        time.sleep(0.1)

        # template reloaded
        self.assertEqual(tmpl(), '<div>Test template 2</div>')

        # remove template
        shutil.rmtree(os.path.join(self.dir2, 'memphis.view.tests'))

        time.sleep(0.1)
        self.assertEqual(tmpl(), '<div>Test template 1</div>')
Ejemplo n.º 5
0
    def test_commands_template_custom_layer_name(self):
        tmpl = view.template('memphis.view:/tests/templates/test.pt',
                             layer = 'test-unknown')

        sys.argv[:] = ['paste', 'templates', '-l', 'test-unknown']

        val = self._run()
Ejemplo n.º 6
0
    def test_renderer_tmpl_extra_params_none(self):
        def viewFactory(context, request):
            return None, None

        r = Renderer(template=view.template("templates/test.pt"))
        res = r(Context(), self.request, viewFactory)
        self.assertEqual(res.body, "<div>My pagelet</div>\n")
Ejemplo n.º 7
0
    def test_tmpl_multiple_extra_params(self):
        tmpl = view.template('memphis.view.tests:templates/test.pt',
                             title = 'Test template',
                             description = 'Test template description')

        self.assertEqual(tapi.registry['memphis.view.tests']['test.pt'][1:3],
                         ['Test template', 'Test template description'])
Ejemplo n.º 8
0
    def test_customize_global_reenable(self):
        self._mkfile1(self.file1)
        self._mkfile2(self.file2)

        f = open(os.path.join(self.dir2, 'test'), 'wb')
        f.write(' ')
        f.close()

        tmpl = view.template(os.path.join(self.dir1, 'file.pt'))
        self.assertEqual(tmpl(), '<div>Test template 1</div>')

        # load without watcher
        self._init_memphis({'template.custom': os.path.join(self.dir2, 'test')})

        self.assertTrue(customize.TEMPLATE._manager is None)
        self.assertTrue(customize.TEMPLATE._watcher is None)
        self.assertEqual(tmpl(), '<div>Test template 1</div>')

        # reinitialize
        customize.TEMPLATE['custom'] = self.dir2
        customize.TEMPLATE['watcher'] = ''
        customize.initialize(config.SettingsInitializing(object()))

        self.assertTrue(customize.TEMPLATE._manager is not None)
        self.assertTrue(customize.TEMPLATE._watcher is None)

        self.assertEqual(tmpl(), '<div>Test template 2</div>')
Ejemplo n.º 9
0
    def test_commands_template_customize_error2(self):
        tmpl = view.template('memphis.view:/tests/templates/test.pt')
        sys.argv[:] = ['paste', 'templates', '-c', 'unknownpkg:filename']

        val = self._run()

        self.assertTrue("Can't find package 'unknownpkg'" in val)
Ejemplo n.º 10
0
    def test_view_with_template(self):
        view.registerView(
            'index.html', view.View, Context,
            template=view.template('memphis.view.tests:templates/test.pt'))

        self._init_memphis()

        res = view.renderView('index.html', Context(), self.request)
        self.assertEqual(res.body, '<div>My pagelet</div>\n')
Ejemplo n.º 11
0
    def test_customize_global_enabled(self):
        self._mkfile1(self.file1)
        self._mkfile2(self.file2)

        tmpl = view.template(os.path.join(self.dir1, 'file.pt'))
        self.assertEqual(tmpl(), '<div>Test template 1</div>')

        # enable custom folder
        self._init_memphis({'template.custom': self.dir2})

        self.assertEqual(tmpl(), '<div>Test template 2</div>')
Ejemplo n.º 12
0
    def test_commands_template_all_several_pkg(self):
        tmpl = view.template('memphis.view:/tests/templates/test.pt')

        sys.argv[:] = ['paste', 'templates', '-a']

        val = self._run()

        self.assertTrue('* memphis.view' in val)
        self.assertTrue('* memphis.view.tests' in val)
        self.assertTrue(
            '- test.pt: ../memphis/view/tests/templates/test.pt' in val)
Ejemplo n.º 13
0
    def test_renderer_tmpl(self):
        def viewFactory(context, request):
            return None, {}

        r = Renderer(template=view.template("templates/test.pt"))
        res = r(Context(), self.request, viewFactory)

        self.assertTrue(isinstance(res, Response))
        self.assertEqual(res.body, "<div>My pagelet</div>\n")
        self.assertEqual(res.status, "200 OK")
        self.assertEqual(res.content_type, "text/html")
Ejemplo n.º 14
0
    def test_view_register_view_function_requestonly_template(self):
        def render(request):
            return {}

        view.registerView('index.html', render,
                          template = view.template('templates/test.pt'))
        self._init_memphis()

        context = Context()
        v = self._view('index.html', context, self.request)
        self.assertEqual(v.body, '<div>My pagelet</div>\n')
Ejemplo n.º 15
0
    def test_view_httpresp_from_update(self):
        class MyView(view.View):
            def update(self):
                raise HTTPForbidden()

        view.registerView('index.html', MyView, Context,
                          template = view.template('templates/test.pt'))
        self._init_memphis()

        self.assertRaises(
            HTTPForbidden,
            view.renderView, 'index.html', Context(), self.request)
Ejemplo n.º 16
0
    def test_renderer_tmpl_with_layout(self):
        class Layout(view.Layout):
            def render(self, content):
                return "<html>%s</html>" % content

        view.registerLayout("test", klass=Layout)
        self._init_memphis()

        def viewFactory(context, request):
            return None, {}

        # renderer layout is different
        r = Renderer(view.template("templates/test.pt"), "")
        res = r(Context(), self.request, viewFactory)
        self.assertEqual(res.body, "<div>My pagelet</div>\n")

        # layout is 'test'
        config.cleanUp()
        r = Renderer(view.template("templates/test.pt"), "test")
        res = r(Context(), self.request, viewFactory)
        self.assertEqual(res.body, "<html><div>My pagelet</div>\n</html>")
Ejemplo n.º 17
0
    def test_renderer_tmpl_change_response_attrs(self):
        def viewFactory(context, request):
            request.response.status = 202
            request.response.content_type = "text/plain"
            return None, None

        r = Renderer(view.template("templates/test.pt"))
        res = r(Context(), self.request, viewFactory)

        self.assertEqual(res.body, "<div>My pagelet</div>\n")
        self.assertEqual(res.status, "202 Accepted")
        self.assertEqual(res.content_type, "text/plain")
Ejemplo n.º 18
0
    def test_commands_template_list(self):
        tmpl = view.template('memphis.view:/tests/templates/test.pt',
                             title='Test template title',
                             description = 'Test template description')

        sys.argv[:] = ['paste', 'templates', '-l', 'memphis.view']

        val = self._run()

        self.assertTrue('* memphis.view' in val)
        self.assertTrue('* memphis.view.tests' not in val)
        self.assertTrue('Test template title' in val)
        self.assertTrue('Test template description' in val)
Ejemplo n.º 19
0
    def test_commands_template_customize_error5(self):
        tmpl = view.template('memphis.view:/tests/templates/test.pt')

        file = os.path.join(self.dir, 'file')
        f = open(file, 'wb')
        f.write(' ')
        f.close()

        sys.argv[:] = ['paste', 'templates', '-c', 'memphis.view:test.pt',
                       '-o', file]

        val = self._run()
        self.assertTrue("Custom path is not a directory:" in val)
Ejemplo n.º 20
0
    def test_commands_template_print(self):
        tmpl = view.template('memphis.view:/tests/templates/test.pt',
                             title='Test template title',
                             description = 'Test template description')

        sys.argv[:] = ['paste', 'templates', '-p', 'memphis.view:test.pt']

        val = self._run()
        self.assertTrue('Test template title' in val)
        self.assertTrue('Test template description' in val)
        self.assertTrue('Package:  memphis.view' in val)
        self.assertTrue('Template: test.pt' in val)
        self.assertTrue('<div>My pagelet</div>' in val)
Ejemplo n.º 21
0
    def test_commands_template_customize(self):
        tmpl = view.template('memphis.view:/tests/templates/test.pt')

        sys.argv[:] = ['paste', 'templates', '-c', 'memphis.view:test.pt',
                       '-o', self.dir]

        val = self._run()

        self.assertTrue(
            "Template 'memphis.view:test.pt' has been customized" in val)
        self.assertEqual(
            open(os.path.join(self.dir, 'memphis.view', 'test.pt'),'rb').read(),
            '<div>My pagelet</div>\n')
Ejemplo n.º 22
0
    def test_commands_template_skip_existing(self):
        tmpl = view.template('memphis.view:/tests/templates/test.pt')

        os.makedirs(os.path.join(self.dir, 'memphis.view'))
        open(os.path.join(self.dir, 'memphis.view', 'test.pt'),'wb').write(
            'my customized template')

        sys.argv[:] = ['paste', 'templates', '-c', 'memphis.view:test.pt',
                       '-o', self.dir]

        val = self._run()
        self.assertTrue("Custom file 'test.pt' already exists" in val)
        self.assertEqual(
            open(os.path.join(self.dir, 'memphis.view', 'test.pt'),'rb').read(),
            'my customized template')
Ejemplo n.º 23
0
    def test_commands_template_force_override(self):
        tmpl = view.template('memphis.view:/tests/templates/test.pt')

        os.makedirs(os.path.join(self.dir, 'memphis.view'))
        open(os.path.join(self.dir, 'memphis.view', 'test.pt'),'w').write(
            'my customized template')

        sys.argv[:] = ['paste', 'templates', '-c', 'memphis.view:test.pt',
                       '-o', self.dir, '--force']

        val = self._run()
        self.assertTrue("Overrids: Template 'memphis.view:test.pt' has been customized." in val)
        self.assertEqual(
            open(os.path.join(self.dir, 'memphis.view', 'test.pt'),'rb').read(),
            '<div>My pagelet</div>\n')
Ejemplo n.º 24
0
    def test_customize_global_custom_layer_name(self):
        self._mkfile1(self.file1)

        tmpl = view.template(os.path.join(self.dir1, 'file.pt'),
                             layer='layer')
        self.assertEqual(tmpl(), '<div>Test template 1</div>')

        os.makedirs(os.path.join(self.dir2, 'layer'))
        f = open(os.path.join(self.dir2, 'layer', 'file.pt'), 'wb')
        f.write(self.file2)
        f.close()

        # enable custom folder
        self._init_memphis({'template.custom': self.dir2})

        self.assertEqual(tmpl(), '<div>Test template 2</div>')
Ejemplo n.º 25
0
    def test_customize_global_disable(self):
        self._mkfile1(self.file1)
        self._mkfile2(self.file2)

        tmpl = view.template(os.path.join(self.dir1, 'file.pt'))
        self.assertEqual(tmpl(), '<div>Test template 1</div>')

        # enable custom folder
        self._init_memphis({'template.custom': self.dir2})
        self.assertEqual(tmpl(), '<div>Test template 2</div>')

        customize.TEMPLATE['custom'] = ''
        customize.initialize(config.SettingsInitializing(None))

        self.assertTrue(customize.TEMPLATE._manager is None)
        self.assertTrue(customize.TEMPLATE._watcher is None)
        self.assertEqual(tmpl(), '<div>Test template 1</div>')
Ejemplo n.º 26
0
    def test_layout_simple_view_with_template(self):
        class View(view.View):
            def __call__(self):
                return 'View: test'

        fn = os.path.join(self.dir, 'test.pt')
        tmpl = open(fn, 'wb')
        tmpl.write('<html>${content}</html>')
        tmpl.close()

        view.registerLayout('test', template = view.template(fn))
        self._init_memphis()

        renderer = SimpleRenderer(layout='test')

        context = Context()
        res = renderer(context, self.request, viewMapper(View))

        self.assertTrue('<html>View: test</html>' in res.body)
Ejemplo n.º 27
0
class LogoutForm(object):

    def update(self):
        request = self.request
        uid = security.authenticated_userid(request)

        if uid is not None:
            headers = security.forget(request)
            raise HTTPFound(
                headers = headers,
                location = '%s/logout.html'%request.application_url)


config.action(
    view.registerView,
    'login.html', view.INavigationRoot,
    klass = LoginForm,
    template=view.template("memphis.users.browser:login.pt"))

config.action(
    view.registerView,
    'login-success.html', view.INavigationRoot,
    klass = LoginSuccess,
    template=view.template("memphis.users:browser/login-success.pt"))

config.action(
    view.registerView,
    'logout.html', view.INavigationRoot,
    klass = LogoutForm,
    template=view.template("logout.pt"))
Ejemplo n.º 28
0
 def setUp(self):
     self.tmpl = view.template('memphis.view.tests:/templates/test.pt')
     self.out = StringIO()
     self._stdout = sys.stdout
     sys.stdout = self.out
     self.dir = tempfile.mkdtemp()
Ejemplo n.º 29
0
    def test_commands_template_customize_error4(self):
        tmpl = view.template('memphis.view:/tests/templates/test.pt')
        sys.argv[:] = ['paste', 'templates', '-c', 'memphis.view:test.pt']

        val = self._run()
        self.assertTrue("Output directory is required, use -o CUSTOMDIR" in val)
Ejemplo n.º 30
0
    def test_commands_template_print_error3(self):
        tmpl = view.template('memphis.view:/tests/templates/test.pt')
        sys.argv[:] = ['paste', 'templates', '-p', 'memphis.view:filename']

        val = self._run()
        self.assertTrue("Can't find template 'filename'" in val)