diff --git a/exchangelib/fields.py b/exchangelib/fields.py index c5dcb912..4a93f8aa 100644 --- a/exchangelib/fields.py +++ b/exchangelib/fields.py @@ -1330,6 +1330,9 @@ def response_tag(self): class IndexedField(EWSElementField, metaclass=abc.ABCMeta): """A base class for all indexed fields.""" + is_list = True + is_complex = True + PARENT_ELEMENT_NAME = None def __init__(self, *args, **kwargs): @@ -1346,41 +1349,36 @@ def to_xml(self, value, version): def response_tag(self): return f"{{{self.namespace}}}{self.PARENT_ELEMENT_NAME}" - def __hash__(self): - return hash(self.field_uri) - - -class EmailAddressesField(IndexedField): - is_list = True - is_complex = True - - PARENT_ELEMENT_NAME = "EmailAddresses" - - def __init__(self, *args, **kwargs): - from .indexed_properties import EmailAddress - - kwargs["value_cls"] = EmailAddress - super().__init__(*args, **kwargs) - def clean(self, value, version=None): if value is not None: default_labels = self.value_cls.LABEL_CHOICES 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] for s, default_label in zip(value, default_labels): if not isinstance(s, str): tmp.append(s) continue - tmp.append(self.value_cls(email=s, label=default_label)) + tmp.append(self.value_cls(**dict(zip(value_cls_fields, (default_label, s))))) value = tmp return super().clean(value, version=version) + def __hash__(self): + return hash(self.field_uri) -class PhoneNumberField(IndexedField): - is_list = True - is_complex = True +class EmailAddressesField(IndexedField): + PARENT_ELEMENT_NAME = "EmailAddresses" + + def __init__(self, *args, **kwargs): + from .indexed_properties import EmailAddress + + kwargs["value_cls"] = EmailAddress + super().__init__(*args, **kwargs) + + +class PhoneNumberField(IndexedField): PARENT_ELEMENT_NAME = "PhoneNumbers" def __init__(self, *args, **kwargs):