针对电子邮件主题的多个过滤器查询的 ErrorSchemaValidation由于“已超出最大读取深度 (32)”

问题描述

我需要过滤掉主题包含我配置的列表中的多个字符串中的任何一个的电子邮件。我尝试使用几个 Q 表达式构建我的查询如下,因为目前(最后我检查过)不支持像 subject__in=["somestring1","somestring2",... "somestring25"] 这样的查询

list_of_subject_filter_string_patterns = ["somestring1",... "somestring25"]
filtered_items = my_exchange_account.inBox.filter(**kwargs)
query_sections = (Q(subject__icontains=pattern) for pattern in list_of_subject_filter_string_patterns)
query = reduce(operator.or_,query_sections)
filtered_items = filtered_items.filter(~query)

if filtered_items.exists():
    # do something
    ....

然而,当我在那个 Q 建筑中传递更长的模式列表时,这会爆炸。

File "/usr/local/lib/python3.6/site-packages/exchangelib/services/common.py",line 329,in _raise_soap_errors
    raise vars(errors)[code](msg) 
ErrorSchemaValidation: The request Failed schema validation. The maximum read depth (32) has been exceeded because XML data being read has more levels of nesting than is allowed by the quota. This quota may be increased by changing the MaxDepth property on the XmlDictionaryReaderQuotas object used when creating the XML reader. Line 1,position 499.

关于如何实现过滤掉多个主题模式的预期查询结果有什么想法吗?

解决方法

我认为您的减少没有达到您的预期。 reduce() 可能会产生类似于 (((("somestring1" OR "somestring2") OR "somestring3") OR "somestring4") OR "somestring5) ... 的东西,这对于 Exchange 服务器来说太多了。您可以启用 debug logging 以检查 XML 的确切格式。

如果您想对字符串进行精确匹配,exchangelib 确实支持 subject__in=[...]。见https://ecederstrand.github.io/exchangelib/#searching