python-o365 icon indicating copy to clipboard operation
python-o365 copied to clipboard

Question: Multiple conditions in q.any()

Open cepbuch opened this issue 5 years ago • 3 comments

I am trying to assign some custom id to message at the sending time (as message/send doesn't return anything as we know)

So, I am sending singleValueExtendedProperty {'id': property_id, 'value': property_value} when creating the message (overridden Message class) and now I want to find this message by mailbox.get_messages.

Such filter requires /any(a: a/id eq {id} and a/value eq {value}) to be passed as filter.

query = self.mailbox.q().any(
    collection='singleValueExtendedProperties',
    attribute='id', operation='eq',
    word=CUSTOM_MESSAGE_ID_PROPERTY
)

returns only /any(a: a/id eq {id}) while I want to add another condition to any() as I said above.

This works, but of course its ugly:

query._filters[0][1] = query._filters[0][1][:-1] + f" and a/value eq '{message_id}')"  # uuid
try:
     message = list(self.mailbox.get_messages(limit=None, query=query))[0]
     ....
...

Is there a way of doing this with built-in tools?

P.S. Thanks for the awesome library! Sometimes looking at the source code is way more helpful than the official outlook docs

cepbuch avatar Nov 07 '19 14:11 cepbuch

So, I am sending singleValueExtendedProperty {'id': property_id, 'value': property_value} when creating the message (overridden Message class) and now I want to find this message by mailbox.get_messages.

I've never used singleValueExtendedProperty. Can you share some code on the Message override?

At the moment Query.any() only accepts a single statement. I think maybe the code can be changed so some function args also accepts tuples so it can build multiple conditions.

P.S. Thanks for the awesome library! Sometimes looking at the source code is way more helpful than the official outlook docs

Great that you looked at the source!!

alejcas avatar Nov 07 '19 20:11 alejcas

Disclaimer: I took this "hack" to solve my problem from some deep-with-zero-upvotes-and-downvotes SO answers. Not sure it is 100% correct and the only way to solve my problem but still it's quite clear and works.

The class is quite simple - just adds new parameter according to singleValueExtendedProperty docs:

class SignedMessage(Message):
    """Additionally assigns custom message id through singleValueExtendedProperty to Message from O365.
    ...
    """
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.custom_message_id = None

    def to_api_data(self, restrict_keys=None):
        message = super().to_api_data(restrict_keys)
        self.custom_message_id = uuid4()
        message['singleValueExtendedProperties'] = [{
            'id': CUSTOM_MESSAGE_ID_PROPERTY,
            'value': str(self.custom_message_id)
        }]
        return message

cepbuch avatar Nov 07 '19 20:11 cepbuch

I'd be happy to create a PR to implement this class. Where is CUSTOM_MESSAGE_ID_PROPERTY set @cepbuch ?

kwilsonmg avatar Oct 10 '23 23:10 kwilsonmg