Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
thet opened this issue May 15, 2020 · 2 comments
Open

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

thet opened this issue May 15, 2020 · 2 comments
Assignees

Comments

@thet
Copy link
Member

thet commented May 15, 2020

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:

  • using a key function in sorted(iterable, *, key=None, reverse=False) to transform missing values into a ceiling date
  • Not using ceiling dates when indexing start/end but not index them at all. Events with missing start/end dates wont then show up in event results when limiting to a time range.
  • No change but just requiring valid start/end dates for events - otherwise they are no events by definition. To allow for optional start/end dates on content types would then require dynamic behaviors or the behavior not implementing IDXEvent but setting IDXEvent dynamicaly after modification when start/end are valid datetimes.

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

@thet thet self-assigned this May 15, 2020
@ale-rt
Copy link
Member

ale-rt commented May 15, 2020

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
Copy link
Member

ale-rt commented May 15, 2020

Ah! And also the indexer should be something like:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants