Beispiel #1
0
    def __call__(self):
        field_list = self.request.form.get('items.fl', '').strip()
        if field_list:
            field_list = field_list.split(',')
        else:
            field_list = ['@type', 'title', 'description', 'review_state']

        obj = IContentListingObject(self.context)
        summary = json_compatible({
            '@id': obj.getURL(),
        })

        for field in field_list:
            accessor = FIELD_ACCESSORS.get(field)
            if accessor is None:
                continue
            if isinstance(accessor, str):
                value = getattr(obj, accessor, None)
                if callable(value):
                    value = value()
            else:
                value = accessor(obj)
            summary[field] = json_compatible(value)

        if ('title' in summary
                and ITranslatedTitleSupport.providedBy(self.context)):
            # Update title to contain translated title in negotiated language
            attr = 'title_{}'.format(get_preferred_language_code())
            summary['title'] = getattr(self.context, attr)

        return summary
Beispiel #2
0
    def __call__(self):
        field_list = self.request.form.get('items.fl', '').strip()
        if field_list:
            field_list = field_list.split(',')
        else:
            field_list = ['@type', 'title', 'description', 'review_state']

        obj = IContentListingObject(self.context)
        summary = json_compatible({
            '@id': obj.getURL(),
        })

        for field in field_list:
            accessor = FIELD_ACCESSORS.get(field)
            if accessor is None:
                continue
            if isinstance(accessor, str):
                value = getattr(obj, accessor, None)
                if callable(value):
                    value = value()
            else:
                value = accessor(obj)
            summary[field] = json_compatible(value)

        if ('title' in summary and
                ITranslatedTitleSupport.providedBy(self.context)):
            # Update title to contain translated title in negotiated language
            attr = 'title_{}'.format(get_preferred_language_code())
            summary['title'] = getattr(self.context, attr)

        return summary
Beispiel #3
0
 def __call__(self):
     obj = IContentListingObject(self.context)
     summary = json_compatible({
         '@id': obj.getURL(),
         '@type': obj.PortalType(),
         'title': obj.Title(),
         'description': obj.Description()
     })
     return summary
def create_list_item(item, fields):
    obj = IContentListingObject(item)
    data = {'@id': obj.getURL()}
    for field in fields:
        if field not in FIELDS:
            continue
        accessor = FIELDS[field][1]
        if isinstance(accessor, str):
            value = getattr(obj, accessor, None)
            if callable(value):
                value = value()
        else:
            value = accessor(obj)
        data[field] = json_compatible(value)
    return data
Beispiel #5
0
def create_list_item(item, fields):
    obj = IContentListingObject(item)
    data = {'@id': obj.getURL()}
    for field in fields:
        if field not in FIELDS:
            continue
        accessor = FIELDS[field][1]
        if isinstance(accessor, str):
            value = getattr(obj, accessor, None)
            if callable(value):
                value = value()
        else:
            value = accessor(obj)
        data[field] = json_compatible(value)
    return data
Beispiel #6
0
def make_item(item, next_prev=True):
    """Make an item for REST API as expected by the frontend client.
    """

    ws = getattr(item, WORKSPACE_ATTRIBUTE,
                 None) or None  # at least not Missing.Value  # noqa
    if next_prev:
        ob = item.getObject()
        parent = aq_parent(ob)

        data_previous = None
        if (parent.portal_type in NODE_TYPES
                and getattr(aq_base(parent), WORKSPACE_ATTRIBUTE, ws) != ws):
            # If the parent's ws != current ws it's OK to not reach this branch because:
            # 1) not a Contribution or Case.
            # 2) or hasn't set it's ``workspace`` attribute and not a different ws for sure.
            _prev = uuidToCatalogBrain(IUUID(parent)) if parent else None
            data_previous = make_item(_prev,
                                      next_prev=False) if _prev else None

        data_next = []
        for child in ob.contentValues():
            if getattr(aq_base(child), WORKSPACE_ATTRIBUTE, ws) != ws:
                # If child hasn't set it's ws attribute its not a different WS for sure.
                _next = uuidToCatalogBrain(IUUID(child))
                data_next.append(make_item(_next, next_prev=False))

    item = IContentListingObject(item)

    ret = {
        "@id": item.getURL(),
        "@type": item.PortalType(),
        "UID": item.uuid(),
        "title": item.Title(),
        "review_state": item.review_state(),
        "workspace": ws,
        "is_workspace_root": item.workspace_root,
        "created": item.CreationDate(),
        "modified": item.ModificationDate()
    }
    if next_prev:
        ret["previous_workspace"] = data_previous
        ret["next_workspaces"] = data_next

    return ret
class DocumentLinkWidget(object):

    template = ViewPageTemplateFile('document_link.pt')

    def __init__(self, document):
        self.document = IContentListingObject(document)
        self.context = self.document
        self.request = getRequest()

    def get_url(self):
        return self.document.getURL()

    def portal_url(self):
        return api.portal.get().absolute_url()

    def get_css_class(self):
        classes = ['document_link']
        if self.show_icon:
            classes.append(self.document.ContentTypeClass())
        if self.context.is_removed:
            classes.append('removed_document')

        return ' '.join(classes)

    def get_title(self):
        if self.title is not None:
            return self.title
        return self.document.Title().decode('utf-8')

    def render(self, title=None, show_icon=True):
        self.title = title
        self.show_icon = show_icon
        return self.template(self, self.request)

    def is_view_allowed(self):
        return api.user.has_permission('View', obj=self.context.getObject())
Beispiel #8
0
class DocumentLinkWidget(object):

    template = ViewPageTemplateFile('document_link.pt')

    def __init__(self, document):
        self.document = IContentListingObject(document)
        self.context = self.document
        self.request = getRequest()

    def get_url(self):
        return self.document.getURL()

    def portal_url(self):
        return api.portal.get().absolute_url()

    def get_css_class(self):
        classes = ['document_link', self.document.ContentTypeClass()]
        return ' '.join(classes)

    def get_title(self):
        return self.document.Title().decode('utf-8')

    def render(self):
        return self.template(self, self.request)
Beispiel #9
0
def make_item_overview(item, next_prev=True):
    """Make an item for REST API as expected by the frontend client.
    This one is used for overviews, where we do not want the direct
    next/previous workspace but the next/previous down/up the tree.
    """

    if next_prev:
        ob = item.getObject()

        _prev = get_root_of_workspace(aq_parent(ob))
        _prev = uuidToCatalogBrain(IUUID(_prev)) if _prev else None
        data_previous = make_item_overview(
            _prev, next_prev=False) if _prev else None  # noqa

        _next = get_next_workspaces(ob, context_aware=True) or []
        data_next = [make_item_overview(it, next_prev=False) for it in _next]

    item = IContentListingObject(item)

    ret = {
        "@id": item.getURL(),
        "@type": item.PortalType(),
        "UID": item.uuid(),
        "title": item.Title(),
        "review_state": item.review_state(),
        "workspace": getattr(item, WORKSPACE_ATTRIBUTE, None)
        or None,  # at least not Missing.Value  # noqa
        "is_workspace_root": item.workspace_root,
        "created": item.CreationDate(),
        "modified": item.ModificationDate()
    }
    if next_prev:
        ret["previous_workspace"] = data_previous
        ret["next_workspaces"] = data_next

    return ret
class TestIndividualRealContentItems(unittest.TestCase):
    layer = CONTENTLISTING_FUNCTIONAL_TESTING

    def setUp(self):
        super(TestIndividualRealContentItems, self).setUp()
        self.portal = self.layer['portal']
        self.folder = self.portal['test-folder']
        self.folder.invokeFactory(
            'Document',
            'mypage',
            title='My Page',
            description='blah',
        )
        self.item = IContentListingObject(self.folder.mypage)
        self.realitem = self.folder.mypage

    def test_printing_item(self):
        self.assertEqual(
            repr(self.item),
            '<plone.app.contentlisting.realobject.RealContentListingObject '
            'instance at /plone/test-folder/mypage>',
        )
        self.assertEqual(
            str(self.item),
            '<plone.app.contentlisting.realobject.RealContentListingObject '
            'instance at /plone/test-folder/mypage>',
        )

    def test_special_getattr_with_underscore(self):
        # looking up attributes starting with _ should always raise
        # AttributeError
        self.assertRaises(AttributeError, self.item.__getattr__, 'foo')

    def test_special_getattr_from_object(self):
        # Asking for an attribute not in the contentlistingobject, should
        # defer lookup to the brain
        self.assertEqual(self.item.absolute_url(), '')
        self.assertEqual(
            repr(self.item.getDataOrigin()),
            '<Document at /plone/test-folder/mypage>',
        )

    def test_item_Title(self):
        self.assertEqual(self.item.Title(), 'My Page')
        self.assertEqual(self.item.Title(), self.realitem.Title())

    def test_item_Description(self):
        self.assertEqual(self.item.Description(), 'blah')
        self.assertEqual(self.item.Description(), self.realitem.Description())

    def test_item_Creator(self):
        self.assertEqual(self.item.Creator(), 'test_user_1_')

    def test_item_getURL(self):
        self.assertEqual(
            self.item.getURL(),
            'http://nohost/plone/test-folder/mypage',
        )
        self.assertEqual(self.item.getURL(), self.realitem.absolute_url())

    def test_item_getSize(self):
        self.assertEqual(self.item.getSize().upper(), '0 KB')

    def test_item_reviewState(self):
        wftool = getToolByName(self.realitem, 'portal_workflow')
        wf = wftool.getInfoFor(self.realitem, 'review_state')
        self.assertEqual(self.item.review_state(), wf)

    def test_item_Type(self):
        self.assertEqual(self.item.Type(), u'Page')
        self.assertEqual(self.item.Type().domain, u'plone')

    def test_item_ContentTypeClass(self):
        # checking the that we print nice strings for css class identifiers
        self.assertEqual(self.item.ContentTypeClass(), 'contenttype-document')

    def test_item_uuid(self):
        uuid = self.item.uuid()
        assert uuid
        assert uuid != self.item.getPath()
class TestIndividualRealContentItems(unittest.TestCase):
    layer = CONTENTLISTING_FUNCTIONAL_TESTING

    def setUp(self):
        super(TestIndividualRealContentItems, self).setUp()
        self.portal = self.layer['portal']
        self.folder = self.portal['test-folder']
        self.folder.invokeFactory(
            'Document',
            'mypage',
            title='My Page',
            description='blah',
        )
        self.item = IContentListingObject(self.folder.mypage)
        self.realitem = self.folder.mypage

    def test_printing_item(self):
        self.assertEqual(
            repr(self.item),
            '<plone.app.contentlisting.realobject.RealContentListingObject '
            'instance at /plone/test-folder/mypage>',
        )
        self.assertEqual(
            str(self.item),
            '<plone.app.contentlisting.realobject.RealContentListingObject '
            'instance at /plone/test-folder/mypage>',
        )

    def test_special_getattr_with_underscore(self):
        # looking up attributes starting with _ should always raise
        # AttributeError
        self.assertRaises(AttributeError, self.item.__getattr__, 'foo')

    def test_special_getattr_from_object(self):
        # Asking for an attribute not in the contentlistingobject, should
        # defer lookup to the brain
        self.assertEqual(self.item.absolute_url(), '')
        self.assertEqual(
            repr(self.item.getDataOrigin()),
            '<Document at /plone/test-folder/mypage>',
        )

    def test_item_Title(self):
        self.assertEqual(self.item.Title(), 'My Page')
        self.assertEqual(self.item.Title(), self.realitem.Title())

    def test_item_Description(self):
        self.assertEqual(self.item.Description(), 'blah')
        self.assertEqual(self.item.Description(), self.realitem.Description())

    def test_item_Creator(self):
        self.assertEqual(self.item.Creator(), 'test_user_1_')

    def test_item_getURL(self):
        self.assertEqual(
            self.item.getURL(),
            'http://nohost/plone/test-folder/mypage',
        )
        self.assertEqual(self.item.getURL(), self.realitem.absolute_url())

    def test_item_reviewState(self):
        wftool = getToolByName(self.realitem, 'portal_workflow')
        wf = wftool.getInfoFor(self.realitem, 'review_state')
        self.assertEqual(self.item.review_state(), wf)

    def test_item_Type(self):
        self.assertEqual(self.item.Type(), u'Page')
        self.assertEqual(self.item.Type().domain, u'plone')

    def test_item_ContentTypeClass(self):
        # checking the that we print nice strings for css class identifiers
        self.assertEqual(self.item.ContentTypeClass(), 'contenttype-document')

    def test_item_uuid(self):
        uuid = self.item.uuid()
        assert uuid
        assert uuid != self.item.getPath()