Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Multiple conditions in q.any() #354

Open
cepbuch opened this issue Nov 7, 2019 · 3 comments
Open

Question: Multiple conditions in q.any() #354

cepbuch opened this issue Nov 7, 2019 · 3 comments

Comments

@cepbuch
Copy link

cepbuch commented Nov 7, 2019

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

@alejcas
Copy link
Member

alejcas commented Nov 7, 2019

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!!

@cepbuch
Copy link
Author

cepbuch commented Nov 7, 2019

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

@kwilsonmg
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants