Skip to content

Commit

Permalink
feat: Simplify move/copy to folder inbox rul action creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ecederstrand committed Mar 26, 2024
1 parent a39ed40 commit b47983a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
5 changes: 4 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1943,7 +1943,10 @@ rule = Rule(
is_enabled=True,
conditions=Conditions(contains_sender_strings=["sender_example"]),
exceptions=Exceptions(),
actions=Actions(delete=True),
actions=Actions(
move_to_folder=a.trash,
mark_as_read=True,
),
)

# Create rule
Expand Down
16 changes: 16 additions & 0 deletions exchangelib/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -1798,3 +1798,19 @@ class SensitivityField(ChoiceField):
def __init__(self, *args, **kwargs):
kwargs["choices"] = SENSITIVITY_CHOICES
super().__init__(*args, **kwargs)


class FolderActionField(EWSElementField):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def clean(self, value, version=None):
from .folders import DistinguishedFolderId, Folder

if isinstance(value, Folder):
folder_id = value.to_id()
if isinstance(folder_id, DistinguishedFolderId):
value = self.value_cls(distinguished_folder_id=folder_id)
else:
value = self.value_cls(folder_id=folder_id)
return super().clean(value, version=version)
5 changes: 3 additions & 2 deletions exchangelib/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
Field,
FieldPath,
FlaggedForActionField,
FolderActionField,
FreeBusyStatusField,
GenericEventListField,
IdElementField,
Expand Down Expand Up @@ -2244,13 +2245,13 @@ class Actions(EWSElement):
ELEMENT_NAME = "Actions"

assign_categories = CharListField(field_uri="AssignCategories")
copy_to_folder = EWSElementField(value_cls=CopyToFolder)
copy_to_folder = FolderActionField(value_cls=CopyToFolder)
delete = BooleanField(field_uri="Delete")
forward_as_attachment_to_recipients = AddressListField(field_uri="ForwardAsAttachmentToRecipients")
forward_to_recipients = AddressListField(field_uri="ForwardToRecipients")
mark_importance = ImportanceField(field_uri="MarkImportance")
mark_as_read = BooleanField(field_uri="MarkAsRead")
move_to_folder = EWSElementField(value_cls=MoveToFolder)
move_to_folder = FolderActionField(value_cls=MoveToFolder)
permanent_delete = BooleanField(field_uri="PermanentDelete")
redirect_to_recipients = AddressListField(field_uri="RedirectToRecipients")
send_sms_alert_to_recipients = AddressListField(field_uri="SendSMSAlertToRecipients")
Expand Down
3 changes: 1 addition & 2 deletions tests/test_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
Actions,
Address,
Conditions,
CopyToFolder,
DelegatePermissions,
DelegateUser,
Exceptions,
Expand Down Expand Up @@ -375,7 +374,7 @@ def test_basic_inbox_rule(self):
def test_all_inbox_rule_actions(self):
for action_name, action in {
"assign_categories": ["foo", "bar"],
"copy_to_folder": CopyToFolder(distinguished_folder_id=self.account.trash.to_id()),
"copy_to_folder": self.account.trash,
"delete": True, # Cannot be random. False would be a no-op action
"forward_as_attachment_to_recipients": [Address(email_address=get_random_email())],
"mark_importance": get_random_choice(
Expand Down

0 comments on commit b47983a

Please sign in to comment.