Пример #1
0
    def test_contextmenu(self):
        with self.layer.authenticated('manager'):
            parent = BaseNode('root')
            model = parent['model'] = SharingNode()
            model.properties.action_up = True
            model.properties.action_view = True
            model.properties.action_list = True
            model.properties.action_edit = True
            model.properties.action_delete = True
            # XXX: model.properties.action_delete_children = True

            request = self.layer.new_request()
            ActionContext(model, request, 'content')
            rendered = render_tile(model, request, 'contextmenu')

            self.assertTrue(rendered.find('toolbaraction-up') > -1)
            self.assertTrue(rendered.find('toolbaraction-view') > -1)
            self.assertTrue(rendered.find('toolbaraction-list') > -1)
            self.assertTrue(rendered.find('toolbaraction-edit') > -1)
            self.assertTrue(rendered.find('toolbaraction-delete') > -1)
            self.assertTrue(rendered.find('toolbaraction-share') > -1)

            model = CopySupportNode()
            ActionContext(model, request, 'listing')
            rendered = render_tile(model, request, 'contextmenu')

            self.assertTrue(rendered.find('toolbaraction-cut') > -1)
            self.assertTrue(rendered.find('toolbaraction-copy') > -1)
            self.assertTrue(rendered.find('toolbaraction-paste') > -1)
Пример #2
0
    def test_ActionContext(self):
        model = BaseNode()
        request = self.layer.new_request()

        ac = ActionContext(model, request, 'tile')
        self.assertTrue(request.environ['action_context'] is ac)
        self.assertTrue(get_action_context(request) is ac)

        self.assertEqual(ac.scope, 'tile')

        request.params['bdajax.action'] = 'ajaxaction'
        self.assertEqual(ac.scope, 'ajaxaction')

        request.params['bdajax.action'] = 'layout'
        self.assertEqual(ac.scope, 'content')

        request.params['contenttile'] = 'contenttile'
        self.assertEqual(ac.scope, 'contenttile')

        class PropNode(BaseNode):
            properties = None

        node = PropNode()
        node.properties = Properties()
        node.properties.default_child = 'a'
        node['a'] = PropNode()
        node['a'].properties = Properties()
        node['a'].properties.default_content_tile = 'default'

        request = self.layer.new_request()
        ac = ActionContext(node, request, 'content')
        self.assertEqual(ac.scope, 'default')
Пример #3
0
    def test_ActionCopy(self):
        model = CopySupportNode('copysupport')
        request = self.layer.new_request()

        ActionContext(model, request, 'listing')
        self.assertTrue(model.supports_copy)

        action = ActionCopy()
        self.assertEqual(action(model, request), u'')

        with self.layer.authenticated('editor'):
            self.assertEqual(action(model, request), u'')

        with self.layer.authenticated('manager'):
            rendered = action(model, request)
            self.checkOutput(
                """
            ...<a
            id="toolbaraction-copy"
            href="#"
            ajax:target="http://example.com/copysupport"
            ><span class="ion-ios7-copy-outline"></span
            >&nbsp;Copy</a>...
            """, rendered)

            model.supports_copy = False
            self.assertEqual(action(model, request), u'')
Пример #4
0
    def test_ActionDelete(self):
        parent = BaseNode(name='root')
        model = parent['model'] = BaseNode()
        request = self.layer.new_request()

        ActionContext(model, request, 'content')

        action = ActionDelete()
        self.assertEqual(action(model, request), u'')

        model.properties.action_delete = True
        self.assertEqual(action(model, request), u'')

        with self.layer.authenticated('editor'):
            self.assertEqual(action(model, request), u'')

        with self.layer.authenticated('manager'):
            rendered = action(model, request)
            self.checkOutput(
                """
            ...<a
            id="toolbaraction-delete"
            href="#"
            ajax:bind="click"
            ajax:target="http://example.com/root/model"
            ajax:action="delete:NONE:NONE"
            ajax:confirm="Do you really want to delete this Item?"
            ><span class="ion-trash-a"></span
            >&nbsp;Delete</a>...
            """, rendered)

            model.properties.default_content_tile = 'othertile'
            self.assertEqual(action(model, request), u'')
Пример #5
0
    def test_ActionEdit(self):
        parent = BaseNode(name='root')
        model = parent['model'] = BaseNode()
        request = self.layer.new_request()

        ActionContext(model, request, 'listing')

        action = ActionEdit()
        self.assertEqual(action(model, request), u'')

        model.properties.action_edit = True
        self.assertEqual(action(model, request), u'')

        with self.layer.authenticated('viewer'):
            self.assertEqual(action(model, request), u'')

        with self.layer.authenticated('editor'):
            rendered = action(model, request)
            self.checkOutput(
                """
            ...<a
            id="toolbaraction-edit"
            href="http://example.com/root/model/edit"
            ajax:bind="click"
            ajax:target="http://example.com/root/model"
            ajax:action="edit:#content:inner"
            ajax:path="href"
            ><span class="glyphicon glyphicon-pencil"></span
            >&nbsp;Edit</a>...
            """, rendered)
Пример #6
0
    def test_ViewLink(self):
        parent = BaseNode(name='root')
        model = parent['model'] = BaseNode()
        request = self.layer.new_request()

        ActionContext(model, request, 'content')

        action = ViewLink()
        self.assertEqual(action(model, request), u'')

        model.properties.action_view = True
        self.assertEqual(action(model, request), u'')

        with self.layer.authenticated('viewer'):
            rendered = action(model, request)
            self.checkOutput(
                """
            ...<a
            id="toolbaraction-view"
            href="http://example.com/root/model"
            class="selected"
            ajax:bind="click"
            ajax:target="http://example.com/root/model"
            ajax:action="content:#content:inner"
            ajax:path="href"
            >&nbsp;model</a>...
            """, rendered)
Пример #7
0
def render_main_template(model, request, contenttile='content'):
    """Renders main template and return response object.

    As main content the tile with name contenttile is rendered.
    """
    ActionContext(model, request, contenttile)
    return render_template_to_response(cone.app.cfg.main_template,
                                       request=request,
                                       model=model)
Пример #8
0
    def test_ContentViewAction(self):
        model = BaseNode(name='model')
        request = self.layer.new_request()

        action = ContentViewAction(name='content_action')
        action.model = model
        action.request = request
        self.assertEqual(action.href, 'http://example.com/model/content_action')
        self.assertTrue(action.display)

        ActionContext(model, request, 'content_action')
        self.assertTrue(action.selected)

        del request.environ['action_context']
        self.assertFalse(action.selected)

        action = ContentViewAction(name='content_action', permission='view')
        action.model = model
        action.request = request
        self.assertFalse(action.display)
        with self.layer.authenticated('manager'):
            self.assertTrue(action.display)

        action = ContentViewAction(name='content_action', interface=BaseNode)
        action.model = model
        action.request = request
        self.assertTrue(action.display)

        class InvalidClassContext(object):
            pass

        action = ContentViewAction(
            name='content_action',
            interface=InvalidClassContext
        )
        action.model = model
        action.request = request
        self.assertFalse(action.display)

        class IContextInterface(Interface):
            pass

        action = ContentViewAction(
            name='content_action',
            interface=IContextInterface
        )
        action.model = model
        action.request = request
        self.assertFalse(action.display)

        @implementer(IContextInterface)
        class InterfaceModel(BaseNode):
            pass

        action.model = InterfaceModel()
        self.assertTrue(action.display)
Пример #9
0
    def test_ActionPaste(self):
        model = CopySupportNode('copysupport')
        request = self.layer.new_request()

        ActionContext(model, request, 'listing')
        self.assertTrue(model.supports_paste)

        action = ActionPaste()
        self.assertEqual(action(model, request), u'')

        with self.layer.authenticated('editor'):
            self.assertEqual(action(model, request), u'')

        with self.layer.authenticated('manager'):
            rendered = action(model, request)
            self.checkOutput(
                """
            ...<a
            id="toolbaraction-paste"
            href="#"
            class="disabled"
            ajax:target="http://example.com/copysupport"
            ><span class="ion-clipboard"></span
            >&nbsp;Paste</a>...
            """, rendered)

            request.cookies['cone.app.copysupport.cut'] = ['foo']
            rendered = action(model, request)
            self.checkOutput(
                """
            ...<a
            id="toolbaraction-paste"
            href="#"
            ajax:target="http://example.com/copysupport"
            ><span class="ion-clipboard"></span
            >&nbsp;Paste</a>...
            """, rendered)

            del request.cookies['cone.app.copysupport.cut']
            request.cookies['cone.app.copysupport.copy'] = ['foo']
            rendered = action(model, request)
            self.checkOutput(
                """
            ...<a
            id="toolbaraction-paste"
            href="#"
            ajax:target="http://example.com/copysupport"
            ><span class="ion-clipboard"></span
            >&nbsp;Paste</a>...
            """, rendered)

            del request.cookies['cone.app.copysupport.copy']

            model.supports_paste = False
            self.assertEqual(action(model, request), u'')
Пример #10
0
def render_form(model, request, tilename):
    """If form is invoked without hidden ajax field, the main template is
    rendered with tile ``tilename`` as content tile, otherwise
    ``render_ajax_form`` is called, which renders the tile wrapped by some
    javascript calls into a script tag. The ajax response will be rendered into
    the hidden iframe on client side, where ajax continuation is processed.

    XXX: move to cone.app.browser.form
    """
    if is_ajax(request):
        ActionContext(model, request, tilename)
        return render_ajax_form(model, request, tilename)
    return render_main_template(model, request, contenttile=tilename)
Пример #11
0
    def test_ActionAdd(self):
        info = NodeInfo()
        info.title = 'Addable'
        info.addables = ['addable']
        register_node_info('addable', info)

        action = ActionAdd()
        addmodel = BaseNode()
        request = self.layer.new_request()
        ActionContext(addmodel, request, 'listing')

        self.assertEqual(action(addmodel, request), u'')

        with self.layer.authenticated('viewer'):
            rule = request.has_permission('add', addmodel)
            self.assertTrue(isinstance(rule, ACLDenied))
            self.assertEqual(action(addmodel, request), u'')

        with self.layer.authenticated('editor'):
            rule = request.has_permission('add', addmodel)
            self.assertTrue(isinstance(rule, ACLAllowed))
            self.assertEqual(action(addmodel, request), u'')

            addmodel.node_info_name = 'addable'
            self.assertTrue(addmodel.nodeinfo is info)

            rendered = action(addmodel, request)
            self.checkOutput(
                """
            ...<li class="dropdown">
            <a href="#"
            class="dropdown-toggle"
            data-toggle="dropdown">
            <span>Add</span>
            <span class="caret"></span>
            </a>
            <ul class="dropdown-menu" role="addmenu">
            <li>
            <a href="http://example.com/add?factory=addable"
            ajax:bind="click"
            ajax:target="http://example.com/?factory=addable"
            ajax:action="add:#content:inner"
            ajax:path="href">
            <span class="glyphicon glyphicon-asterisk"></span>
            Addable
            </a>
            </li>
            </ul>
            </li>...
            """, rendered)
Пример #12
0
    def test_ActionView(self):
        parent = BaseNode(name='root')
        model = parent['model'] = BaseNode()
        request = self.layer.new_request()

        ActionContext(model, request, 'content')

        action = ActionView()
        self.assertEqual(action(model, request), u'')

        model.properties.action_view = True
        self.assertEqual(action(model, request), u'')

        with self.layer.authenticated('viewer'):
            rendered = action(model, request)
            self.checkOutput(
                """
            ...<a
            id="toolbaraction-view"
            href="http://example.com/root/model"
            class="selected"
            ajax:bind="click"
            ajax:target="http://example.com/root/model"
            ajax:action="content:#content:inner"
            ajax:path="href"
            ><span class="glyphicon glyphicon-eye-open"></span
            >&nbsp;View</a>...
            """, rendered)

            model.properties.default_content_tile = 'otherdefault'
            rendered = action(model, request)
            self.checkOutput(
                """
            ...<a
            id="toolbaraction-view"
            href="http://example.com/root/model"
            ajax:bind="click"
            ajax:target="http://example.com/root/model"
            ajax:action="view:#content:inner"
            ajax:path="href"
            ><span class="glyphicon glyphicon-eye-open"></span
            >&nbsp;View</a>...
            """, rendered)

            model.properties.default_content_tile = None
Пример #13
0
def ajax_tile(model, request):
    """bdajax ``ajaxaction`` implementation for cone.

    * Renders tile with name ``bdajax.action``.

    * Uses definitions from ``request.environ['cone.app.continuation']``
      for continuation definitions.
    """
    try:
        name = request.params['bdajax.action']
        ActionContext(model, request, name)
        rendered = render_tile(model, request, name)
        continuation = request.environ.get('cone.app.continuation')
        if continuation:
            continuation = AjaxContinue(continuation).definitions
        else:
            continuation = False
        return {
            'mode': request.params.get('bdajax.mode'),
            'selector': request.params.get('bdajax.selector'),
            'payload': rendered,
            'continuation': continuation,
        }
    except Forbidden:
        request.response.status = 403
        return {}
    except Exception:
        logging.exception('Error within ajax tile')
        tb = format_traceback()
        continuation = AjaxContinue([AjaxMessage(tb, 'error',
                                                 None)]).definitions
        return {
            'mode': 'NONE',
            'selector': 'NONE',
            'payload': '',
            'continuation': continuation,
        }