O365 / python-o365

A simple python library to interact with Microsoft Graph and Office 365 API
Apache License 2.0
1.66k stars 420 forks source link

Query: Or condition breaking date ranges. #645

Open Aens opened 3 years ago

Aens commented 3 years ago

So, this is pretty straightforward query for a mailbox object, take a look at the query:

query = mailbox.new_query()
(query
.on_attribute("from")
.contains("an_email@example.com")
# .chain('or')
# .contains("another_mail@example.com")
.chain('and')
.on_attribute('receivedDateTime')
.greater_equal(datetime(2021, 6, 14))
.chain('and')
.on_attribute('receivedDateTime')
.less_equal(datetime(2021, 6, 23)))

When I un-comment those 2 lines, the code gets mails from outside of that date range. Is it a bug?

Aens commented 3 years ago

Update: A workaround has been adding the date ranges for EACH address, but isn't that a bit... annoying?

query = mailbox.new_query()
(query
.on_attribute("from")
.contains("an_email@example.com")
.chain('and')
.on_attribute('receivedDateTime')
.greater_equal(datetime(2021, 6, 14))
.chain('and')
.on_attribute('receivedDateTime')
.less_equal(datetime(2021, 6, 23))
.chain('or')
.contains("another_mail@example.com")
.chain('and')
.on_attribute('receivedDateTime')
.greater_equal(datetime(2021, 6, 14))
.chain('and')
.on_attribute('receivedDateTime')
.less_equal(datetime(2021, 6, 23))
)

I can see this becoming very obnoxious once we have many different addresses.

alejcas commented 3 years ago

Try with open_group and close_group.

Those effectively add parenthesis to the ODATA formula.

alejcas commented 3 years ago

Try with open_group and close_group.

Does this worked for you?