Skip to content

Commit

Permalink
Add QuerySet.depth() modifier
Browse files Browse the repository at this point in the history
This allows specifying the search depth for items
  • Loading branch information
ecederstrand authored Oct 7, 2022
1 parent 2de8a5b commit 5335725
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions exchangelib/queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from future.utils import python_2_unicode_compatible

from .items import CalendarItem, ID_ONLY
from .items import CalendarItem, Item, Persona, ALL_OCCURRENCIES, ID_ONLY, SHALLOW
from .fields import FieldPath, FieldOrder
from .restriction import Q
from .version import EXCHANGE_2010
Expand Down Expand Up @@ -78,6 +78,7 @@ def __init__(self, folder_collection, request_type=ITEM):
self.calendar_view = None
self.page_size = None
self.max_items = None
self._depth = SHALLOW

self._cache = None

Expand Down Expand Up @@ -108,14 +109,14 @@ def copy(self):
new_qs.calendar_view = self.calendar_view
new_qs.page_size = self.page_size
new_qs.max_items = self.max_items
new_qs._depth = self._depth
return new_qs

@property
def is_cached(self):
return self._cache is not None

def _get_field_path(self, field_path):
from .items import Persona
if self.request_type == self.PERSONA:
return FieldPath(field=Persona.get_field_by_fieldname(field_path))
for folder in self.folder_collection:
Expand All @@ -126,7 +127,6 @@ def _get_field_path(self, field_path):
raise ValueError("Unknown fieldname '%s' on folders '%s'" % (field_path, self.folder_collection.folders))

def _get_field_order(self, field_path):
from .items import Persona
if self.request_type == self.PERSONA:
return FieldOrder(
field_path=FieldPath(field=Persona.get_field_by_fieldname(field_path.lstrip('-'))),
Expand Down Expand Up @@ -178,8 +178,6 @@ def _format_items(self, items, return_format):
}[return_format](items)

def _query(self):
from .folders import SHALLOW
from .items import Persona
if self.only_fields is None:
# We didn't restrict list of field paths. Get all fields from the server, including extended properties.
if self.request_type == self.PERSONA:
Expand Down Expand Up @@ -227,6 +225,7 @@ def _query(self):
else:
find_item_kwargs = dict(
shape=ID_ONLY, # Always use IdOnly here, because AllProperties doesn't actually get *all* properties
depth=self._depth,
additional_fields=additional_fields,
order_fields=order_fields,
calendar_view=self.calendar_view,
Expand Down Expand Up @@ -369,7 +368,6 @@ def _item_yielder(self, iterable, item_func, id_only_func, changekey_only_func,
yield item_func(i)

def _as_items(self, iterable):
from .items import Item
return self._item_yielder(
iterable=iterable,
item_func=lambda i: i,
Expand Down Expand Up @@ -515,6 +513,13 @@ def values_list(self, *args, **kwargs):
new_qs.return_format = self.FLAT if flat else self.VALUES_LIST
return new_qs

def depth(self, depth):
"""Specify the search depth (SHALLOW, ASSOCIATED or DEEP)
"""
new_qs = self.copy()
new_qs._depth = depth
return new_qs

###########################
#
# Methods that end chaining
Expand Down Expand Up @@ -576,7 +581,6 @@ def exists(self):
def delete(self, page_size=1000):
""" Delete the items matching the query, with as little effort as possible. 'page_size' is the number of items
to fetch and delete from the server per request. We're only fetching the IDs, so keep it high"""
from .items import ALL_OCCURRENCIES
if self.is_cached:
res = self.folder_collection.account.bulk_delete(
ids=self._cache,
Expand Down

0 comments on commit 5335725

Please sign in to comment.