如何比较两个NewsItem列表并减去相同的NewsItem?

时间:2019-05-20 02:53:49

标签: python python-2.7 plone zope plone-4.x

我的Plone是4.x和Python 2.7。

我创建了一个模板,请参见代码段:

    tal:define="path_list here/getPhysicalPath;
                path python:'/'.join(path_list);
                pathdepth viewletOptions/path/depth | python:-1;
                highlighted python:here.portal_catalog(path={'query':path,'depth':pathdepth}
                                                    ,portal_type='News Item'
                                                    ,review_state='highlight'
                                                    ,sort_on='effective'
                                                    ,sort_order='reverse'
                                                    ,hasImage=True)[:4];
                oldnew python:here.portal_catalog(path={'query':path,'depth':pathdepth}
                                                    ,portal_type='News Item'
                                                    ,review_state=['highlight','published']
                                                    ,sort_on='effective'
                                                    ,sort_order='reverse')[5:7];">

我有两个列表highlighted'n oldnew,我尝试在ZMI中非常简单地创建脚本(python)

    for i in highlighted:
      if i in oldnew:
        oldnew.remove(i)
    return oldnew

并提出了一个错误

TypeError: mybrains.__cmp__(x,y) requires y to be a 'mybrains', not a 'Acquisition.ImplicitAcquisitionWrapper'

如何删除highlightedoldnew的相同NewsItens?

1 个答案:

答案 0 :(得分:0)

由于为项目分配了多个工作流,因此无法使用ZMI脚本来实现,因为由于Python受限制,当您尝试访问多个工作流状态时会获得“权限不足”。

请使用浏览器视图,并包括以下帮助方法以获取所需的项目:

def getStates(context, obj):
    """
    For each assigned workflow of object collect state and return all states.
    """
    states = []
    wfs = context.portal_workflow.getWorkflowsFor(obj)
    for wf in wfs:
        state_info_dict = wf.getStatusOf(wf.id, obj)
        state = state_info_dict['review_state']
        states.append(state)
    return states


def getChildrenByStates(context, include_state='highlighted', exclude_state='published'):
    """"
    Get all children of context which have the include_state but not not the exclude_state.
    """
    objects = []
    path = '/'.join(context.aq_inner.getPhysicalPath())
    search_results = context.portal_catalog(path={'query':path,'depth':-1})
    for search_result in search_results:
        obj = search_result.getObject()
        states = getStates(context, obj)
        if include_state in states and not exclude_state in states:
            objects.append(obj)
    return objects