Skip to content

Commit

Permalink
override the apply() and values() methods
Browse files Browse the repository at this point in the history
  • Loading branch information
janwijbrand committed Nov 20, 2015
1 parent 701606c commit 2892a41
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/grokcore/catalog/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
"""
import sys
import calendar
import datetime
import BTrees.Length
import zope.container.contained
import zope.catalog.interfaces
import zope.catalog.attribute
import zope.container.contained
import zc.catalog.index

from zope.interface import implements
from zope.interface.interfaces import IMethod, IInterface
Expand Down Expand Up @@ -168,11 +168,7 @@ def to_timestamp(dt):
return calendar.timegm(dt.timetuple())


def from_timestamp(stamp):
return datetime.datetime.fromtimestamp(float(stamp))


class _DatetimeIndex(ValueIndex):
class _DatetimeIndex(zc.catalog.index.ValueIndex):

def clear(self):
self.values_to_documents = BTrees.LOBTree.LOBTree()
Expand All @@ -186,8 +182,21 @@ def index_doc(self, doc_id, value):
value = to_timestamp(value)
super(_DatetimeIndex, self).index_doc(doc_id, value)

# def apply():
# pass
def apply(self, query):
for name in ['any_of', 'between']:
if name not in query:
continue
# The "value" of the dict is a sequence of datetime objects.
# Convert it into a timestamps.
query[name] = [to_timestamp(v) for v in query[name]]
return super(_DatetimeIndex, self).apply(query)

def values(self, min=None, max=None, excludemin=False, excludemax=False,
doc_id=None):
min = to_timestamp(min) if min is not None else None
max = to_timestamp(max) if max is not None else None
return super(_DatetimeIndex, self).values(
min, max, excludemin, excludemax, doc_id)


class DatetimeIndex(
Expand Down

0 comments on commit 2892a41

Please sign in to comment.