reporter.batch basically finished

git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@1237 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
helmutm 2006-06-02 08:39:29 +00:00
parent d659933c04
commit 7c8fbb6532
9 changed files with 140 additions and 32 deletions

View file

@ -14,4 +14,13 @@
menu="zmi_views" title="Contents"
/>
<page
for="zope.app.container.interfaces.IContainer"
name="contents.html"
template="contents.pt"
class="cybertools.container.ordered.ContainerView"
permission="zope.ManageContent"
menu="zmi_views" title="Contents"
/>
</configure>

View file

@ -52,8 +52,10 @@
</tr>
</metal:block>
<metal:block tal:define="supportsRename view/supportsRename"
tal:repeat="item container_contents">
<metal:block tal:define="supportsRename view/supportsRename;
batch nocall:context/@@cybertools.reporter.batch;
batch python:batch.setup(container_contents)">
<tal:list repeat="item batch/items">
<tr tal:define="oddrow repeat/item/odd; url item/url;
id_quoted item/id/url:quote"
tal:attributes="class python:oddrow and 'even' or 'odd'" >
@ -118,6 +120,43 @@
tal:content="modified"
i18n:translate="">&nbsp;</span></td>
</tr>
</tal:list>
<metal:nav define-macro="batch_navigation_tr">
<tr class="batch_navigation"
style="border-top: 1px solid #ccc"
tal:condition="batch/showNavigation">
<td colspan="6"
style="text-align: center">
Pages:
<metal:nav define-macro="batch_navigation"
tal:define="first batch/first/title;
previous batch/previous/title;
current batch/current/title;
next batch/next/title;
last batch/last/title;">
<a href="#"
tal:attributes="href batch/first/url"
tal:content="first"
tal:condition="python: first != current">1</a>
<span tal:condition="python: first &lt; previous-1">...</span>
<a href="#"
tal:attributes="href batch/previous/url"
tal:content="batch/previous/title"
tal:condition="python: first != previous and previous != current">2</a>
<b tal:content="batch/current/title">3</b>
<a href="#"
tal:attributes="href batch/next/url"
tal:content="batch/next/title"
tal:condition="python: last != next and next != current">3</a>
<span tal:condition="python: last > next+1">...</span>
<a href="#"
tal:attributes="href batch/last/url"
tal:content="batch/last/title"
tal:condition="python: last != current">5</a>
</metal:nav>
</td>
</tr>
</metal:nav>
</metal:block>
</tbody>

View file

@ -29,7 +29,15 @@ from zope.app.container.browser.contents import JustContents
from zope.app.i18n import ZopeMessageFactory as _
class OrderedContainerView(JustContents):
class ContainerView(JustContents):
def checkMoveAction(self):
pass
orderable = False
class OrderedContainerView(ContainerView):
""" A view providing the necessary methods for moving sub-objects
within an ordered container.
"""

View file

@ -35,10 +35,16 @@ We'll use a fairly simple Iterable:
We are now ready to use the corresponding browser view:
>>> from zope.publisher.browser import TestRequest
>>> form = dict(b_page=1, b_size=4)
>>> form = dict(b_page=2, b_size=4, b_overlap=1)
>>> request = TestRequest(form=form)
>>> from cybertools.reporter.browser.batch import BatchView
>>> bview = BatchView(it, request)
>>> bview = BatchView(None, request)
>>> bview.setup(it)
<...BatchView...>
>>> bview.items()
[3, 4, 5, 6]
>>> bview.last
{'url': 'http://127.0.0.1?b_page=5&b_size=4&b_overlap=1&b_orphan=0', 'title': 5}
The real reporting stuff
------------------------

View file

@ -32,24 +32,29 @@ class Batch(object):
lastPage = False
def __init__(self, iterable, pageIndex=0, size=20, overlap=0, orphan=0):
self.iterable = list(iterable)
if type(iterable) not in (tuple, list):
iterable = list(iterable)
self.iterable = iterable
length = len(self.iterable)
self.pages = range(0, length, size-overlap)
if pageIndex >= len(self.pages):
pageIndex = len(self.pages) - 1
if pageIndex < 0:
pageIndex = 0
self.pageIndex = pageIndex
self.start = pageIndex * (size - overlap)
self.size = self.actualSize = size
self.overlap = overlap
self.orphan = orphan
length = len(self.iterable)
self.pages = range(0, length, size-overlap)
lastPage = self.pages[-1]
if length == 0: lastPage = 0
else: lastPage = self.pages[-1]
lastLen = length - lastPage
if lastLen <= orphan + overlap:
del self.pages[-1]
if length > 0:
del self.pages[-1]
if pageIndex == len(self.pages) - 1: #we're on the last page
self.actualSize = size + lastLen #take over the orphans
if pageIndex >= len(self.pages) or pageIndex < 0:
self.items = []
else:
self.items = self.iterable[self.start:self.start+self.actualSize]
self.items = self.iterable[self.start:self.start+self.actualSize]
def __getitem__(self, idx):
return self.items[idx]
@ -66,13 +71,13 @@ class Batch(object):
if idx < 0:
return 0
if idx >= len(self.pages):
return len(self.pages) - 1
return max(len(self.pages) - 1, 0)
return idx
def getIndexAbsolute(self, idx=0):
if idx < 0:
idx = len(self.pages) + idx
if idx < 0 or idx >= len(self.pages):
return None
idx = max(len(self.pages) + idx, 0)
#if idx < 0 or idx >= len(self.pages):
# return None
return idx

View file

@ -23,38 +23,62 @@ HTML providing template.
$Id$
"""
from zope.cachedescriptors.property import Lazy
from cybertools.reporter.batch import Batch
class BatchView(object):
def __init__(self, context, request):
def __init__(self, context, request, iterable=None):
self.context = context
self.request = request
form = request.form
if iterable is not None:
self.setup(iterable)
def setup(self, iterable):
form = self.request.form
page = int(form.get('b_page', 1))
size = int(form.get('b_size', 20))
overlap = int(form.get('b_overlap', 0))
orphan = int(form.get('b_orphan', 0))
self.batch = Batch(context, page-1, size, overlap, orphan)
self.batch = Batch(iterable, page-1, size, overlap, orphan)
return self
def items(self):
return setupUrlParams(self.batch.items)
return self.batch.items
@Lazy
def current(self):
return self.info(self.batch.getIndexRelative(0))
@Lazy
def first(self):
return setupUrlParams(self.batch.getIndexAbsolute(0))
return self.info(self.batch.getIndexAbsolute(0))
@Lazy
def last(self):
return setupUrlParams(self.batch.getIndexAbsolute(-1))
return self.info(self.batch.getIndexAbsolute(-1))
@Lazy
def previous(self):
return setupUrlParams(self.batch.getIndexRelative(1))
return self.info(self.batch.getIndexRelative(-1))
@Lazy
def next(self):
return setupUrlParams(self.batch.getIndexRelative(-1))
return self.info(self.batch.getIndexRelative(1))
def setupUrlParams(self, page):
def urlParams(self, page):
batch = self.batch
return ('?b_page=%i&b_size=%i&b_overlap=%i&b_orphan=%i'
% (page, self.size, self.overlap, self.orphan) )
% (page+1, batch.size, batch.overlap, batch.orphan) )
def url(self, page):
return str(self.request.URL) + self.urlParams(page)
def info(self, page):
return {'title': page+1, 'url': self.url(page)}
def showNavigation(self):
return self.first['title'] != self.last['title']

View file

@ -0,0 +1,15 @@
<!-- $Id$ -->
<configure
xmlns="http://namespaces.zope.org/browser"
xmlns:zope="http://namespaces.zope.org/zope"
i18n_domain="zope">
<page
for="*"
name="cybertools.reporter.batch"
class="cybertools.reporter.browser.batch.BatchView"
permission="zope.View"
/>
</configure>

View file

@ -5,4 +5,6 @@
xmlns:browser="http://namespaces.zope.org/browser"
i18n_domain="zope">
<include package=".browser" />
</configure>

View file

@ -38,16 +38,16 @@ class IBatch(Interface):
start = Attribute(u'The current start index of the batch in the parent iterable')
def __getitem__(idx):
""" Return the item at index idx.
""" Return the item at index idx on the current page.
"""
def next():
""" Return the next item in the batch. Raise StopIteration if
""" Return the next item on the current page. Raise StopIteration if
the end of the batch is reached.
"""
def __len__():
""" Return the number of items in the batch.
""" Return the number of items on the current page.
"""
def getIndexRelative(relativePageNumber):