Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
raise ValueError('Unsupported field %s' % field)
if isinstance(field, URIField):
return get_random_url()
if isinstance(field, EmailAddressField):
return get_random_email()
if isinstance(field, ChoiceField):
return get_random_choice(field.supported_choices(version=self.account.version))
if isinstance(field, CultureField):
return get_random_choice(['da-DK', 'de-DE', 'en-US', 'es-ES', 'fr-CA', 'nl-NL', 'ru-RU', 'sv-SE'])
if isinstance(field, BodyField):
return get_random_string(400)
if isinstance(field, CharListField):
return [get_random_string(16) for _ in range(random.randint(1, 4))]
if isinstance(field, TextListField):
return [get_random_string(400) for _ in range(random.randint(1, 4))]
if isinstance(field, CharField):
return get_random_string(field.max_length)
if isinstance(field, TextField):
return get_random_string(400)
if isinstance(field, MimeContentField):
return get_random_string(400)
if isinstance(field, Base64Field):
return get_random_bytes(400)
if isinstance(field, BooleanField):
return get_random_bool()
if isinstance(field, DecimalField):
return get_random_decimal(field.min or 1, field.max or 99)
if isinstance(field, IntegerField):
return get_random_int(field.min or 0, field.max or 256)
if isinstance(field, DateTimeField):
return get_random_datetime(tz=self.account.default_timezone)
if isinstance(field, AttachmentField):
MessageHeaderField('headers', field_uri='item:InternetMessageHeaders', is_read_only=True),
DateTimeField('datetime_sent', field_uri='item:DateTimeSent', is_read_only=True),
DateTimeField('datetime_created', field_uri='item:DateTimeCreated', is_read_only=True),
# Placeholder for ResponseObjects
DateTimeField('reminder_due_by', field_uri='item:ReminderDueBy', is_required_after_save=True,
is_searchable=False),
BooleanField('reminder_is_set', field_uri='item:ReminderIsSet', is_required=True, default=False),
IntegerField('reminder_minutes_before_start', field_uri='item:ReminderMinutesBeforeStart',
is_required_after_save=True, min=0, default=0),
CharField('display_cc', field_uri='item:DisplayCc', is_read_only=True),
CharField('display_to', field_uri='item:DisplayTo', is_read_only=True),
BooleanField('has_attachments', field_uri='item:HasAttachments', is_read_only=True),
# ExtendedProperty fields go here
CultureField('culture', field_uri='item:Culture', is_required_after_save=True, is_searchable=False),
EffectiveRightsField('effective_rights', field_uri='item:EffectiveRights', is_read_only=True),
CharField('last_modified_name', field_uri='item:LastModifiedName', is_read_only=True),
DateTimeField('last_modified_time', field_uri='item:LastModifiedTime', is_read_only=True),
BooleanField('is_associated', field_uri='item:IsAssociated', is_read_only=True, supported_from=EXCHANGE_2010),
URIField('web_client_read_form_query_string', field_uri='item:WebClientReadFormQueryString',
is_read_only=True, supported_from=EXCHANGE_2010),
URIField('web_client_edit_form_query_string', field_uri='item:WebClientEditFormQueryString',
is_read_only=True, supported_from=EXCHANGE_2010),
EWSElementField('conversation_id', field_uri='item:ConversationId', value_cls=ConversationId,
is_read_only=True, supported_from=EXCHANGE_2010),
BodyField('unique_body', field_uri='item:UniqueBody', is_read_only=True, supported_from=EXCHANGE_2010),
]
FIELDS = LOCAL_FIELDS[0:1] + BaseItem.FIELDS + LOCAL_FIELDS[1:]
__slots__ = tuple(f.name for f in LOCAL_FIELDS)
# Used to register extended properties
def from_xml(self, elem, account):
iter_elem = elem.find(self.response_tag())
if iter_elem is not None:
return get_xml_attrs(iter_elem, self.list_elem_tag())
return self.default
class URIField(TextField):
"""Helper to mark strings that must conform to xsd:anyURI
If we want an URI validator, see http://stackoverflow.com/questions/14466585/is-this-regex-correct-for-xsdanyuri
"""
pass
class EmailAddressField(CharField):
"""A helper class used for email address string that we can use for email validation"""
pass
class CultureField(CharField):
"""Helper to mark strings that are # RFC 1766 culture values."""
pass
class Choice(object):
"""Implements versioned choices for the ChoiceField field"""
def __init__(self, value, supported_from=None):
self.value = value
self.supported_from = supported_from
def supports_version(self, version):
return value
class IdField(CharField):
"""A field to hold the 'Id' and 'Changekey' attributes on 'ItemId' type items. There is no guaranteed max length,
but we can assume 512 bytes in practice. See
https://docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/ews-identifiers-in-exchange
"""
def __init__(self, *args, **kwargs):
super(IdField, self).__init__(*args, **kwargs)
self.max_length = 512 # This is above the normal 255 limit, but this is actually an attribute, not a field
self.is_searchable = False
self.is_attribute = True
class CharListField(CharField):
is_list = True
def list_elem_tag(self):
return '{%s}String' % self.namespace
def from_xml(self, elem, account):
iter_elem = elem.find(self.response_tag())
if iter_elem is not None:
return get_xml_attrs(iter_elem, self.list_elem_tag())
return self.default
class URIField(TextField):
"""Helper to mark strings that must conform to xsd:anyURI
If we want an URI validator, see http://stackoverflow.com/questions/14466585/is-this-regex-correct-for-xsdanyuri
"""
"""
ELEMENT_NAME = 'Message'
LOCAL_FIELDS = [
MailboxField('sender', field_uri='message:Sender', is_read_only=True, is_read_only_after_send=True),
MailboxListField('to_recipients', field_uri='message:ToRecipients', is_read_only_after_send=True,
is_searchable=False),
MailboxListField('cc_recipients', field_uri='message:CcRecipients', is_read_only_after_send=True,
is_searchable=False),
MailboxListField('bcc_recipients', field_uri='message:BccRecipients', is_read_only_after_send=True,
is_searchable=False),
BooleanField('is_read_receipt_requested', field_uri='message:IsReadReceiptRequested',
is_required=True, default=False, is_read_only_after_send=True),
BooleanField('is_delivery_receipt_requested', field_uri='message:IsDeliveryReceiptRequested',
is_required=True, default=False, is_read_only_after_send=True),
Base64Field('conversation_index', field_uri='message:ConversationIndex', is_read_only=True),
CharField('conversation_topic', field_uri='message:ConversationTopic', is_read_only=True),
# Rename 'From' to 'author'. We can't use fieldname 'from' since it's a Python keyword.
MailboxField('author', field_uri='message:From', is_read_only_after_send=True),
CharField('message_id', field_uri='message:InternetMessageId', is_read_only_after_send=True),
BooleanField('is_read', field_uri='message:IsRead', is_required=True, default=False),
BooleanField('is_response_requested', field_uri='message:IsResponseRequested', default=False, is_required=True),
TextField('references', field_uri='message:References'),
MailboxListField('reply_to', field_uri='message:ReplyTo', is_read_only_after_send=True, is_searchable=False),
MailboxField('received_by', field_uri='message:ReceivedBy', is_read_only=True),
MailboxField('received_representing', field_uri='message:ReceivedRepresenting', is_read_only=True),
# Placeholder for ReminderMessageData
]
FIELDS = Item.FIELDS + LOCAL_FIELDS
__slots__ = tuple(f.name for f in LOCAL_FIELDS)
def send(self, save_copy=True, copy_to_folder=None, conflict_resolution=AUTO_RESOLVE,
SOFT_DELETE = 'SoftDelete'
MOVE_TO_DELETED_ITEMS = 'MoveToDeletedItems'
DELETE_TYPE_CHOICES = (HARD_DELETE, SOFT_DELETE, MOVE_TO_DELETED_ITEMS)
class Item(BaseItem):
"""
MSDN: https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/item
"""
ELEMENT_NAME = 'Item'
LOCAL_FIELDS = [
MimeContentField('mime_content', field_uri='item:MimeContent', is_read_only_after_send=True),
EWSElementField('parent_folder_id', field_uri='item:ParentFolderId', value_cls=ParentFolderId,
is_read_only=True),
CharField('item_class', field_uri='item:ItemClass', is_read_only=True),
CharField('subject', field_uri='item:Subject'),
ChoiceField('sensitivity', field_uri='item:Sensitivity', choices={
Choice('Normal'), Choice('Personal'), Choice('Private'), Choice('Confidential')
}, is_required=True, default='Normal'),
TextField('text_body', field_uri='item:TextBody', is_read_only=True, supported_from=EXCHANGE_2013),
BodyField('body', field_uri='item:Body'), # Accepts and returns Body or HTMLBody instances
AttachmentField('attachments', field_uri='item:Attachments'), # ItemAttachment or FileAttachment
DateTimeField('datetime_received', field_uri='item:DateTimeReceived', is_read_only=True),
IntegerField('size', field_uri='item:Size', is_read_only=True), # Item size in bytes
CharListField('categories', field_uri='item:Categories'),
ChoiceField('importance', field_uri='item:Importance', choices={
Choice('Low'), Choice('Normal'), Choice('High')
}, is_required=True, default='Normal'),
TextField('in_reply_to', field_uri='item:InReplyTo'),
BooleanField('is_submitted', field_uri='item:IsSubmitted', is_read_only=True),
BooleanField('is_draft', field_uri='item:IsDraft', is_read_only=True),
choices={Choice(c) for c in ID_FORMATS}),
]
__slots__ = tuple(f.name for f in FIELDS)
class AlternatePublicFolderItemId(EWSElement):
"""MSDN:
https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/alternatepublicfolderitemid
"""
ELEMENT_NAME = 'AlternatePublicFolderItemId'
FIELDS = [
CharField('folder_id', field_uri='FolderId', is_required=True, is_attribute=True),
ChoiceField('format', field_uri='Format', is_required=True, is_attribute=True,
choices={Choice(c) for c in ID_FORMATS}),
CharField('item_id', field_uri='ItemId', is_required=True, is_attribute=True),
]
__slots__ = tuple(f.name for f in FIELDS)
class IdChangeKeyMixIn(EWSElement):
"""Base class for classes that have 'id' and 'changekey' fields which are actually attributes on ID element"""
ID_ELEMENT_CLS = ItemId
FIELDS = [
IdField('id', field_uri=ID_ELEMENT_CLS.ID_ATTR, is_read_only=True),
IdField('changekey', field_uri=ID_ELEMENT_CLS.CHANGEKEY_ATTR, is_read_only=True),
]
__slots__ = tuple(f.name for f in FIELDS)
super(CharField, self).__init__(*args, **kwargs)
def clean(self, value, version=None):
value = super(CharField, self).clean(value, version=version)
if value is not None:
if self.is_list:
for v in value:
if len(v) > self.max_length:
raise ValueError("'%s' value '%s' exceeds length %s" % (self.name, v, self.max_length))
else:
if len(value) > self.max_length:
raise ValueError("'%s' value '%s' exceeds length %s" % (self.name, value, self.max_length))
return value
class IdField(CharField):
"""A field to hold the 'Id' and 'Changekey' attributes on 'ItemId' type items. There is no guaranteed max length,
but we can assume 512 bytes in practice. See
https://docs.microsoft.com/en-us/exchange/client-developer/exchange-web-services/ews-identifiers-in-exchange
"""
def __init__(self, *args, **kwargs):
super(IdField, self).__init__(*args, **kwargs)
self.max_length = 512 # This is above the normal 255 limit, but this is actually an attribute, not a field
self.is_searchable = False
self.is_attribute = True
class CharListField(CharField):
is_list = True
def list_elem_tag(self):
return '{%s}String' % self.namespace
TextField('directory_id', field_uri='contacts:DirectoryId', supported_from=EXCHANGE_2013, is_read_only=True),
# Placeholder for ManagerMailbox
# Placeholder for DirectReports
]
FIELDS = Item.FIELDS + LOCAL_FIELDS
__slots__ = tuple(f.name for f in LOCAL_FIELDS)
class DistributionList(Item):
"""
MSDN: https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/distributionlist
"""
ELEMENT_NAME = 'DistributionList'
LOCAL_FIELDS = [
CharField('display_name', field_uri='contacts:DisplayName', is_required=True),
CharField('file_as', field_uri='contacts:FileAs', is_read_only=True),
ChoiceField('contact_source', field_uri='contacts:ContactSource', choices={
Choice('Store'), Choice('ActiveDirectory')
}, is_read_only=True),
MemberListField('members', field_uri='distributionlist:Members'),
]
FIELDS = Item.FIELDS + LOCAL_FIELDS
__slots__ = tuple(f.name for f in LOCAL_FIELDS)
class PostItem(Item):
"""
MSDN: https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/postitem
"""
ELEMENT_NAME = 'PostItem'
}, default='None', is_required_after_save=True),
TextField('profession', field_uri='contacts:Profession'),
TextField('spouse_name', field_uri='contacts:SpouseName'),
CharField('surname', field_uri='contacts:Surname'),
DateTimeField('wedding_anniversary', field_uri='contacts:WeddingAnniversary'),
BooleanField('has_picture', field_uri='contacts:HasPicture', supported_from=EXCHANGE_2010, is_read_only=True),
TextField('phonetic_full_name', field_uri='contacts:PhoneticFullName', supported_from=EXCHANGE_2013,
is_read_only=True),
TextField('phonetic_first_name', field_uri='contacts:PhoneticFirstName', supported_from=EXCHANGE_2013,
is_read_only=True),
TextField('phonetic_last_name', field_uri='contacts:PhoneticLastName', supported_from=EXCHANGE_2013,
is_read_only=True),
EmailAddressField('email_alias', field_uri='contacts:Alias', is_read_only=True),
# 'notes' is documented in MSDN but apparently unused. Writing to it raises ErrorInvalidPropertyRequest. OWA
# put entries into the 'notes' form field into the 'body' field.
CharField('notes', field_uri='contacts:Notes', supported_from=EXCHANGE_2013, is_read_only=True),
# 'photo' is documented in MSDN but apparently unused. Writing to it raises ErrorInvalidPropertyRequest. OWA
# adds photos as FileAttachments on the contact item (with 'is_contact_photo=True'), which automatically flips
# the 'has_picture' field.
Base64Field('photo', field_uri='contacts:Photo', is_read_only=True),
# Placeholder for UserSMIMECertificate
# Placeholder for MSExchangeCertificate
TextField('directory_id', field_uri='contacts:DirectoryId', supported_from=EXCHANGE_2013, is_read_only=True),
# Placeholder for ManagerMailbox
# Placeholder for DirectReports
]
FIELDS = Item.FIELDS + LOCAL_FIELDS
__slots__ = tuple(f.name for f in LOCAL_FIELDS)
class DistributionList(Item):