针对多天事件的portal_catalog查询

时间:2016-05-25 13:30:11

标签: plone

是否可以在portal_catalog中查询未来事件和当前正在运行的事件?

例如:

start >= now <= end or
start <= now <= end

或者是否有其他可能的查询?

Plone 4
plone.app.event

2 个答案:

答案 0 :(得分:2)

plone目录的查询选项非常有限,afaik唯一支持or运算符的索引是KeywordIndex

一种可能的解决方案是将其拆分为两个查询。

from DateTime import DateTime

query_current = {}
query_current['start'] = {'query': DateTime(),
                          'range': 'min'}
query_current['end'] = {'query': DateTime(),
                        'range': 'max'}

query_future = {}
query_future['start'] = {'query': DateTime(),
                         'range': 'min'}
query_future['end'] = {'query': DateTime(),
                       'range': 'min'}


items = list(portal_catalog(**query_current)) + list(portal_catalog(**query_future))

这有很好的记录:http://docs.plone.org/4/en/develop/plone/searching_and_indexing/query.html#querying-by-date

另一种可能性被称为Products.AdvancedQuery,但说实话我从未使用过它,因为我猜它已经死了 - &gt; http://www.dieter.handshake.de/pyprojects/zope/AdvancedQuery.html 你可能无法安装它。

答案 1 :(得分:0)

谢谢你,我会在几天内尝试一下

现在这对我有用

now = localized_now(self.context)
now_timestamp = time.mktime(now.timetuple())

# get all events out of catalog
allEvents = portal_catalog(
    portal_type="Event",
    review_state="published",
    sort_on='start',
    path={'query': nav_root_path}
)
items = []
for i in allEvents:
    obj = i.getObject()
    start_timestamp = time.mktime(obj.start.timetuple())
    end_timestamp = time.mktime(obj.end.timetuple())
    if start_timestamp >= now_timestamp <= end_timestamp or\
       start_timestamp <= now_timestamp <= end_timestamp:
        items.append((i, i.start))