def test_first_common_item(self):
        """Test first common item util."""
        l1 = [4, 5, 6, 7]
        l2 = [1, 2, 6, 7]

        self.assertEqual(
            first_common_item(l1, l2),
            6)

        l1 = [1, 2, 4, 5, 6, 7]
        l2 = [1, 2, 6, 7]
        self.assertEqual(
            first_common_item(l1, l2),
            1)

        l1 = [4, 5, 6, 7]
        l2 = [1, 2]
        self.assertIsNone(
            first_common_item(l1, l2))

        l1 = [1]
        l2 = [1]
        self.assertEqual(
            first_common_item(l1, l2),
            1)
    def test_first_common_item(self):
        """Test first common item util."""
        l1 = [4, 5, 6, 7]
        l2 = [1, 2, 6, 7]

        self.assertEqual(
            first_common_item(l1, l2),
            6)

        l1 = [1, 2, 4, 5, 6, 7]
        l2 = [1, 2, 6, 7]
        self.assertEqual(
            first_common_item(l1, l2),
            1)

        l1 = [4, 5, 6, 7]
        l2 = [1, 2]
        self.assertIsNone(
            first_common_item(l1, l2))

        l1 = [1]
        l2 = [1]
        self.assertEqual(
            first_common_item(l1, l2),
            1)
Exemplo n.º 3
0
    def update(self):
        if INextPrevNotNavigable.providedBy(self.context):
            return

        session = self.request.SESSION
        if session.has_key(QUERY):  # noqa
            query = session[QUERY]
            params = convert_to_str(
                json.loads(query, object_hook=json_object_hook)
            )
            catalog = api.portal.get_tool('portal_catalog')
            brains = catalog.searchResults(**params)

            # if we have too many results, we return and delete session data
            max_res = api.portal.get_registry_record('collective.querynextprev.maxresults') or 250
            if len(brains) > max_res:
                self.is_navigable = False
                expire_session_data(self.request)
                return

            uids = [brain.UID for brain in brains]  # noqa #pylint: disable=E1103
            context_uid = self.context.UID()
            if context_uid in uids and len(uids) > 1:
                self.is_navigable = True
                context_index = uids.index(context_uid)
                self.previous_uids = list(reversed(
                    get_previous_items(uids, context_index)))
                self.next_uids = get_next_items(uids, context_index)
                session[PREVIOUS_UIDS] = json.dumps(self.previous_uids)
                session[NEXT_UIDS] = json.dumps(self.next_uids)
                return  # don't delete session data
            elif session.has_key(PREVIOUS_UIDS) or session.has_key(NEXT_UIDS):  # noqa #pylint: disable=C0301
                # context is not in results anymore
                # get previous
                old_previous = json.loads(session[PREVIOUS_UIDS])
                previous_item = first_common_item(uids, old_previous)
                if previous_item:
                    self.is_navigable = True
                    index = uids.index(previous_item)
                    self.previous_uids = list(reversed(
                        get_previous_items(uids, index, include_index=True)))
                    session[PREVIOUS_UIDS] = json.dumps(self.previous_uids)

                # get next
                old_next = json.loads(session[NEXT_UIDS])
                next_item = first_common_item(uids, old_next)
                if next_item:
                    self.is_navigable = True
                    index = uids.index(next_item)
                    self.next_uids = get_next_items(
                        uids, index, include_index=True)
                    session[NEXT_UIDS] = json.dumps(self.next_uids)

                if previous_item or next_item:
                    return  # don't delete session data

            expire_session_data(self.request)
    def update(self):
        if INextPrevNotNavigable.providedBy(self.context):
            return

        session = self.request.SESSION
        if session.has_key(QUERY):  # noqa
            query = session[QUERY]
            params = convert_to_str(json.loads(query))
            catalog = api.portal.get_tool('portal_catalog')
            brains = catalog.searchResults(**params)

            # if we have too many results, we return and delete session data
            max_res = api.portal.get_registry_record('collective.querynextprev.maxresults') or 250
            if len(brains) > max_res:
                self.is_navigable = False
                expire_session_data(self.request)
                return

            uids = [brain.UID for brain in brains]  # noqa #pylint: disable=E1103
            context_uid = self.context.UID()
            if context_uid in uids and len(uids) > 1:
                self.is_navigable = True
                context_index = uids.index(context_uid)
                self.previous_uids = list(reversed(
                    get_previous_items(uids, context_index)))
                self.next_uids = get_next_items(uids, context_index)
                session[PREVIOUS_UIDS] = json.dumps(self.previous_uids)
                session[NEXT_UIDS] = json.dumps(self.next_uids)
                return  # don't delete session data
            elif session.has_key(PREVIOUS_UIDS) or session.has_key(NEXT_UIDS):  # noqa #pylint: disable=C0301
                # context is not in results anymore
                # get previous
                old_previous = json.loads(session[PREVIOUS_UIDS])
                previous_item = first_common_item(uids, old_previous)
                if previous_item:
                    self.is_navigable = True
                    index = uids.index(previous_item)
                    self.previous_uids = list(reversed(
                        get_previous_items(uids, index, include_index=True)))
                    session[PREVIOUS_UIDS] = json.dumps(self.previous_uids)

                # get next
                old_next = json.loads(session[NEXT_UIDS])
                next_item = first_common_item(uids, old_next)
                if next_item:
                    self.is_navigable = True
                    index = uids.index(next_item)
                    self.next_uids = get_next_items(
                        uids, index, include_index=True)
                    session[NEXT_UIDS] = json.dumps(self.next_uids)

                if previous_item or next_item:
                    return  # don't delete session data

            expire_session_data(self.request)
Exemplo n.º 5
0
    def __call__(self):
        request = self.request
        session = request.SESSION
        if session.has_key(QUERY) and session.has_key(self.uids_param):  # noqa
            next_uids = convert_to_str(json.loads(session[self.uids_param]))

            # reexecute the query to search within most recent results
            new_uids = self.get_uids()

            # search UID starting from context index in uids
            uid = first_common_item(next_uids, new_uids)
            if uid is not None:
                next_url = api.content.get(UID=uid).absolute_url()

                # update uids in session
                index = new_uids.index(uid)
                previous_uids = list(reversed(
                    get_previous_items(new_uids, index)))
                next_uids = get_next_items(new_uids, index)
                session[PREVIOUS_UIDS] = json.dumps(previous_uids)
                session[NEXT_UIDS] = json.dumps(next_uids)

                request.response.redirect(next_url)
                return  # don't expire session data

        if session.has_key(SEARCH_URL):  # noqa
            request.response.redirect(session[SEARCH_URL])
        else:
            request.response.redirect(api.portal.get().absolute_url())

        expire_session_data(request)
        return
Exemplo n.º 6
0
    def __call__(self):
        request = self.request
        session = request.SESSION
        if session.has_key(QUERY) and session.has_key(self.uids_param):  # noqa
            next_uids = convert_to_str(json.loads(session[self.uids_param]))

            # reexecute the query to search within most recent results
            new_uids = self.get_uids()

            # search UID starting from context index in uids
            uid = first_common_item(next_uids, new_uids)
            if uid is not None:
                next_url = api.content.get(UID=uid).absolute_url()

                # update uids in session
                index = new_uids.index(uid)
                previous_uids = list(reversed(
                    get_previous_items(new_uids, index)))
                next_uids = get_next_items(new_uids, index)
                session[PREVIOUS_UIDS] = json.dumps(previous_uids)
                session[NEXT_UIDS] = json.dumps(next_uids)

                request.response.redirect(next_url)
                return  # don't expire session data

        if session.has_key(SEARCH_URL):  # noqa
            request.response.redirect(session[SEARCH_URL])
        else:
            request.response.redirect(api.portal.get().absolute_url())

        expire_session_data(request)
        return