diff --git a/docs/HISTORY.txt b/docs/HISTORY.txt
index 272f680..df0c2f0 100644
--- a/docs/HISTORY.txt
+++ b/docs/HISTORY.txt
@@ -7,6 +7,9 @@ Changelog
* Fix a bug when the ``start`` value is higher than the number of
element in the batch in the regular batch.
+* Add two options ``min`` and ``max`` to the date batch in order to
+ define possible limits for it.
+
1.0 (2011-11-07)
----------------
diff --git a/src/zeam/utils/batch/date/batch.py b/src/zeam/utils/batch/date/batch.py
index f04d3d1..67d2c43 100644
--- a/src/zeam/utils/batch/date/batch.py
+++ b/src/zeam/utils/batch/date/batch.py
@@ -15,7 +15,8 @@ class DateBatch(ActiveBatch):
def __init__(
self, collection,
- start=None, count=BATCH_MONTH, name='', request=None, factory=None):
+ start=None, count=BATCH_MONTH, name='', request=None, factory=None,
+ min=None, max=None):
if request is not None:
key = 'bstart'
if name:
@@ -27,13 +28,31 @@ def __init__(
pass
if start is None:
start = datetime.now()
+ self.min = min
+ self.max = max
super(DateBatch, self).__init__(
collection,
start=start, count=count, name=name,
request=request, factory=factory)
def all(self):
- for month in range(1, 13):
+ start = 1
+ if self.min is not None:
+ if self.min.year == self.start.year:
+ # We are on the starting year.
+ start = self.min.month
+ elif self.min.year > self.start.year:
+ # We are before the starting year
+ start = 13
+ end = 13
+ if self.max is not None:
+ if self.max.year == self.start.year:
+ # We are on the ending year
+ end = self.max.month + 1
+ elif self.max.year < self.start.year:
+ # We are after the ending year
+ end = 1
+ for month in range(start, end):
yield datetime(self.start.year, month, 1)
def batch_length(self):
@@ -41,8 +60,20 @@ def batch_length(self):
@property
def previous(self):
+ if self.min is not None and self.min.year >= self.start.year:
+ # We are before the minimal year.
+ return None
+ if self.max is not None and self.max.year < self.start.year:
+ # We are after the maximal year.
+ return None
return datetime(self.start.year - 1, 12, 1)
@property
def next(self):
+ if self.max is not None and self.max.year <= self.start.year:
+ # We are after the maximal year.
+ return None
+ if self.min is not None and self.min.year > self.start.year:
+ # We are before the minimal year.
+ return None
return datetime(self.start.year + 1, 1, 1)
diff --git a/src/zeam/utils/batch/date/batch.txt b/src/zeam/utils/batch/date/batch.txt
index 2125c50..f098f9d 100644
--- a/src/zeam/utils/batch/date/batch.txt
+++ b/src/zeam/utils/batch/date/batch.txt
@@ -74,6 +74,58 @@ And you can see all the months:
2011-11-01 00:00:00
2011-12-01 00:00:00
+Minimal and maximal dates:
+
+ >>> c = DateBatch(getter,
+ ... start=datetime(2011, 11, 11),
+ ... min=datetime(2011, 4, 1),
+ ... max=datetime(2012, 8, 30))
+ >>> c.previous
+ >>> c.next
+ datetime.datetime(2012, 1, 1, 0, 0)
+ >>> for month in c.all():
+ ... print month
+ 2011-04-01 00:00:00
+ 2011-05-01 00:00:00
+ 2011-06-01 00:00:00
+ 2011-07-01 00:00:00
+ 2011-08-01 00:00:00
+ 2011-09-01 00:00:00
+ 2011-10-01 00:00:00
+ 2011-11-01 00:00:00
+ 2011-12-01 00:00:00
+
+Change the start of the batch to the next one, test again limits:
+
+ >>> c.start = b.next
+ >>> c.previous
+ datetime.datetime(2011, 12, 1, 0, 0)
+ >>> c.next
+ >>> for month in c.all():
+ ... print month
+ 2012-01-01 00:00:00
+ 2012-02-01 00:00:00
+ 2012-03-01 00:00:00
+ 2012-04-01 00:00:00
+ 2012-05-01 00:00:00
+ 2012-06-01 00:00:00
+ 2012-07-01 00:00:00
+ 2012-08-01 00:00:00
+
+Or out of the range:
+
+ >>> c.start = datetime(2001, 4, 1)
+ >>> c.previous
+ >>> c.next
+ >>> for month in c.all():
+ ... print month
+
+ >>> c.start = datetime(2021, 4, 1)
+ >>> c.previous
+ >>> c.next
+ >>> for month in c.all():
+ ... print month
+
Factory
-------
diff --git a/src/zeam/utils/batch/date/views.py b/src/zeam/utils/batch/date/views.py
index 9a83ab3..c32f5a6 100644
--- a/src/zeam/utils/batch/date/views.py
+++ b/src/zeam/utils/batch/date/views.py
@@ -5,6 +5,7 @@
from zope.interface import Interface
from zope.publisher.interfaces.http import IHTTPRequest
+from zope.cachedescriptors.property import Lazy
from zeam.utils.batch.interfaces import IDateBatch
from zeam.utils.batch.views import BasicBatching
@@ -33,17 +34,21 @@ def batch(self):
url=url_item,
style=style)
- @property
+ @Lazy
def batch_previous(self):
previous = self._batch.previous
- previous_tms = previous.strftime("%Y-%m")
- return dict(year=previous.year, url=self._create_link(previous_tms))
+ if previous is not None:
+ previous_tms = previous.strftime("%Y-%m")
+ return dict(year=previous.year, url=self._create_link(previous_tms))
+ return {}
- @property
+ @Lazy
def batch_next(self):
next = self._batch.next
- next_tms = next.strftime("%Y-%m")
- return dict(year=next.year, url=self._create_link(next_tms))
+ if next is not None:
+ next_tms = next.strftime("%Y-%m")
+ return dict(year=next.year, url=self._create_link(next_tms))
+ return {}
class BatchPages(megrok.pagetemplate.PageTemplate):
diff --git a/src/zeam/utils/batch/date/views_templates/batchpages.pt b/src/zeam/utils/batch/date/views_templates/batchpages.pt
index d74afee..4af95a3 100644
--- a/src/zeam/utils/batch/date/views_templates/batchpages.pt
+++ b/src/zeam/utils/batch/date/views_templates/batchpages.pt
@@ -2,7 +2,8 @@
tal:define="batch_previous batch/batch_previous;
batch_next batch/batch_next">
+ tal:attributes="href batch_previous/url"
+ tal:condition="batch_previous">
<
@@ -13,7 +14,8 @@
+ tal:attributes="href batch_next/url"
+ tal:condition="batch_next">
>
diff --git a/src/zeam/utils/batch/interfaces.py b/src/zeam/utils/batch/interfaces.py
index c1fe7ee..aa6765b 100644
--- a/src/zeam/utils/batch/interfaces.py
+++ b/src/zeam/utils/batch/interfaces.py
@@ -60,6 +60,8 @@ class IActiveBatch(IBatchBehavior):
class IDateBatch(IActiveBatch):
"""Batch element by date.
"""
+ min = Attribute(u"Minimal date where to stop the batch")
+ max = Attribute(u"Maximal date where to stop the batch")
class IAlphabeticalBatch(IActiveBatch, IBatch):