Batch navigation
----------------

First we need a content to apply the batch on::

  >>> from zope.interface import implements
  >>> from persistent import Persistent
  >>> from zeam.utils.batch.interfaces import IBatchedContent
  >>> class Content(Persistent):
  ...    implements(IBatchedContent)
  >>> app = getRootFolder()
  >>> app['content'] = Content()
  >>> content = app['content']
  >>> IBatchedContent.providedBy(content)
  True

After we need a batch::

  >>> from zeam.utils.batch.batch import batch
  >>> list_long = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
  >>> b = batch(list_long, count=4)

And a request::

  >>> from zope.publisher.browser import TestRequest
  >>> request = TestRequest()

So we can get the view::

  >>> from zope.component import queryMultiAdapter
  >>> from zeam.utils.batch.interfaces import IBatchView
  >>> view = queryMultiAdapter((content, b, request), IBatchView)
  >>> view
  <zeam.utils.batch.browser.views.batchView object at ...>
  >>> from zope.interface.verify import verifyObject
  >>> verifyObject(IBatchView, view)
  True

We are on the first page, start is 0::

  >>> view.previous
  >>> view.next
  'http://127.0.0.1/content/++batch++4'
  >>> for b in view.batch:
  ...    print b
  {'url': 'http://127.0.0.1/content', 'style': 'current', 'name': 1}
  {'url': 'http://127.0.0.1/content/++batch++4', 'style': None, 'name': 2}
  {'url': 'http://127.0.0.1/content/++batch++8', 'style': None, 'name': 3}

We set start to 4::

  >>> b = batch(list_long, start=4, count=4)
  >>> view = queryMultiAdapter((content, b, request), IBatchView)
  >>> view.previous
  'http://127.0.0.1/content'
  >>> view.next
  'http://127.0.0.1/content/++batch++8'
  >>> for b in view.batch:
  ...    print b
  {'url': 'http://127.0.0.1/content', 'style': None, 'name': 1}
  {'url': 'http://127.0.0.1/content/++batch++4', 'style': 'current', 'name': 2}
  {'url': 'http://127.0.0.1/content/++batch++8', 'style': None, 'name': 3}

We set start to 8, last page::

  >>> b = batch(list_long, start=8, count=4)
  >>> view = queryMultiAdapter((content, b, request), IBatchView)
  >>> view.previous
  'http://127.0.0.1/content/++batch++4'
  >>> view.next
  >>> for b in view.batch:
  ...    print b
  {'url': 'http://127.0.0.1/content', 'style': None, 'name': 1}
  {'url': 'http://127.0.0.1/content/++batch++4', 'style': None, 'name': 2}
  {'url': 'http://127.0.0.1/content/++batch++8', 'style': 'current', 'name': 3}

Now we have less than one page::

  >>> b = batch(['a', 'b',], count=4)
  >>> view = queryMultiAdapter((content, b, request), IBatchView)
  >>> view.previous
  >>> view.next
  >>> for b in view.batch:
  ...    print b

The number of item is modulo to the size of the batch::

  >>> list_short = [1, 2, 3, 4, 5, 6, 7, 8]
  >>> b = batch(list_short, start=4, count=4)
  >>> view = queryMultiAdapter((content, b, request), IBatchView)
  >>> view.previous
  'http://127.0.0.1/content'
  >>> view.next
  >>> for b in view.batch:
  ...    print b
  {'url': 'http://127.0.0.1/content', 'style': None, 'name': 1}
  {'url': 'http://127.0.0.1/content/++batch++4', 'style': 'current', 'name': 2}

If you put a name, it's inserted in the link::

  >>> b = batch(list_long, start=4, count=4, name='video')
  >>> view = queryMultiAdapter((content, b, request), IBatchView)
  >>> view.previous
  'http://127.0.0.1/content'
  >>> view.next
  'http://127.0.0.1/content/++batch++video+8'
  >>> for b in view.batch:
  ...    print b
  {'url': 'http://127.0.0.1/content', 'style': None, 'name': 1}
  {'url': 'http://127.0.0.1/content/++batch++video+4', 'style': 'current', 'name': 2}
  {'url': 'http://127.0.0.1/content/++batch++video+8', 'style': None, 'name': 3}


Tear down:

  >>> sync()
