Skip to main content

Generic Batch support for Zope

Project description

This package provides a batch functionality for Zope 2, Zope 3 and Grok.

Example

A very straightforward example. We need to define a context to work on:

>>> import grokcore.view as grok
>>> from persistent import Persistent
>>> from zope.component import queryMultiAdapter
>>> from zeam.utils.batch import batch
>>> from zeam.utils.batch.interfaces import IBatching

>>> class Content(Persistent):
...     pass

And now, you can define a view which use a batch, and render it:

>>> class MyViewClass(grok.View):
...     grok.context(Content)
...
...     def update(self):
...          fulllist = [1, 2, 3, 4, 5, 6, 7, 8, 9]
...          self.myitems = batch(
...                 fulllist , count=3, name='nbs', request=self.request,
...                 factory=lambda x: str(x))
...
...          self.batch = queryMultiAdapter(
...                 (self.context, self.myitems, self.request),
...                 IBatching)()
...
...     template = grok.PageTemplate('''
...     <tal:navigation tal:replace="structure view/batch" />
...     <span tal:content="item" tal:repeat="item view/myitems" />
...     <tal:navigation tal:replace="structure view/batch" />
...     ''')

And this work:

>>> from grokcore.component import testing
>>> testing.grok_component("view", MyViewClass)
True

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

>>> root = getRootFolder()
>>> root['myObject'] = Content()
>>> myobj = root['myObject']

>>> view = queryMultiAdapter((myobj, request), name="myviewclass")
>>> "batchNav" in view()
True

API

batch

This object implements the batch.

The batch object is instanciated with the following arguments:

  • a list of the objects to batch

  • the request

  • the number of items per page

  • a name (optional)

  • a factory that will be passed each item before each iteration (optional)

The batch is an iterable object behaving like a list. It only gives access to the set of objects for the current page.

It provides the number of pages generated and the current position. Please refer to the interface, for more information.

A multi adapter providing IBatching can render the batch. It adapts the context, the batch object and the request. The __call__ method of this component will return a snippet of HTML containing basic controls for your batch: a next and previous link and a direct access to the other pages.

Changelog

0.6 (2010-07-15)

  • Generated batch links can include other parameters in the link. That let you batch form results for instance.

0.5 (2009-11-17)

  • Batching views have two new properties: first and last which gives links to the first and last batch.

0.4.1 (2009-10-16)

  • Corrected the broken distribution that was missing the i18n folder.

0.4 (2009-10-16)

  • Added README.txt as a doctest [trollfot]

  • The rendering is made by a IPageTemplate component, not longer by the Batching itself. [trollfot]

  • We no longer use the IBatchedContent. It has been removed. [trollfot]

  • zeam.utils.batch is now fully grokked. [trollfot]

  • batchView has been renamed Batching. [trollfot]

  • You adapt the batch with the view on which you display the batch: its name will keep when generating links,

  • Batch can be disabled with a count of 0,

  • Add translations for french, english and dutch.

0.3 (2008-10-18)

  • Fix and add tests,

  • Don’t display batch navigation if everything fits on one page,

  • No more special links are generated for the first page of the batch.

0.2

  • Initial release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zeam.utils.batch-0.6.tar.gz (9.9 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page