Пример #1
0
    def testAssignmentSettings(self):
        mapping = assignment_mapping_from_key(self.portal,
            manager_name=u"test.testcolumn", category=CONTEXT_CATEGORY, key="/")
        assignment = mapping['test.portlet1']
        settings = IPortletAssignmentSettings(assignment)
        self.assertTrue(settings.get('visible', True))

        assignment = mapping['test.portlet2']
        settings = IPortletAssignmentSettings(assignment)
        self.assertFalse(settings.get('visible', True))
    def test_invoke_edit_form(self):
        mapping = PortletAssignmentMapping()
        request = self.folder.REQUEST

        mapping['foo'] = news.Assignment(count=5)
        editview = getMultiAdapter((mapping['foo'], request), name='edit')
        editview.update()
        editview.applyChanges(data={'count': 6,
                                    EXTENDER_PREFIX+'.css_class': 'my-class'})
        portlet_assignment = mapping.values()[0]
        settings = IPortletAssignmentSettings(portlet_assignment)

        self.assertEqual(portlet_assignment.count, 6)
        # We have not extended our storage adapter, so nothing gets saved:
        self.assertIsNone(settings.get('css_class', None))

        # Register our schemaextender
        gsm = getGlobalSiteManager()
        gsm.registerAdapter(PortletCssClassAdapter,
                            (IPortletAssignment,))
        gsm.registerAdapter(PortletCssClassFormExtender,
                            (Interface,
                             IDefaultBrowserLayer,
                             IPortletEditForm),
                            IFormExtender,
                            'portletcssclass.extender')
        mapping = PortletAssignmentMapping()
        request = self.folder.REQUEST

        mapping['foo'] = news.Assignment(count=5)
        editview = getMultiAdapter((mapping['foo'], request), name='edit')
        editview.update()
        editview.applyChanges(data={'count': 6,
                                    EXTENDER_PREFIX+'.css_class': 'my-class'})
        portlet_assignment = mapping.values()[0]
        settings = IPortletAssignmentSettings(portlet_assignment)

        gsm.unregisterAdapter(PortletCssClassFormExtender,
                              (Interface,
                               IDefaultBrowserLayer,
                               IPortletEditForm),
                              IFormExtender,
                              'portletcssclass.extender')
        gsm.unregisterAdapter(PortletCssClassAdapter,
                              (IPortletAssignment,))

        self.assertEqual(portlet_assignment.count, 6)
        # The prefix is used for the form field, not for the stored data:
        self.assertEqual(settings.get('css_class'), 'my-class')
    def test_invoke_add_form(self):
        portlet = getUtility(IPortletType, name='portlets.News')
        mapping = self.portal.restrictedTraverse(
            '++contextportlets++plone.leftcolumn')
        for m in mapping.keys():
            del mapping[m]
        addview = mapping.restrictedTraverse('+/' + portlet.addview)
        addview.update()
        addview.createAndAdd(data={'count': 5,
                                   EXTENDER_PREFIX+'.css_class': 'my-class'})
        portlet_assignment = mapping.values()[0]
        settings = IPortletAssignmentSettings(portlet_assignment)

        self.assertEqual(portlet_assignment.count, 5)
        # We have not extended our storage adapter, so nothing gets saved:
        self.assertIsNone(settings.get('css_class', None))

        # Register our schemaextender
        gsm = getGlobalSiteManager()
        gsm.registerAdapter(PortletCssClassAdapter,
                            (IPortletAssignment,))
        gsm.registerAdapter(PortletCssClassFormExtender,
                            (Interface,
                             IDefaultBrowserLayer,
                             IPortletAddForm),
                            IFormExtender,
                            'portletcssclass.extender')
        for m in mapping.keys():
            del mapping[m]
        addview = mapping.restrictedTraverse('+/' + portlet.addview)
        addview.update()
        addview.createAndAdd(data={'count': 5,
                                   EXTENDER_PREFIX+'.css_class': 'my-class'})
        portlet_assignment = mapping.values()[0]
        settings = IPortletAssignmentSettings(portlet_assignment)

        gsm.unregisterAdapter(PortletCssClassFormExtender,
                              (Interface,
                               IDefaultBrowserLayer,
                               IPortletAddForm),
                              IFormExtender,
                              'portletcssclass.extender')
        gsm.unregisterAdapter(PortletCssClassAdapter,
                              (IPortletAssignment,))

        self.assertEqual(portlet_assignment.count, 5)
        # The prefix is used for the form field, not for the stored data:
        self.assertEqual(settings.get('css_class'), 'my-class')
Пример #4
0
 def toggle_visibility(self, name):
     self.authorize()
     assignments = aq_inner(self.context)
     settings = IPortletAssignmentSettings(assignments[name])
     visible = settings.get('visible', True)
     settings['visible'] = not visible
     return self.finish_portlet_change()
    def getPortlets(self):
        if IPortletContext.providedBy(self.context):
            pcontext = self.context
        else:
            pcontext = queryAdapter(self.context, IPortletContext)

        if pcontext is None:
            return []

        assignments = []
        for category, key in pcontext.globalPortletCategories(True):
            mapping = self.storage.get(category, None)
            if mapping is not None:
                for assignment in mapping.get(key, {}).values():
                    try:
                        settings = IPortletAssignmentSettings(assignment)
                    except Exception:
                        logException(u'Error during retrieving assignment '
                            'settings. Context: "%s", Category: "%s", Key: '
                            '"%s", Assignment Class: "%s", Assignment ID: "%s"'
                            % ('/'.join(self.context.getPhysicalPath()),
                            category, key, str(assignment.__class__),
                            assignment.__name__), context=self.context)
                        continue

                    if not settings.get('visible', True):
                        continue
                    assignments.append({'category': category,
                                        'key': key,
                                        'name': assignment.__name__,
                                        'assignment': assignment
                                        })

        return assignments
Пример #6
0
 def toggle_visibility(self, name):
     assignments = aq_inner(self.context)
     settings = IPortletAssignmentSettings(assignments[name])
     visible = settings.get('visible', True)
     settings['visible'] = not visible
     self.request.response.redirect(self._nextUrl())
     return ''
Пример #7
0
def extract_mapping(manager_name, category, key, mapping):
    portlets_schemata = dict([
        (iface, name) for name, iface in getUtilitiesFor(IPortletTypeInterface)
    ])

    for name, assignment in mapping.items():
        type_ = None
        schema = None
        for schema in providedBy(assignment).flattened():
            type_ = portlets_schemata.get(schema, None)
            if type_ is not None:
                break

        if type_ is not None:
            child = PrettyDocument().createElement('assignment')
            child.setAttribute('manager', manager_name)
            child.setAttribute('category', category)
            child.setAttribute('key', key)
            child.setAttribute('type', type_)
            child.setAttribute('name', name)

            assignment = assignment.__of__(mapping)

            settings = IPortletAssignmentSettings(assignment)
            visible = settings.get('visible', True)
            child.setAttribute('visible', repr(visible))

            handler = IPortletAssignmentExportImportHandler(assignment)
            handler.export_assignment(schema, PrettyDocument(), child)

            yield child
Пример #8
0
 def toggle_visibility(self, name):
     self.authorize()
     assignments = aq_inner(self.context)
     settings = IPortletAssignmentSettings(assignments[name])
     visible = settings.get('visible', True)
     settings['visible'] = not visible
     return self.finish_portlet_change()
Пример #9
0
        def extractMapping(manager_name, category, key, mapping):
            for name, assignment in mapping.items():
                type_ = None
                for schema in providedBy(assignment).flattened():
                    type_ = portletSchemata.get(schema, None)
                    if type_ is not None:
                        break

                if type_ is not None:
                    child = self._doc.createElement('assignment')
                    child.setAttribute('manager', manager_name)
                    child.setAttribute('category', category)
                    child.setAttribute('key', key)
                    child.setAttribute('type', type_)
                    child.setAttribute('name', name)

                    assignment = assignment.__of__(mapping)

                    settings = IPortletAssignmentSettings(assignment)
                    visible = settings.get('visible', True)
                    child.setAttribute('visible', repr(visible))

                    handler = IPortletAssignmentExportImportHandler(assignment)
                    handler.export_assignment(schema, self._doc, child)
                    fragment.appendChild(child)
Пример #10
0
        def extractMapping(manager_name, category, key, mapping):
            for name, assignment in mapping.items():
                type_ = None
                for schema in providedBy(assignment).flattened():
                    type_ = portletSchemata.get(schema, None)
                    if type_ is not None:
                        break

                if type_ is not None:
                    child = self._doc.createElement('assignment')
                    child.setAttribute('manager', manager_name)
                    child.setAttribute('category', category)
                    child.setAttribute('key', key)
                    child.setAttribute('type', type_)
                    child.setAttribute('name', name)

                    assignment = assignment.__of__(mapping)

                    settings = IPortletAssignmentSettings(assignment)
                    visible = settings.get('visible', True)
                    child.setAttribute('visible', repr(visible))

                    handler = IPortletAssignmentExportImportHandler(assignment)
                    handler.export_assignment(schema, self._doc, child)
                    fragment.appendChild(child)
Пример #11
0
    def getPortlets(self):
        if IPortletContext.providedBy(self.context):
            pcontext = self.context
        else:
            pcontext = queryAdapter(self.context, IPortletContext)

        if pcontext is None:
            return []

        assignments = []
        for category, key in pcontext.globalPortletCategories(True):
            mapping = self.storage.get(category, None)
            if mapping is not None:
                for assignment in mapping.get(key, {}).values():
                    try:
                        settings = IPortletAssignmentSettings(assignment)
                    except TypeError:
                        # Portlet does not exist any longer
                        continue
                    else:
                        if not settings.get('visible', True):
                            continue

                    assignments.append({'category': category,
                                        'key': key,
                                        'name': assignment.__name__,
                                        'assignment': assignment
                                        })

        return assignments
 def test_should_remove_tag_if_context_can_not_be_found(self):
     portal = self.layer['portal']
     rightcol = getUtility(IPortletManager, name=u'plone.rightcolumn', context=portal)
     right = getMultiAdapter((portal, rightcol,), IPortletAssignmentMapping, context=portal)
     
     staticportlet = static.Assignment(header=u"Static Portlet", text=u"TEXT INPUT")
     right[u'staticportlet'] = staticportlet
     
     settings = IPortletAssignmentSettings(staticportlet)
     visible = settings.get('visible', True)
     settings['visible'] = False
     
     class FakeContent:
         def UID(self): return '1'
     page = portal[portal.invokeFactory('Document', 'testpage')]
     hash = portletHash(rightcol, staticportlet, FakeContent())
     field = page.getField('text')
     portletmarkup = portletMarkup(hash)
     field.set(page, portletmarkup, mimetype='text/html')
     page.setTitle('Blah')
     page.reindexObject()
     transaction.commit()
     
     self.browser.open('http://nohost/plone/testpage')
     
     self.failUnless("TEXT INPUT" not in self.browser.contents)
     self.failUnless('<img class="TINYMCEPORTLET mce-only' not in self.browser.contents)
     
Пример #13
0
    def test_invoke_edit_form(self):
        mapping = PortletAssignmentMapping()
        request = self.folder.REQUEST

        mapping['foo'] = news.Assignment(count=5)
        editview = getMultiAdapter((mapping['foo'], request), name='edit')
        editview.update()
        editview.applyChanges(data={
            'count': 6,
            EXTENDER_PREFIX + '.css_class': 'my-class'
        })
        portlet_assignment = mapping.values()[0]
        settings = IPortletAssignmentSettings(portlet_assignment)

        self.assertEqual(portlet_assignment.count, 6)
        # We have not extended our storage adapter, so nothing gets saved:
        self.assertIsNone(settings.get('css_class', None))

        # Register our schemaextender
        gsm = getGlobalSiteManager()
        gsm.registerAdapter(PortletCssClassAdapter, (IPortletAssignment, ))
        gsm.registerAdapter(
            PortletCssClassFormExtender,
            (Interface, IDefaultBrowserLayer, IPortletEditForm), IFormExtender,
            'portletcssclass.extender')
        mapping = PortletAssignmentMapping()
        request = self.folder.REQUEST

        mapping['foo'] = news.Assignment(count=5)
        editview = getMultiAdapter((mapping['foo'], request), name='edit')
        editview.update()
        editview.applyChanges(data={
            'count': 6,
            EXTENDER_PREFIX + '.css_class': 'my-class'
        })
        portlet_assignment = mapping.values()[0]
        settings = IPortletAssignmentSettings(portlet_assignment)

        gsm.unregisterAdapter(
            PortletCssClassFormExtender,
            (Interface, IDefaultBrowserLayer, IPortletEditForm), IFormExtender,
            'portletcssclass.extender')
        gsm.unregisterAdapter(PortletCssClassAdapter, (IPortletAssignment, ))

        self.assertEqual(portlet_assignment.count, 6)
        # The prefix is used for the form field, not for the stored data:
        self.assertEqual(settings.get('css_class'), 'my-class')
Пример #14
0
    def test_invoke_add_form(self):
        portlet = getUtility(IPortletType, name='portlets.News')
        mapping = self.portal.restrictedTraverse(
            '++contextportlets++plone.leftcolumn')
        for m in mapping.keys():
            del mapping[m]
        addview = mapping.restrictedTraverse('+/' + portlet.addview)
        addview.update()
        addview.createAndAdd(data={
            'count': 5,
            EXTENDER_PREFIX + '.css_class': 'my-class'
        })
        portlet_assignment = mapping.values()[0]
        settings = IPortletAssignmentSettings(portlet_assignment)

        self.assertEqual(portlet_assignment.count, 5)
        # We have not extended our storage adapter, so nothing gets saved:
        self.assertIsNone(settings.get('css_class', None))

        # Register our schemaextender
        gsm = getGlobalSiteManager()
        gsm.registerAdapter(PortletCssClassAdapter, (IPortletAssignment, ))
        gsm.registerAdapter(PortletCssClassFormExtender,
                            (Interface, IDefaultBrowserLayer, IPortletAddForm),
                            IFormExtender, 'portletcssclass.extender')
        for m in mapping.keys():
            del mapping[m]
        addview = mapping.restrictedTraverse('+/' + portlet.addview)
        addview.update()
        addview.createAndAdd(data={
            'count': 5,
            EXTENDER_PREFIX + '.css_class': 'my-class'
        })
        portlet_assignment = mapping.values()[0]
        settings = IPortletAssignmentSettings(portlet_assignment)

        gsm.unregisterAdapter(
            PortletCssClassFormExtender,
            (Interface, IDefaultBrowserLayer, IPortletAddForm), IFormExtender,
            'portletcssclass.extender')
        gsm.unregisterAdapter(PortletCssClassAdapter, (IPortletAssignment, ))

        self.assertEqual(portlet_assignment.count, 5)
        # The prefix is used for the form field, not for the stored data:
        self.assertEqual(settings.get('css_class'), 'my-class')
Пример #15
0
    def toggle_visibility(self, portlethash, viewname):
        info = unhashPortletInfo(portlethash)
        assignments = assignment_mapping_from_key(self.context,
                        info['manager'], info['category'], info['key'])

        IPortletPermissionChecker(assignments.__of__(aq_inner(self.context)))()

        assignment = assignments[info['name']]
        settings = IPortletAssignmentSettings(assignment)
        visible = settings.get('visible', True)
        settings['visible'] = not visible
        return self._render_column(info, viewname)
Пример #16
0
    def panels_for_assignments(self, assignments, manager, base_url):
        # todo: do we use a special cat for panels?
        category = self.__parent__.category
        key = self.__parent__.key

        data = []
        for idx in range(len(assignments)):
            name = assignments[idx].__name__
            if hasattr(assignments[idx], '__Broken_state__'):
                name = assignments[idx].__Broken_state__['__name__']

            # Todo: we can define an edit view for panels - for overlay etc. - IF needed...
            editview = queryMultiAdapter((assignments[idx], self.request),
                                         name='edit',
                                         default=None)

            if editview is None:
                editviewName = ''
            else:
                editviewName = '%s/%s/edit' % (base_url, name)

            panel_hash = hashPortletInfo(
                dict(
                    manager=manager.__name__,
                    category=category,
                    key=key,
                    name=name,
                ))

            try:
                settings = IPortletAssignmentSettings(assignments[idx])
                visible = settings.get('visible', True)
            except TypeError:
                visible = False

            data.append({
                'title': assignments[idx].title,
                'editview': editviewName,
                'hash': panel_hash,
                'name': name,
                'up_url': '%s/@@move-panel-up' % (base_url),
                'down_url': '%s/@@move-panel-down' % (base_url),
                'delete_url': '%s/@@delete-panel' % (base_url),
                'duplicate_url': '%s/@@duplicate-panel' % (base_url),
                'hide_url': '%s/@@toggle-visibility' % (base_url),
                'show_url': '%s/@@toggle-visibility' % (base_url),
                'visible': visible,
                'has_assignments': len(assignments[idx]),
            })
        if len(data) > 0:
            data[0]['up_url'] = data[-1]['down_url'] = None

        return data
Пример #17
0
def portlets_for_assignments(self, assignments, manager, base_url):
    category = self.__parent__.category
    key = self.__parent__.key

    data = []
    for idx in range(len(assignments)):
        name = assignments[idx].__name__
        if hasattr(assignments[idx], '__Broken_state__'):
            name = assignments[idx].__Broken_state__['__name__']

        editview = queryMultiAdapter((assignments[idx], self.request),
                                     name='edit',
                                     default=None)

        if editview is None:
            editviewName = ''
        else:
            editviewName = '%s/%s/edit' % (base_url, name)

        settingsviewName = '%s/%s/edit-portlet-metadata' % (base_url, name)

        portlet_hash = hashPortletInfo(
            dict(
                manager=manager.__name__,
                category=category,
                key=key,
                name=name,
            ))

        try:
            settings = IPortletAssignmentSettings(assignments[idx])
            visible = settings.get('visible', True)
        except TypeError:
            visible = False

        data.append({
            'title': assignments[idx].title,
            'editview': editviewName,
            'hash': portlet_hash,
            'name': name,
            'up_url': '%s/@@move-portlet-up' % base_url,
            'down_url': '%s/@@move-portlet-down' % base_url,
            'delete_url': '%s/@@delete-portlet' % base_url,
            'metadata_url': settingsviewName,
            'hide_url': '%s/@@toggle-visibility' % base_url,
            'show_url': '%s/@@toggle-visibility' % base_url,
            'visible': visible,
        })
    if len(data) > 0:
        data[0]['up_url'] = data[-1]['down_url'] = None

    return data
Пример #18
0
    def __call__(self):
        # The parent object is the Plone content object here; we get
        # it from the acquisition chain.
        parent = self.context.aq_inner.aq_parent.aq_parent
        panel = self.context

        portlets = []
        for assignment in panel:
            settings = IPortletAssignmentSettings(assignment)
            if not settings.get('visible', True):
                continue

            try:
                portlet = getMultiAdapter(
                    (parent, self.request, self, panel, assignment),
                    IPortletRenderer)
            except ComponentLookupError:
                logging.getLogger("panels").info(
                    "unable to look up renderer for '%s.%s'." %
                    (assignment.__class__.__module__,
                     assignment.__class__.__name__))
                continue

            info = {
                'manager': "panels",
                'category': CONTEXT_CATEGORY,
                'key': '/'.join(panel.getPhysicalPath()),
                'name': assignment.__name__,
                'renderer': portlet,
                'settings': settings,
                'assignment': assignment
            }

            hashPortletInfo(info)

            portlet.update()

            try:
                available = portlet.available
            except Exception as e:
                logging.getLogger('panels').info(
                    "available threw an exception for %s (%s %s)" %
                    (assignment.__name__, type(e), str(e)))
                continue

            if available:
                result = self.portlet(**info)
                portlets.append(result)

        return render(portlets, self.context.layout, self.request)
Пример #19
0
    def portlets(self):

        data = {}

        portlet_managers = getUtilitiesFor(IPortletManager, context=self.context)

        for (name, manager) in portlet_managers:

            mapping = getMultiAdapter((self.context, manager), IPortletAssignmentMapping)

            for (id, assignment) in mapping.items():

                renderer = getMultiAdapter(
                    (
                        self.context,
                        self.request,
                        self,
                        manager,
                        assignment
                    ),
                    IPortletRenderer
                )

                if renderer.__of__(self.context).available:

                    try:
                        settings = IPortletAssignmentSettings(assignment)
                        visible = settings.get('visible', True)
                    except TypeError:
                        visible = False

                    if visible:

                        if not data.has_key(name):
                            data[name] = []

                        config = dict(assignment.__dict__)

                        for k in config.keys():
                            if k.startswith('__') or k in ('assignment_context_path',):
                                del config[k]

                        data[name].append({
                            'id' : id,
                            'klass' : assignment.__class__.__module__,
                            'title' : assignment.title,
                            'config' : config,
                        })

        return data
def export_local_portlets(obj):
    """Serialize portlets for one content object
    Code mostly taken from https://github.com/plone/plone.restapi/pull/669
    """
    portlets_schemata = {
        iface: name
        for name, iface in getUtilitiesFor(IPortletTypeInterface)
    }
    items = {}
    for manager_name, manager in getUtilitiesFor(IPortletManager):
        mapping = queryMultiAdapter((obj, manager), IPortletAssignmentMapping)
        if mapping is None:
            continue
        mapping = mapping.__of__(obj)
        for name, assignment in mapping.items():
            portlet_type = None
            schema = None
            for schema in providedBy(assignment).flattened():
                portlet_type = portlets_schemata.get(schema, None)
                if portlet_type is not None:
                    break
            if portlet_type is None:
                continue
            assignment = assignment.__of__(mapping)
            settings = IPortletAssignmentSettings(assignment)
            if manager_name not in items:
                items[manager_name] = []
            values = {}
            for name in schema.names():
                value = getattr(assignment, name, None)
                if RelationValue is not None and isinstance(
                        value, RelationValue):
                    value = value.to_object.UID()
                elif isinstance(value, RichTextValue):
                    value = {
                        "data": json_compatible(value.raw),
                        "content-type": json_compatible(value.mimeType),
                        "encoding": json_compatible(value.encoding),
                    }
                value = json_compatible(value)
                values[name] = value
            items[manager_name].append({
                "type": portlet_type,
                "visible": settings.get("visible", True),
                "assignment": values,
            })
    return items
def portlets_for_assignments(self, assignments, manager, base_url):
    category = self.__parent__.category
    key = self.__parent__.key

    data = []
    for idx in range(len(assignments)):
        name = assignments[idx].__name__
        if hasattr(assignments[idx], '__Broken_state__'):
            name = assignments[idx].__Broken_state__['__name__']

        editview = queryMultiAdapter(
            (assignments[idx], self.request), name='edit', default=None)

        if editview is None:
            editviewName = ''
        else:
            editviewName = '%s/%s/edit' % (base_url, name)

        settingsviewName = '%s/%s/edit-portlet-metadata' % (base_url, name)

        portlet_hash = hashPortletInfo(
            dict(manager=manager.__name__, category=category,
                 key=key, name=name,))

        try:
            settings = IPortletAssignmentSettings(assignments[idx])
            visible = settings.get('visible', True)
        except TypeError:
            visible = False

        data.append({
            'title': assignments[idx].title,
            'editview': editviewName,
            'hash': portlet_hash,
            'name': name,
            'up_url': '%s/@@move-portlet-up' % (base_url),
            'down_url': '%s/@@move-portlet-down' % (base_url),
            'delete_url': '%s/@@delete-portlet' % (base_url),
            'metadata_url': settingsviewName,
            'hide_url': '%s/@@toggle-visibility' % (base_url),
            'show_url': '%s/@@toggle-visibility' % (base_url),
            'visible': visible,
        })
    if len(data) > 0:
        data[0]['up_url'] = data[-1]['down_url'] = None

    return data
Пример #22
0
    def portlets_for_assignments(self, assignments, manager, base_url):
        category = self.__parent__.category
        key = self.__parent__.key
        
        portal = self.context.portal_url.getPortalObject()
        view = portal.restrictedTraverse('@@plone')
        
        data = []
        for idx in range(len(assignments)):
            name = assignments[idx].__name__
            
            editview = queryMultiAdapter(
                (assignments[idx], self.request), name='edit', default=None)
            
            if editview is None:
                editviewName = ''
            else:
                editviewName = '%s/%s/edit' % (base_url, name)
            
            portlet_hash = hashPortletInfo(
                dict(manager=manager.__name__, category=category,
                     key=key, name=name,))
            
            settings = IPortletAssignmentSettings(assignments[idx])
            renderer = getMultiAdapter(
                        (self.context, self.request, view,
                            manager, assignments[idx]),
                                IPortletRenderer)

            data.append({
                'title'      : assignments[idx].title,
                'editview'   : editviewName,
                'hash'       : portlet_hash,
                'renderer'   : renderer.__of__(self.context),
                'up_url'     : '%s/@@move-portlet-up?name=%s' % (base_url, name),
                'down_url'   : '%s/@@move-portlet-down?name=%s' % (base_url, name),
                'delete_url' : '%s/@@delete-portlet?name=%s' % (base_url, name),
                'hide_url'   : '%s/@@toggle-visibility?name=%s' % (base_url, name),
                'show_url'   : '%s/@@toggle-visibility?name=%s' % (base_url, name),
                'visible'    : settings.get('visible', True),
                })
        if len(data) > 0:
            data[0]['up_url'] = data[-1]['down_url'] = None
            
        return data
Пример #23
0
    def __call__(self):
        # The parent object is the Plone content object here; we get
        # it from the acquisition chain.
        parent = self.context.aq_inner.aq_parent.aq_parent
        panel = self.context

        portlets = []
        for assignment in panel:
            settings = IPortletAssignmentSettings(assignment)
            if not settings.get('visible', True):
                continue

            try:
                portlet = getMultiAdapter(
                    (parent,
                     self.request,
                     self,
                     panel,
                     assignment), IPortletRenderer)
            except ComponentLookupError:
                logging.getLogger("panels").info(
                    "unable to look up renderer for '%s.%s'." % (
                        assignment.__class__.__module__,
                        assignment.__class__.__name__
                        )
                    )
                continue

            info = {
                'manager': panel.__name__,
                'category': CONTEXT_CATEGORY,
                'key': '/'.join(parent.getPhysicalPath()),
                'name': assignment.__name__,
                'renderer': portlet,
                }

            hashPortletInfo(info)

            portlet.update()
            if portlet.available:
                result = self.portlet(**info)
                portlets.append(result)

        return render(portlets, self.context.layout, self.request)
Пример #24
0
    def portlets_for_assignments(self, assignments, manager, base_url):
        category = self.__parent__.category
        key = self.__parent__.key

        data = []
        for idx in range(len(assignments)):
            name = assignments[idx].__name__
            if hasattr(assignments[idx], "__Broken_state__"):
                name = assignments[idx].__Broken_state__["__name__"]

            editview = queryMultiAdapter((assignments[idx], self.request), name="edit", default=None)

            if editview is None:
                editviewName = ""
            else:
                editviewName = "%s/%s/edit" % (base_url, name)

            portlet_hash = hashPortletInfo(dict(manager=manager.__name__, category=category, key=key, name=name))

            try:
                settings = IPortletAssignmentSettings(assignments[idx])
                visible = settings.get("visible", True)
            except TypeError:
                visible = False

            data.append(
                {
                    "title": assignments[idx].title,
                    "editview": editviewName,
                    "hash": portlet_hash,
                    "name": name,
                    "up_url": "%s/@@move-portlet-up" % (base_url),
                    "down_url": "%s/@@move-portlet-down" % (base_url),
                    "delete_url": "%s/@@delete-portlet" % (base_url),
                    "hide_url": "%s/@@toggle-visibility" % (base_url),
                    "show_url": "%s/@@toggle-visibility" % (base_url),
                    "visible": visible,
                }
            )
        if len(data) > 0:
            data[0]["up_url"] = data[-1]["down_url"] = None

        return data
Пример #25
0
    def getPortlets(self):
        """Work out which portlets to display, returning a list of dicts
        describing assignments to render.
        """
        manager = self.storage.__name__

        pcontext = IPortletContext(self.context, None)
        if pcontext is None:
            return []

        assignable = ILocalPortletAssignable(self.context, None)
        if assignable is None:
            return []

        annotations = IAnnotations(assignable, None)
        if annotations is None:
            return []

        local = annotations.get(CONTEXT_ASSIGNMENT_KEY, None)
        if local is None:
            return []

        localManager = local.get(manager, None)
        if localManager is None:
            return []

        assignments = []
        for assignment in localManager.values():
            try:
                settings = IPortletAssignmentSettings(assignment)
            except TypeError:
                # Portlet does not exist any longer
                continue
            if not settings.get('visible', True):
                continue
            assignments.append(assignment)

        return [{
            'category': CONTEXT_CATEGORY,
            'key': pcontext.uid,
            'name': a.__name__,
            'assignment': a
        } for a in assignments]
Пример #26
0
    def getPortlets(self):
        """Work out which portlets to display, returning a list of dicts
        describing assignments to render.
        """
        manager = self.storage.__name__

        pcontext = IPortletContext(self.context, None)
        if pcontext is None:
            return []

        assignable = ILocalPortletAssignable(self.context, None)
        if assignable is None:
            return []

        annotations = IAnnotations(assignable, None)
        if annotations is None:
            return []

        local = annotations.get(CONTEXT_ASSIGNMENT_KEY, None)
        if local is None:
            return []

        localManager = local.get(manager, None)
        if localManager is None:
            return []

        assignments = []
        for assignment in localManager.values():
            if PLONE4:
                try:
                    settings = IPortletAssignmentSettings(assignment)
                except TypeError:
                    # Portlet does not exist any longer
                    continue
                if not settings.get('visible', True):
                    continue
            assignments.append(assignment)

        return [{'category': CONTEXT_CATEGORY,
                 'key': pcontext.uid,
                 'name': a.__name__,
                 'assignment': a} for a in assignments]
Пример #27
0
 def get_portlets(self):
     portlets_schemata = {
         iface: name
         for name, iface in getUtilitiesFor(IPortletTypeInterface)
     }
     portlets = {}
     for manager_name, manager in getUtilitiesFor(IPortletManager):
         mapping = queryMultiAdapter(
             (self.context, manager), IPortletAssignmentMapping
         )
         if mapping is None:
             continue
         mapping = mapping.__of__(self.context)
         for name, assignment in mapping.items():
             type_ = None
             schema = None
             for schema in providedBy(assignment).flattened():
                 type_ = portlets_schemata.get(schema, None)
                 if type_ is not None:
                     break
             if type_ is None:
                 continue
             assignment = assignment.__of__(mapping)
             settings = IPortletAssignmentSettings(assignment)
             if manager_name not in portlets:
                 portlets[manager_name] = []
             title = getattr(assignment, "title", "") or getattr(
                 assignment, "header", ""
             )
             portlets[manager_name].append(
                 {
                     "type": type_,
                     "title": title,
                     "visible": settings.get("visible", True),
                     "assignment": {
                         name: getattr(assignment, name, None)
                         for name in schema.names()
                     },
                 }
             )
     self["portlets"] = portlets
Пример #28
0
    def portlets_for_assignments(self, assignments, manager, base_url):
        data = []
        for idx in range(len(assignments)):
            name = assignments[idx].__name__

            editview = queryMultiAdapter(
                (assignments[idx], self.request), name='edit', default=None)

            if editview is None:
                editviewName = ''
            else:
                editviewName = '%s/%s/edit' % (base_url, name)

            settings = IPortletAssignmentSettings(assignments[idx])

            data.append({
                'title': assignments[idx].title,
                'editview': editviewName,
                'visible': settings.get('visible', True),
                })
        return data
Пример #29
0
    def exportAssignments(self, obj):
        assignments = []
        for manager_name, manager in self.portlet_managers:
            mapping = queryMultiAdapter((obj, manager),
                                        IPortletAssignmentMapping)
            if mapping is None:
                continue

            mapping = mapping.__of__(obj)
            if len(mapping.items()) > 0:
                for name, assignment in mapping.items():
                    type_ = None
                    for schema in providedBy(assignment).flattened():
                        type_ = self.portlet_schemata.get(schema, None)
                        if type_ is not None:
                            break

                    if type_ is not None:
                        child = self.doc.createElement('assignment')
                        child.setAttribute('manager', manager_name)
                        child.setAttribute('category', CONTEXT_CATEGORY)
                        child.setAttribute('key',
                                           '/'.join(obj.getPhysicalPath()))
                        child.setAttribute('type', type_)
                        child.setAttribute('name', name)
                        settings = IPortletAssignmentSettings(assignment)
                        if not settings.get('visible', True):
                            child.setAttribute('visible', 'False')

                        assignment = assignment.__of__(mapping)
                        # use existing adapter for exporting a portlet assignment  # noqa
                        handler = IPortletAssignmentExportImportHandler(
                            assignment)  # noqa
                        handler.export_assignment(schema, self.doc, child)

                        assignments.append(child)

        return assignments
Пример #30
0
    def exportAssignments(self, obj):
        assignments = []
        for manager_name, manager in self.portlet_managers:
            mapping = queryMultiAdapter(
                (obj, manager),
                IPortletAssignmentMapping)
            if mapping is None:
                continue

            mapping = mapping.__of__(obj)
            if len(mapping.items()) > 0:
                for name, assignment in mapping.items():
                    type_ = None
                    for schema in providedBy(assignment).flattened():
                        type_ = self.portlet_schemata.get(schema, None)
                        if type_ is not None:
                            break

                    if type_ is not None:
                        child = self.doc.createElement('assignment')
                        child.setAttribute('manager', manager_name)
                        child.setAttribute('category', CONTEXT_CATEGORY)
                        child.setAttribute('key', '/'.join(
                            obj.getPhysicalPath()))
                        child.setAttribute('type', type_)
                        child.setAttribute('name', name)
                        settings = IPortletAssignmentSettings(assignment)
                        if not settings.get('visible', True):
                            child.setAttribute('visible', 'False')

                        assignment = assignment.__of__(mapping)
                        # use existing adapter for exporting a portlet assignment  # noqa
                        handler = IPortletAssignmentExportImportHandler(assignment)  # noqa
                        handler.export_assignment(schema, self.doc, child)

                        assignments.append(child)

        return assignments
Пример #31
0
    def getColumn(self, name, group='AnonymousUsers'):
        column = getUtility(IPortletManager, name=name)
        category = column[GROUP_CATEGORY]
        mapping = category.get(group, None)
        if mapping is None:
            return u''

        context = aq_inner(self.context)
        assignments = []
        for assignment in mapping.values():
            settings = IPortletAssignmentSettings(assignment)
            if not settings.get('visible', True):
                continue
            assignments.append({'category': GROUP_CATEGORY,
                                'key': group,
                                'name': assignment.__name__,
                                'assignment': assignment
                                })

        renderer = HomePageColumnsRenderer(context, self.request, self, column)
        renderer.assignments = assignments
        renderer.update()
        return renderer.render()
 def test_portlet_renders_in_tiny_mce(self):
     portal = self.layer['portal']
     rightcol = getUtility(IPortletManager, name=u'plone.rightcolumn', context=portal)
     right = getMultiAdapter((portal, rightcol,), IPortletAssignmentMapping, context=portal)
     
     staticportlet = static.Assignment(header=u"Static Portlet", text=u"TEXT INPUT")
     right[u'staticportlet'] = staticportlet
     
     settings = IPortletAssignmentSettings(staticportlet)
     visible = settings.get('visible', True)
     settings['visible'] = False
     
     page = portal[portal.invokeFactory('Document', 'testpage')]
     hash = portletHash(rightcol, staticportlet, portal)
     field = page.getField('text')
     portletmarkup = portletMarkup(hash)
     field.set(page, portletmarkup, mimetype='text/html')
     page.setTitle('Blah')
     page.reindexObject()
     transaction.commit()
     
     self.browser.open('http://nohost/plone/testpage')
     
     self.failUnless("TEXT INPUT" in self.browser.contents)
    def __call__(self, context):

        portlets = []
        if hasattr(context, 'portlets'):
            portlets = context.portlets
            for portlet in portlets:
                if not portlet in context.__parent__:
                    context.portlets.remove(portlet)

        possible = {}
        for k, v in context.__parent__.items():
            if v == context:
                continue
            settings = IPortletAssignmentSettings(v)
            if k in portlets:
                possible[k] = v
            if settings.get('visible', True):
                possible[k] = v
                if k in portlets:
                    context.portlets.remove(k)

        return SimpleVocabulary(
            [SimpleTerm(value=k, token=base64.b64encode(k), title=v.title)
             for k, v in possible.items()])
Пример #34
0
    def __call__(self):
        # The parent object is the Plone content object here; we get
        # it from the acquisition chain.

        # The panel can be rendered in different contexts, where the length of
        # the chain to the Plone content object is not obvious;
        # on portlet edit forms for instance, where we have a panel of
        # portlets below the edit form.
        # So to get the content object we go up in the aq chain, until we are
        # out of the chain of portlet assignments, panels etc.
        parent = self.context.aq_inner
        while True:
            parent = parent.aq_parent
            if not (IPanel.providedBy(parent)
                or IPortletAssignment.providedBy(parent)
                or IPortletAssignmentMapping.providedBy(parent)):
                break

        panel = self.context

        portlets = []
        for assignment in panel:
            settings = IPortletAssignmentSettings(assignment)
            if not settings.get('visible', True):
                continue

            try:
                portlet = getMultiAdapter(
                    (parent,
                     self.request,
                     self,
                     panel,
                     assignment), IPortletRenderer)
            except ComponentLookupError:
                logging.getLogger("panels").info(
                    "unable to look up renderer for '%s.%s'." % (
                        assignment.__class__.__module__,
                        assignment.__class__.__name__
                    )
                )
                continue

            info = {
                'manager': "panels",
                'category': CONTEXT_CATEGORY,
                'key': str('/'.join(parent.getPhysicalPath())),
                'name': assignment.__name__,
                'renderer': portlet,
                'settings': settings,
                'assignment': assignment
            }

            hashPortletInfo(info)

            portlet.__portlet_metadata__ = info.copy()
            del portlet.__portlet_metadata__['renderer']

            portlet.update()

            try:
                available = portlet.available
            except ConflictError:
                raise
            except Exception as e:
                logging.getLogger('panels').info(
                    "available threw an exception for %s (%s %s)" % (
                        assignment.__name__,
                        type(e),
                        str(e)
                    )
                )
                continue

            if available:
                result = self.portlet(**info)
                portlets.append(result)

        return render(
            portlets, self.context.layout, self.request
        )
Пример #35
0
    def getPortlets(self):
        """Work out which portlets to display, returning a list of dicts
        describing assignments to render.
        """

        if IPortletContext.providedBy(self.context):
            pcontext = self.context
        else:
            pcontext = queryAdapter(self.context, IPortletContext)

        if pcontext is None:
            return []

        # Holds a list of (category, key, assignment).
        categories = []

        # Keeps track of the blacklisting status for global categores
        # (user, group, content type). The status is either True (blocked)
        # or False (not blocked).
        blacklisted = {}

        # This is the name of the manager (column) we're rendering
        manager = self.storage.__name__

        # 1. Fetch blacklisting status for each global category

        # First, find out which categories we will need to determine
        # blacklist status for

        for category, key in pcontext.globalPortletCategories(False):
            blacklisted[category] = None

        # Then walk the content hierarchy to find out what blacklist status
        # was assigned. Note that the blacklist is tri-state; if it's None it
        # means no assertion has been made (i.e. the category has neither been
        # whitelisted or blacklisted by this object or any parent). The first
        # item to give either a blacklisted (True) or whitelisted (False)
        # value for a given item will set the appropriate value. Parents of
        # this item that also set a black- or white-list value will then be
        # ignored.

        # Whilst walking the hierarchy, we also collect parent portlets,
        # until we hit the first block.

        current = self.context
        currentpc = pcontext
        blacklistFetched = set()
        parentsBlocked = False

        while current is not None and currentpc is not None:
            if ILocalPortletAssignable.providedBy(current):
                assignable = current
            else:
                assignable = queryAdapter(current, ILocalPortletAssignable)

            if assignable is not None:
                if IAnnotations.providedBy(assignable):
                    annotations = assignable
                else:
                    annotations = queryAdapter(assignable, IAnnotations)

                if not parentsBlocked:
                    local = annotations.get(CONTEXT_ASSIGNMENT_KEY, None)
                    if local is not None:
                        localManager = local.get(manager, None)
                        if localManager is not None:
                            categories.extend([(CONTEXT_CATEGORY, currentpc.uid, a) for a in localManager.values()])

                lpam = getMultiAdapter((assignable, self.storage), ILocalPortletAssignmentManager)
                if lpam.getBlacklistStatus(CONTEXT_CATEGORY):
                    parentsBlocked = True
                for cat, cat_status in blacklisted.items():
                    local_status = lpam.getBlacklistStatus(cat)
                    if local_status is not None:
                        blacklistFetched.add(cat)
                        if cat_status is None:
                            blacklisted[cat] = local_status

            # We can abort if parents are blocked and we've fetched all
            # blacklist statuses

            if parentsBlocked and len(blacklistFetched) == len(blacklisted):
                break

            # Check the parent - if there is no parent, we will stop
            current = currentpc.getParent()
            if current is not None:
                if IPortletContext.providedBy(current):
                    currentpc = current
                else:
                    currentpc = queryAdapter(current, IPortletContext)

        # Get all global mappings for non-blacklisted categories

        for category, key in pcontext.globalPortletCategories(False):
            if not blacklisted[category]:
                mapping = self.storage.get(category, None)
                if mapping is not None:
                    for a in mapping.get(key, {}).values():
                        categories.append((category, key, a, ))

        assignments = []
        for category, key, assignment in categories:
            try:
                settings = IPortletAssignmentSettings(assignment)
                if not settings.get('visible', True):
                    continue
            except TypeError:
                # Portlet does not exist any longer
                continue
            assignments.append({'category': category,
                                'key': key,
                                'name': str(assignment.__name__),
                                'assignment': assignment
                                })
        return assignments
Пример #36
0
 def __getattr__(self, attr):
     settings = IPortletAssignmentSettings(self.context)
     return settings.get(attr, None)
 def __getattr__(self, attr):
     settings = IPortletAssignmentSettings(self.context)
     return settings.get(attr, None)
Пример #38
0
    def __init__(self, context, request):
        super(DisplayPanelView, self).__init__(context, request)

        # todo: move away from init

        # The parent object is the Plone content object here; we get
        # it from the acquisition chain.

        # The panel can be rendered in different contexts, where the length of
        # the chain to the Plone content object is not obvious;
        # on portlet edit forms for instance, where we have a panel of
        # portlets below the edit form.
        # So to get the content object we go up in the aq chain, until we are
        # out of the chain of portlet assignments, panels etc.
        parent = self.context.aq_inner
        while True:
            parent = parent.aq_parent
            if not (IPanel.providedBy(parent)
                    or IPortletAssignment.providedBy(parent)
                    or IPortletAssignmentMapping.providedBy(parent)):
                break

        panel = self.context

        portlets = []
        for assignment in panel:
            settings = IPortletAssignmentSettings(assignment)
            if not settings.get('visible', True):
                continue

            try:
                portlet = getMultiAdapter((parent, self.request, self, panel,
                                           assignment.__of__(panel)),
                                          IPortletRenderer)
            except ComponentLookupError:
                logging.getLogger("panels").info(
                    "unable to look up renderer for '%s.%s'." %
                    (assignment.__class__.__module__,
                     assignment.__class__.__name__))
                continue

            info = {
                'manager': "panels",
                'category': CONTEXT_CATEGORY,
                'key': str('/'.join(parent.getPhysicalPath())),
                'name': assignment.__name__,
                'renderer': portlet,
                'settings': settings,
                'assignment': assignment
            }

            # todo: check new impl. of portlet rendering
            hashPortletInfo(info)

            portlet.__portlet_metadata__ = info.copy()
            del portlet.__portlet_metadata__['renderer']

            portlet.update()

            try:
                available = portlet.available
            except ConflictError:
                raise
            except Exception as err:
                logging.getLogger('panels').info(
                    "available threw an exception for %s (%s %s)" %
                    (assignment.__name__, type(err), str(err)))
                continue

            info['available'] = available
            portlets.append(info)

        self.portlets = portlets