Exemplo n.º 1
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.º 2
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.º 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)
    def test_expire_session_data(self):
        """Test expire_session_data function."""
        request = self.request
        request.SESSION = {}
        expire_session_data(request)
        self.assertEqual(request.SESSION, {})

        request.SESSION = {
            'foo': 'bar',
            'querynextprev.foo': 'bar',
            'querynextprev.bar': 'foo',
            }
        expire_session_data(request)
        self.assertEqual(request.SESSION, {'foo': 'bar'})
    def test_expire_session_data(self):
        """Test expire_session_data function."""
        request = self.request
        request.SESSION = {}
        expire_session_data(request)
        self.assertEqual(request.SESSION, {})

        request.SESSION = {
            'foo': 'bar',
            'querynextprev.foo': 'bar',
            'querynextprev.bar': 'foo',
        }
        expire_session_data(request)
        self.assertEqual(request.SESSION, {'foo': 'bar'})