Skip to content

Commit

Permalink
chore: Move common code up
Browse files Browse the repository at this point in the history
  • Loading branch information
ecederstrand committed Apr 23, 2024
1 parent d9035d0 commit 661b809
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions exchangelib/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down

0 comments on commit 661b809

Please sign in to comment.