diff --git a/caldav/objects.py b/caldav/objects.py index b2f84461..6226dac3 100644 --- a/caldav/objects.py +++ b/caldav/objects.py @@ -1220,6 +1220,13 @@ def search( xml, comp_class, props=props ) + for o in objects: + ## This would not be needed if the servers would follow the standard ... + o.load(only_if_unloaded=True) + + ## Google sometimes returns empty objects + objects = [o for o in objects if o.has_component()] + if kwargs.get("expand", False): ## expand can only be used together with start and end. ## Error checking is done in build_search_xml_query. If @@ -1229,13 +1236,6 @@ def search( start = kwargs["start"] end = kwargs["end"] - for o in objects: - ## This would not be needed if the servers would follow the standard ... - o.load(only_if_unloaded=True) - - ## Google sometimes returns empty objects - objects = [o for o in objects if o.icalendar_component] - for o in objects: component = o.icalendar_component if component is None: @@ -1248,9 +1248,6 @@ def search( objects = [] for o in objects_: objects.extend(o.split_expanded()) - else: - ## Google sometimes returns empty objects - objects = [o for o in objects if o.icalendar_component] def sort_key_func(x): ret = [] @@ -2122,6 +2119,8 @@ def _get_icalendar_component(self, assert_one=False): See also https://github.com/python-caldav/caldav/issues/232 """ self.load(only_if_unloaded=True) + if not self.icalendar_instance: + return None ret = [ x for x in self.icalendar_instance.subcomponents @@ -2530,6 +2529,17 @@ def is_loaded(self): self._data or self._vobject_instance or self._icalendar_instance ) and self.data.count("BEGIN:") > 1 + def has_component(self): + return ( + self._data + or self._vobject_instance + or (self._icalendar_instance and self.icalendar_component) + ) and self.data.count("BEGIN:VEVENT") + self.data.count( + "BEGIN:VTODO" + ) + self.data.count( + "BEGIN:VJOURNAL" + ) > 0 + def __str__(self) -> str: return "%s: %s" % (self.__class__.__name__, self.url) diff --git a/tests/test_caldav_unit.py b/tests/test_caldav_unit.py index 4efff154..572920f4 100644 --- a/tests/test_caldav_unit.py +++ b/tests/test_caldav_unit.py @@ -350,12 +350,14 @@ def testAbsoluteURL(self): "http://cal.example.com/home/bernard/calendars/" ) - @mock.patch("caldav.objects.CalendarObjectResource.is_loaded") - def testDateSearch(self, mocked): + def _load(self, only_if_unloaded=True): + self.data = todo6 + + @mock.patch("caldav.objects.CalendarObjectResource.load", new=_load) + def testDateSearch(self): """ ## ref https://github.com/python-caldav/caldav/issues/133 """ - mocked.__bool__ = lambda self: True xml = """ /principals/calendar/home@petroski.example.com/963/43B060B3-A023-48ED-B9E7-6FFD38D5073E.ics