How can I compare two lists of NewsItem and subtract the same NewsItem?

159 Views Asked by At

My Plone is 4.x and Python 2.7.

I create a Template, see the snippet:

    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];">

I have two list highlighted 'n oldnew, and I tried creating a script (python) in ZMI very simple

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

And raised a error

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

How can I remove the same NewsItens of highlighted in oldnew?

1

There are 1 best solutions below

1
Ida On

As you have several workflows assigned for items, this cannot be achieved using a ZMI-script, because you'll get "Insufficient Privileges" when trying to access the several workflow-states, due to restricted Python.

Instead use a browser-view and include these helper-methods for getting the desired items:

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