diff --git a/container/configure.zcml b/container/configure.zcml index 2af6c97..76ff1bc 100644 --- a/container/configure.zcml +++ b/container/configure.zcml @@ -14,4 +14,13 @@ menu="zmi_views" title="Contents" /> + + diff --git a/container/contents.pt b/container/contents.pt index 15d0d4d..f3aab5e 100644 --- a/container/contents.pt +++ b/container/contents.pt @@ -52,8 +52,10 @@ - + + @@ -118,6 +120,43 @@ tal:content="modified" i18n:translate=""> + + + + + Pages: + + 1 + ... + 2 + 3 + 3 + ... + 5 + + + + diff --git a/container/ordered.py b/container/ordered.py index 85070da..7d0e254 100644 --- a/container/ordered.py +++ b/container/ordered.py @@ -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. """ diff --git a/reporter/README.txt b/reporter/README.txt index b07ec57..9cdee79 100644 --- a/reporter/README.txt +++ b/reporter/README.txt @@ -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 ------------------------ diff --git a/reporter/batch.py b/reporter/batch.py index 11d0615..4fd7093 100644 --- a/reporter/batch.py +++ b/reporter/batch.py @@ -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 diff --git a/reporter/browser/batch.py b/reporter/browser/batch.py index 3998c2a..1bdb832 100644 --- a/reporter/browser/batch.py +++ b/reporter/browser/batch.py @@ -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'] + - diff --git a/reporter/browser/configure.zcml b/reporter/browser/configure.zcml new file mode 100644 index 0000000..49d79fe --- /dev/null +++ b/reporter/browser/configure.zcml @@ -0,0 +1,15 @@ + + + + + + + diff --git a/reporter/configure.zcml b/reporter/configure.zcml index db5642b..f820e5f 100644 --- a/reporter/configure.zcml +++ b/reporter/configure.zcml @@ -5,4 +5,6 @@ xmlns:browser="http://namespaces.zope.org/browser" i18n_domain="zope"> + + diff --git a/reporter/interfaces.py b/reporter/interfaces.py index 8ff7523..30d8767 100644 --- a/reporter/interfaces.py +++ b/reporter/interfaces.py @@ -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):