Skip to content

Commit

Permalink
feat: make Contact.im_addresses writable
Browse files Browse the repository at this point in the history
  • Loading branch information
ecederstrand committed Apr 23, 2024
1 parent 6330c4c commit 54390d0
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
10 changes: 10 additions & 0 deletions exchangelib/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,16 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)


class ImAddressField(IndexedField):
PARENT_ELEMENT_NAME = "ImAddresses"

def __init__(self, *args, **kwargs):
from .indexed_properties import ImAddress

kwargs["value_cls"] = ImAddress
super().__init__(*args, **kwargs)


class PhoneNumberField(IndexedField):
PARENT_ELEMENT_NAME = "PhoneNumbers"

Expand Down
10 changes: 10 additions & 0 deletions exchangelib/indexed_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ class EmailAddress(SingleFieldIndexedElement):
email = EmailSubField(is_required=True)


class ImAddress(SingleFieldIndexedElement):
"""MSDN: https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/entry-imaddress"""

ELEMENT_NAME = "Entry"
LABEL_CHOICES = ("ImAddress1", "ImAddress2", "ImAddress3")

label = LabelField(field_uri="Key", choices={Choice(c) for c in LABEL_CHOICES}, default=LABEL_CHOICES[0])
im_address = SubField(is_required=True)


class PhoneNumber(SingleFieldIndexedElement):
"""MSDN: https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/entry-phonenumber"""

Expand Down
3 changes: 2 additions & 1 deletion exchangelib/items/contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
EWSElementField,
EWSElementListField,
IdElementField,
ImAddressField,
MailboxField,
MailboxListField,
MemberListField,
Expand Down Expand Up @@ -86,7 +87,7 @@ class Contact(Item):
)
department = TextField(field_uri="contacts:Department")
generation = TextField(field_uri="contacts:Generation")
im_addresses = CharField(field_uri="contacts:ImAddresses", is_read_only=True)
im_addresses = ImAddressField(field_uri="contacts:ImAddress")
job_title = TextField(field_uri="contacts:JobTitle")
manager = TextField(field_uri="contacts:Manager")
mileage = TextField(field_uri="contacts:Mileage")
Expand Down
4 changes: 3 additions & 1 deletion exchangelib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def get_xml_attrs(tree, name):

def value_to_xml_text(value):
from .ewsdatetime import EWSDate, EWSDateTime, EWSTimeZone
from .indexed_properties import EmailAddress, PhoneNumber
from .indexed_properties import EmailAddress, ImAddress, PhoneNumber
from .properties import AssociatedCalendarItemId, Attendee, ConversationId, Mailbox

# We can't just create a map and look up with type(value) because we want to support subtypes
Expand All @@ -208,6 +208,8 @@ def value_to_xml_text(value):
return value.phone_number
if isinstance(value, EmailAddress):
return value.email
if isinstance(value, ImAddress):
return value.im_address
if isinstance(value, Mailbox):
return value.email_address
if isinstance(value, Attendee):
Expand Down
8 changes: 7 additions & 1 deletion tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
EmailAddressField,
EWSElementField,
ExtendedPropertyField,
ImAddressField,
IntegerField,
MailboxField,
MailboxListField,
Expand All @@ -52,7 +53,7 @@
TimeZoneField,
URIField,
)
from exchangelib.indexed_properties import EmailAddress, PhoneNumber, PhysicalAddress
from exchangelib.indexed_properties import EmailAddress, ImAddress, PhoneNumber, PhysicalAddress
from exchangelib.properties import (
Attendee,
CompleteName,
Expand Down Expand Up @@ -247,6 +248,11 @@ def random_val(self, field):
for label in EmailAddress.get_field_by_fieldname("label").supported_choices(version=self.account.version):
addrs.append(EmailAddress(email=get_random_email(), label=label))
return addrs
if isinstance(field, ImAddressField):
addrs = []
for label in ImAddress.get_field_by_fieldname("label").supported_choices(version=self.account.version):
addrs.append(ImAddress(im_address=get_random_email(), label=label))
return addrs
if isinstance(field, PhysicalAddressField):
addrs = []
for label in PhysicalAddress.get_field_by_fieldname("label").supported_choices(
Expand Down

0 comments on commit 54390d0

Please sign in to comment.