Skip to content

Commit

Permalink
Add min and max limits to the date batch.
Browse files Browse the repository at this point in the history
  • Loading branch information
thefunny42 committed Sep 19, 2012
1 parent 64fca23 commit 2a8d202
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 10 deletions.
3 changes: 3 additions & 0 deletions docs/HISTORY.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
----------------

Expand Down
35 changes: 33 additions & 2 deletions src/zeam/utils/batch/date/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -27,22 +28,52 @@ 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):
return 12

@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)
52 changes: 52 additions & 0 deletions src/zeam/utils/batch/date/batch.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------
Expand Down
17 changes: 11 additions & 6 deletions src/zeam/utils/batch/date/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 4 additions & 2 deletions src/zeam/utils/batch/date/views_templates/batchpages.pt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
tal:define="batch_previous batch/batch_previous;
batch_next batch/batch_next">
<a href="#" class="previous"
tal:attributes="href batch/batch_previous/url">
tal:attributes="href batch_previous/url"
tal:condition="batch_previous">
&lt; <tal:year tal:replace="batch_previous/year" />
</a>
<tal:repeat tal:repeat="item batch/batch">
Expand All @@ -13,7 +14,8 @@
</a>
</tal:repeat>
<a href="#" class="next"
tal:attributes="href batch/batch_next/url">
tal:attributes="href batch_next/url"
tal:condition="batch_next">
<tal:year tal:replace="batch_next/year" /> &gt;
</a>
</div>
2 changes: 2 additions & 0 deletions src/zeam/utils/batch/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 2a8d202

Please sign in to comment.