Skip to content

Commit

Permalink
chore: Simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Cederstrand committed Apr 24, 2024
1 parent 54390d0 commit 5144b88
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
4 changes: 2 additions & 2 deletions exchangelib/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -1355,12 +1355,12 @@ def clean(self, value, version=None):
if len(value) > len(default_labels):
raise ValueError(f"This field can handle at most {len(default_labels)} values (value: {value})")
tmp = []
value_cls_fields = [f.name for f in self.value_cls.FIELDS]
value_field_name = self.value_cls.value_field(version=version).name
for s, default_label in zip(value, default_labels):
if not isinstance(s, str):
tmp.append(s)
continue
tmp.append(self.value_cls(**dict(zip(value_cls_fields, (default_label, s)))))
tmp.append(self.value_cls(**{"label": default_label, value_field_name: s})
value = tmp
return super().clean(value, version=version)

Expand Down
18 changes: 5 additions & 13 deletions 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, ImAddress, PhoneNumber
from .indexed_properties import SingleFieldIndexedElement
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 @@ -200,23 +200,15 @@ def value_to_xml_text(value):
return value.isoformat()
if isinstance(value, EWSTimeZone):
return value.ms_id
if isinstance(value, EWSDateTime):
if isinstance(value, (EWSDate, EWSDateTime)):
return value.ewsformat()
if isinstance(value, EWSDate):
return value.ewsformat()
if isinstance(value, PhoneNumber):
return value.phone_number
if isinstance(value, EmailAddress):
return value.email
if isinstance(value, ImAddress):
return value.im_address
if isinstance(value, SingleFieldIndexedElement):
return getattr(value, value.value_field(version=None).name)
if isinstance(value, Mailbox):
return value.email_address
if isinstance(value, Attendee):
return value.mailbox.email_address
if isinstance(value, ConversationId):
return value.id
if isinstance(value, AssociatedCalendarItemId):
if isinstance(value, (ConversationId, AssociatedCalendarItemId)):
return value.id
raise TypeError(f"Unsupported type: {type(value)} ({value})")

Expand Down

0 comments on commit 5144b88

Please sign in to comment.