plone / plone.app.event

Event content type for Plone
https://pypi.python.org/pypi/plone.app.event
Other
17 stars 37 forks source link

events with empty start/end break on sorting in get_events #316

Open thet opened 4 years ago

thet commented 4 years ago

I encountered an issue with special events with optional start and end dates.

For that I created a behavior which derives from plone.app.event.dx.behaviors.IEventBasic, which itself implements plone.app.event.dx.interfaces.IDXEvent, which itself derives from plone.event.interfaces.IEvent which is used as search criteria in plone.app.event.base.get_events.

When using get_events and sorting on start like so:

        events = get_events(
            self.context,
            start=start,
            end=end,
            ret_mode=RET_MODE_ACCESSORS,
            expand=True,
            sort='start',
            sort_reverse=False,
            path=path
        )

it breaks with:

Traceback (innermost last):
  Module ZPublisher.Publish, line 138, in publish
  Module ZPublisher.mapply, line 77, in mapply
  Module ZPublisher.Publish, line 48, in call_object
  Module bda.aaf.services_nextroom.calendarexport, line 66, in __call__
  Module bda.aaf.services_nextroom.calendarexport, line 56, in get_events
  Module plone.app.event.base, line 163, in get_events
  Module plone.app.event.base, line 291, in expand_events
TypeError: can't compare datetime.datetime to NoneType
Traceback (most recent call last):
  File "/home/plone/.buildout/shared-eggs/Zope2-2.13.29-py2.7.egg/ZPublisher/Publish.py", line 138, in publish
    request, bind=1)
  File "/home/plone/.buildout/shared-eggs/Zope2-2.13.29-py2.7.egg/ZPublisher/mapply.py", line 77, in mapply
    if debug is not None: return debug(object,args,context)
  File "/home/plone/.buildout/shared-eggs/Zope2-2.13.29-py2.7.egg/ZPublisher/Publish.py", line 48, in call_object
    result=apply(object,args) # Type s<cr> to step into published object.
  File "/plone/buildout/src-aaf/bda.aaf.services_nextroom/src/bda/aaf/services_nextroom/calendarexport.py", line 66, in __call__
    for event in self.get_events():
  File "/plone/buildout/src-aaf/bda.aaf.services_nextroom/src/bda/aaf/services_nextroom/calendarexport.py", line 56, in get_events
    path=path
  File "/home/plone/.buildout/shared-eggs/plone.app.event-3.2.4-py2.7.egg/plone/app/event/base.py", line 163, in get_events
    sort_reverse)
  File "/home/plone/.buildout/shared-eggs/plone.app.event-3.2.4-py2.7.egg/plone/app/event/base.py", line 291, in expand_events
    exp_result.sort(key=lambda x: _get_compare_attr(x, sort))
TypeError: can't compare datetime.datetime to NoneType

I see the following options:

Any opinions? /cc @jensens @ale-rt @mauritsvanrees

ale-rt commented 4 years ago

Yes, I have already found this issue, you can replace None with datetime.min or datetime.max according to your needs:

>>> from datetime import datetime
>>> sorted((datetime.today(), None), key=lambda x: x or datetime.min)
[None, datetime.datetime(2020, 5, 15, 12, 49, 22, 317443)]
ale-rt commented 4 years ago

Ah! And also the indexer should be something like:

def start(obj):
     return obj.start or datetime.min