
    3fiI                         d dl Zd dlZd dlmZ d dlmZ ddlmZ ddl	m
Z
mZ ddlmZmZmZmZmZmZ  ej&                  e      Z G d d	ee      Z G d
 de      Z G d de      Z G d de      Zy)    N)parse)	HTTPError   )Category)MessageRecipientType)NEXT_LINK_KEYWORDApiComponentAttachableMixin
Pagination
Recipients
TrackerSetc                       e Zd ZdZddddddZeZddd	 fd

Zd Zd Z	d Z
ed        Zed        Zed        Zej                  d        Zed        Zej                  d        Zed        Zej                  d        Zed        Zej                  d        Zed        Zed        Zej                  d        Zed        Zej                  d        Zed        Zej                  d        Zed        Zej                  d         Zed!        Zej                  d"        Zed#        Zej                  d$        Zed%        Zej                  d&        Zed'        Zej                  d(        Zed)        Zed*        Zed+        Zej                  d,        Zed-        Zej                  d.        Zed/        Z e j                  d0        Z ed1        Z!e!j                  d2        Z!ed3        Z"e"j                  d4        Z"ed5        Z#e#j                  d6        Z#ed7        Z$d?d8Z%d9 Z&d: Z'd?e(jR                  d;d<Z*d?d=Z+d> Z, xZ-S )@ContactzI Contact manages lists of events on associated contact on Microsoft 365. 	/contactsz/contacts/{id}z$/contactFolders/{folder_id}/contactsz/contacts/{id}/photo/$valuez#/contacts/{id}/photos/{size}/$value)contactroot_contactchild_contactphoto
photo_sizeNparentconc                2
   |r|rt        d      |r|j                  n|| _        |j                  dd      xs |rt        |dd      nd}t        |   |r|j                  n|j                  d      |       |j                  | j                  i       }| j                  }t        |      | _        |j                   |d      d      | _        |j                   |d      d      | _        |j                   |d	      d      | _        | j                  j                  }| j                  r$t!        | j                        j#                  |      nd| _        | j                  r$t!        | j                        j#                  |      nd| _        |j                   |d
      d      | _        |j                   |d      d      | _        |j                   |d      d      | _        |j                   |d      d      | _        |j                   |d      d      | _        |j                   |d      d      | _        |j                   |d      d      | _        |j                   |d      d      | _        |j                   |d      d      | _        |j                   |d      g       xs g | _        |j                   |d      d      | _        |j                   |d      g       xs g | _        |j                   |d      g       }t=        |D 	cg c]2  }	|	j                   |d      d      |	j                   |d      d      f4 c}	|  |d            | _        |j                   |d            }
d| j>                  _         |
r)|
| j>                  vr| j>                  jC                  |
       |j                   |d      i       | _"        |j                   |d      i       | _#        |j                   |d      i       | _$        |j                   |d       d      | _%        |j                   |d!      g       | _&        |j                   |d"      d      | _'        |j                   |d#      d      | _(        |j                   |d$      d      }|j                   |d%      d      }|r)|| jR                  vr| jR                  jC                  |       |r)|| jR                  vr| jR                  jC                  |       d&| j>                  _         yc c}	w )'a   Create a contact API component

        :param parent: parent account for this folder
        :type parent: Account
        :param Connection con: connection to use if no parent specified
        :param Protocol protocol: protocol to use if no parent specified
         (kwargs)
        :param str main_resource: use this resource instead of parent resource
         (kwargs)
        *Need a parent or a connection but not bothmain_resourceNprotocolr   r   )casingidcreatedDateTimelastModifiedDateTimedisplayName fileAs	givenNamesurnametitlejobTitlecompanyName
departmentofficeLocationbusinessPhonesmobilePhone
homePhonesemailAddressesnameaddress)
recipientsr   fieldemailTbusinessAddresshomeAddressotherAddresspreferredLanguage
categoriesparentFolderIdpersonalNotesmailuserPrincipalNameF)*
ValueErrorr   popgetattrsuper__init__r   get_cloud_data_key_ccr   _track_changes	object_id_Contact__created_Contact__modifiedtimezoner   
astimezone_Contact__display_name_Contact__fileAs_Contact__name_Contact__surname_Contact__title_Contact__job_title_Contact__company_name_Contact__department_Contact__office_location_Contact__business_phones_Contact__mobile_phone_Contact__home_phonesr   _Contact__emailsuntrackadd_Contact__business_address_Contact__home_address_Contact__other_address_Contact__preferred_language_Contact__categories_Contact__folder_id_Contact__personal_notesemails)selfr   r   kwargsr   
cloud_datacclocal_tzrc   rcpr5   r=   user_principal_name	__class__s                O/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/O365/address_book.pyrC   zContact.__init__"   sU    cIJJ!'6::S 

?D9 H6<GFOT2$ 	 	(.V__FJJz4J' 	 	) ZZ 4 4b9
XX )3 $4$7#+<(=tD$..,B)CTJ==)) t~~.99-1 	   0;;.2 	 )nnR->C"r(|R8 nnR_b9#9r:!~~bk26%>>"Z."=(nnR->C&NN2l+;R@!+3C0Db!I!+3C0Db!I!OR(nnR->C'^^B|,<bAGR#3 4b9"#)+ FR0#''"Y-2LM +r"235 r'{+ $U$--/MMe$",..4E1F"K(nnR->C)~~b.@"E$.NN26I3J37%9! 'NN2l+;R@%>>"-=*>E *r//BB G ~~bj$/(nnR0C-DdKD+KKOOD!#6dkk#IKKOO/0 %7+s   7Tc                 "    | j                         S N__repr__rd   s    rl   __str__zContact.__str__w       }}    c                 >    | j                   xs | j                  xs dS )NzUnknown Name)display_name	full_namerq   s    rl   rp   zContact.__repr__z   s      DDNNDnDrt   c                 4    | j                   |j                   k(  S rn   )rH   rd   others     rl   __eq__zContact.__eq__}       ~~00rt   c                     | j                   S )z0 Created Time

        :rtype: datetime
        )rI   rq   s    rl   createdzContact.created   s     ~~rt   c                     | j                   S )z6 Last Modified Time

        :rtype: datetime
        )rJ   rq   s    rl   modifiedzContact.modified   s     rt   c                     | j                   S )z Display Name

        :getter: Get the display name of the contact
        :setter: Update the display name
        :type: str
        )rM   rq   s    rl   rv   zContact.display_name        """rt   c                 f    || _         | j                  j                  | j                  d             y )Nr#   )rM   rG   r[   rF   rd   values     rl   rv   zContact.display_name   '    # 78rt   c                     | j                   S )zw File As

        :getter: Get the fileAs of the contact
        :setter: Update the fileAs
        :type: str
        )rN   rq   s    rl   r%   zContact.fileAs   s     }}rt   c                 f    || _         | j                  j                  | j                  d             y )Nr%   )rN   rG   r[   rF   r   s     rl   r%   zContact.fileAs   s&     23rt   c                     | j                   S )zv First Name

        :getter: Get the name of the contact
        :setter: Update the name
        :type: str
        )rO   rq   s    rl   r1   zContact.name   s     {{rt   c                 f    || _         | j                  j                  | j                  d             y )Nr&   )rO   rG   r[   rF   r   s     rl   r1   zContact.name   s&     56rt   c                     | j                   S )z Surname of Contact

        :getter: Get the surname of the contact
        :setter: Update the surname
        :type: str
        )rP   rq   s    rl   r'   zContact.surname   s     ~~rt   c                 f    || _         | j                  j                  | j                  d             y )Nr'   )rP   rG   r[   rF   r   s     rl   r'   zContact.surname   s&     34rt   c                 j    dj                  | j                  | j                        j                         S )z9 Full Name (Name + Surname)

        :rtype: str
        z{} {})formatr1   r'   striprq   s    rl   rw   zContact.full_name   s&     ~~dii6<<>>rt   c                     | j                   S )z Title (Mr., Ms., etc..)

        :getter: Get the title of the contact
        :setter: Update the title
        :type: str
        )rQ   rq   s    rl   r(   zContact.title   s     ||rt   c                 f    || _         | j                  j                  | j                  d             y )Nr(   )rQ   rG   r[   rF   r   s     rl   r(   zContact.title   s&     12rt   c                     | j                   S )z{ Job Title

        :getter: Get the job title of contact
        :setter: Update the job title
        :type: str
        )rR   rq   s    rl   	job_titlezContact.job_title   s     rt   c                 f    || _         | j                  j                  | j                  d             y )Nr)   )rR   rG   r[   rF   r   s     rl   r   zContact.job_title   s'      45rt   c                     | j                   S )z Name of the company

        :getter: Get the company name of contact
        :setter: Update the company name
        :type: str
        )rS   rq   s    rl   company_namezContact.company_name   r   rt   c                 f    || _         | j                  j                  | j                  d             y )Nr*   )rS   rG   r[   rF   r   s     rl   r   zContact.company_name   r   rt   c                     | j                   S )z~ Department

        :getter: Get the department of contact
        :setter: Update the department
        :type: str
        )rT   rq   s    rl   r+   zContact.department          rt   c                 f    || _         | j                  j                  | j                  d             y )Nr+   )rT   rG   r[   rF   r   s     rl   r+   zContact.department  s'    ! 67rt   c                     | j                   S )z Office Location

        :getter: Get the office location of contact
        :setter: Update the office location
        :type: str
        )rU   rq   s    rl   office_locationzContact.office_location       %%%rt   c                 f    || _         | j                  j                  | j                  d             y )Nr,   )rU   rG   r[   rF   r   s     rl   r   zContact.office_location  s(    !&)9 :;rt   c                     | j                   S )z Business Contact numbers

        :getter: Get the contact numbers of contact
        :setter: Update the contact numbers
        :type: list[str]
        )rV   rq   s    rl   business_phoneszContact.business_phones  r   rt   c                     t        |t              rt        |      }t        |t              s|g}|| _        | j                  j                  | j                  d             y )Nr-   )
isinstancetuplelistrV   rG   r[   rF   r   s     rl   r   zContact.business_phones)  sL    eU#KE%&GE!&)9 :;rt   c                     | j                   S )z Personal Contact numbers

        :getter: Get the contact numbers of contact
        :setter: Update the contact numbers
        :type: list[str]
        )rW   rq   s    rl   mobile_phonezContact.mobile_phone2  r   rt   c                 f    || _         | j                  j                  | j                  d             y )Nr.   )rW   rG   r[   rF   r   s     rl   r   zContact.mobile_phone<  r   rt   c                     | j                   S )z Home Contact numbers

        :getter: Get the contact numbers of contact
        :setter: Update the contact numbers
        :type: list[str]
        )rX   rq   s    rl   home_phoneszContact.home_phonesA  s     !!!rt   c                    t        |t              r|| _        nEt        |t              r	|g| _        n,t        |t              rt        |      | _        nt        d      | j                  j                  | j                  d             y )Nzhome_phones must be a listr/   )	r   r   rX   strr   r?   rG   r[   rF   r   s     rl   r   zContact.home_phonesK  sg    eT"!&Ds#"'Du%!%eD9:: 67rt   c                     | j                   S )zF List of email ids of the Contact

        :rtype: Recipients
        )rY   rq   s    rl   rc   zContact.emailsW  s     }}rt   c                 N    | j                   sy| j                   d   j                  S )zE Primary(First) email id of the Contact

        :rtype: str
        Nr   )rc   r2   rq   s    rl   
main_emailzContact.main_email_  s"     {{{{1~%%%rt   c                     | j                   S )z Business Address

        :getter: Get the address of contact
        :setter: Update the address
        :type: dict
        )r\   rq   s    rl   business_addresszContact.business_addressi  s     &&&rt   c                     t        |t              st        d      || _        | j                  j                  | j                  d             y )Nz"business_address" must be dictr6   )r   dictr?   r\   rG   r[   rF   r   s     rl   r   zContact.business_addresss  s>    %&>??"'): ;<rt   c                     | j                   S )z{ Home Address

        :getter: Get the address of contact
        :setter: Update the address
        :type: dict
        )r]   rq   s    rl   home_addresszContact.home_addressz  r   rt   c                     t        |t              st        d      || _        | j                  j                  | j                  d             y )Nz"home_address" must be dictr7   )r   r   r?   r]   rG   r[   rF   r   s     rl   r   zContact.home_address  s=    %&:;;# 78rt   c                     | j                   S )z| Other Address

        :getter: Get the address of contact
        :setter: Update the address
        :type: dict
        )r^   rq   s    rl   other_addresszContact.other_address  s     ###rt   c                     t        |t              st        d      || _        | j                  j                  | j                  d             y )Nz"other_address" must be dictr8   )r   r   r?   r^   rG   r[   rF   r   s     rl   r   zContact.other_address  s=    %&;<<$ 89rt   c                     | j                   S )z Preferred Language

        :getter: Get the language of contact
        :setter: Update the language
        :type: str
        )r_   rq   s    rl   preferred_languagezContact.preferred_language  s     (((rt   c                 f    || _         | j                  j                  | j                  d             y )Nr9   )r_   rG   r[   rF   r   s     rl   r   zContact.preferred_language  s(    $)!)< =>rt   c                     | j                   S )z Assigned Categories

        :getter: Get the categories
        :setter: Update the categories
        :type: list[str]
        )r`   rq   s    rl   r:   zContact.categories  r   rt   c                    t        |t              r`g | _        |D ]S  }t        |t              r&| j                  j	                  |j
                         9| j                  j	                  |       U nGt        |t              r	|g| _        n.t        |t              r|j
                  g| _        nt        d      | j                  j                  | j                  d             y )Nzcategories must be a listr:   )r   r   r`   r   appendr1   r   r?   rG   r[   rF   )rd   r   vals      rl   r:   zContact.categories  s    eT" "D 2c8,%%,,SXX6%%,,S1	2
 s#!&Dx(!&D899 67rt   c                     | j                   S rn   )rb   rq   s    rl   personal_noteszContact.personal_notes  s    $$$rt   c                 f    || _         | j                  j                  | j                  d             y )Nr<   )rb   rG   r[   rF   r   s     rl   r   zContact.personal_notes  s'     % 9:rt   c                     | j                   S )z9ID of the containing folder

        :rtype: str
        )ra   rq   s    rl   	folder_idzContact.folder_id  s     rt   c           
         | j                   }i  |d      | j                   |d      | j                   |d      | j                   |d      | j                   |d      | j
                   |d      | j                   |d      | j                   |d      | j                   |d	      | j                   |d
      | j                   |d      | j                   |d      | j                   |d      | j                  D cg c]>  }| j                  d      |j                  xs d| j                  d      |j                  i@ c} |d      | j                    |d      | j"                   |d      | j$                   |d      | j&                   |d      | j(                  i}|r=|j+                   |d             t-        |j/                               D ]
  }||vs||=  |S c c}w )z} Returns a dictionary in cloud format

        :param restrict_keys: a set of keys to restrict the returned data to.
        r#   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r$   r2   r6   r7   r8   r:   r<   )rF   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rc   r1   r2   r\   r]   r^   r`   rb   r[   r   keys)rd   restrict_keysrg   	recipientdatakeys         rl   to_api_datazContact.to_api_data  s:   
 XX
}t22
xL$--
 {OT[[
 yM4>>	

 wK
 zND,,
 }t22
 |d//
  $"8"8
  $"8"8
 }t22
 |d00
  48KK#A'0 %)HHV$4inn6J$(HHY$79J9J$L #A
   !4#:#:!
" }t22#
$ ~ 4 4%
& |d//'
( !6!6)
. b DIIK( "m+S	" !#As   AG/c                    | j                   st        d      | j                  | j                  j	                  d      j                  | j                               }| j                  j                  |      }t        |      S )z Deletes this contact

        :return: Success or Failure
        :rtype: bool
        :raises RuntimeError: if contact is not yet saved to cloud
        z'Attempting to delete an unsaved Contactr   r    )	rH   RuntimeError	build_url
_endpointsrD   r   r   deleteboolrd   urlresponses      rl   r   zContact.delete  sh     ~~HIInnOO/66$..6IK 88??3'H~rt   c                 Z   | j                   r| j                  sy| j                  | j                  j	                  d      j                  | j                               }| j                  j                  }| j                  | j                        }n| j                  rE| j                  | j                  j	                  d      j                  | j                              }n*| j                  | j                  j	                  d            }| j                  j                  }| j                  | j                        } |||      }|sy	| j                   s|j                         }|j	                  | j                  d
      d      | _         |j	                  | j                  d      d      | _        |j	                  | j                  d      d      | _        | j                  j                   }| j                  r$t#        | j$                        j'                  |      nd| _        | j                  r*t#        | j(                        j'                  |      | _        yd| _        yt*        j,                  j/                         j1                  | j                  j                         | _        y)z Saves this contact to the cloud (create or update existing one
        based on what values have changed)

        :return: Saved or Not
        :rtype: bool
        Tr   r   )r   r   r   r   r   Fr    Nr!   r"   )tzinfo)rH   rG   r   r   rD   r   r   patchr   ra   postjsonrF   rI   rJ   r   rK   r   r~   rL   r   dtdatetimenowreplace)rd   r   methodr   r   r   rh   s          rl   savezContact.save  s    >>&&..##N3::dnn:MOCXX^^F##$2E2E#FD nnOO''8??"&"2"2 @ 45 nnT__%8%8%CDXX]]F##$2E2E#FD#D)~~mmoG$[[$>DN$[[2C)DdKDN%kk$((3I*J*.0DO }}--H!^^ #4<<0;;15 N "__ $DMM2==DO
 	 37 O
  !kkoo/77t}}?U?U7VDOrt   )recipient_typec                   t        |t              rt        |      }|xs | j                  j	                         }|sy| j                  | d      }t        |t        |j                              }|j                  |       |S )a   This method returns a new draft Message instance with
        contacts first email as a recipient

        :param Recipient recipient: a Recipient instance where to send this
         message. If None first email of this contact will be used
        :param RecipientType recipient_type: section to add recipient into
        :return: newly created message
        :rtype: Message or None
        NTr   is_draft)	r   r   r   rc    get_first_recipient_with_addressmessage_constructorrA   r   r[   )rd   r   r   new_messagetarget_recipientss        rl   r   zContact.new_message@  sv     nc**>:NO!M!M!O	..dT.J#K^5I5I1JKi(rt   c                    |E| j                  | j                  j                  d      j                  | j                              }nE| j                  | j                  j                  d      j                  | j                  |            }	 | j
                  j                  |      }|sy|j                  S # t        $ r.}t        j                  dj                  |             Y d}~yd}~ww xY w)zReturns this contact profile photo

        :param str size: 48x48, 64x64, 96x96, 120x120, 240x240,
         360x360, 432x432, 504x504, and 648x648
        Nr   r   r   )r    sizez;Error while retrieving the contact profile photo. Error: {})
r   r   rD   r   rH   r   r   logdebugcontent)rd   r   r   r   es        rl   get_profile_photozContact.get_profile_photoY  s     <..!4!4W!=!D!D!D!WXC..!4!4\!B!I!IT^^bf!I!ghC	xx||C(H
   	IISZZ[\]^	s   B8 8	C/$C**C/c                     | j                  | j                  j                  d      j                  | j                              }| j
                  j                  ||ddi      }t        |      S )z` Updates this contact profile photo
        :param bytes photo: the photo data in bytes
        r   r   zContent-typez
image/jpeg)r   headers)r   r   rD   r   rH   r   r   r   )rd   r   r   r   s       rl   update_profile_photozContact.update_profile_photoo  sZ    
 nnT__009@@DNN@ST88>>#ENL;Y>ZH~rt   rn   ).__name__
__module____qualname____doc__r   r   r   rC   rr   rp   r{   propertyr~   r   rv   setterr%   r1   r'   rw   r(   r   r   r+   r   r   r   r   rc   r   r   r   r   r   r:   r   r   r   r   r   r   TOr   r   r   __classcell__rk   s   @rl   r   r      s   S (?.;J "!%4 S&jE1     # # 9 9   ]]4 4   
[[7 7   ^^5 5 ? ?   \\3 3     6 6 # # 9 9 ! ! 8 8 & & < < & & < < # # 9 9 " " 	8 	8   & & ' ' = = # # 9 9 $ $ : : ) ) ? ? ! ! 8 8  % % ; ;    $L"0dM<L<L 2 ,rt   r   c                   l     e Zd ZdZddddddZeZeZddd	 fd

Z	d Z
d Zd ZddddddZd Z xZS )BaseContactFolderz, Base Contact Folder Grouping Functionality r   z/contactFolders/{id}/contactsz/contactFolders/{id}z/contactFoldersz!/contactFolders/{id}/childFolders)root_contactsfolder_contacts
get_folderroot_folderschild_foldersNr   c                ^   |r|rt        d      |r|j                  n|| _        |j                  dd      xs |rt        |dd      nd}t        |   |r|j                  n|j                  d      |       |j                  dd      | _        |j                  | j                  i       }|j                  | j                  d      |j                  d	d
            | _        |j                  | j                  d      d      | _        |j                  | j                  d      d      | _        y)a   Create a contact folder component

        :param parent: parent folder/account for this folder
        :type parent: BaseContactFolder or Account
        :param Connection con: connection to use if no parent specified
        :param Protocol protocol: protocol to use if no parent specified
         (kwargs)
        :param str main_resource: use this resource instead of parent resource
         (kwargs)
        r   r   Nr   r   rootFr#   r1   r$   r    r;   )r?   r   r@   rA   rB   rC   r   rD   r  rE   rF   r1   r   	parent_id)rd   r   r   re   r   rf   rk   s         rl   rC   zBaseContactFolder.__init__  s    cIJJ!'6::S 

?D9 H6<GFOT2$ 	 	(.V__FJJz4J' 	 	) JJvu-	ZZ 4 4b9
 NN488M#:#)::f.0$23	 $=#1A(BDIrt   c                 "    | j                         S rn   ro   rq   s    rl   rr   zBaseContactFolder.__str__  rs   rt   c                 8    dj                  | j                        S )NzContact Folder: {})r   r1   rq   s    rl   rp   zBaseContactFolder.__repr__  s    #**49955rt   c                 4    | j                   |j                   k(  S rn   r   ry   s     rl   r{   zBaseContactFolder.__eq__  r|   rt   )queryorder_bybatchc                     j                   r+ j                   j                  j                  d            }nD j                   j                  j                  d      j	                   j
                              }|| j                  j                  kD  r j                  j                  }d|r|n|i}|r||d<   |r5t        |t              r||d<   n|j                  |j                                 j                  j                  ||      }|st        d	      S |j                         } fd
|j                  dg       D        }	|j                  t        d      }
|r|
rt!         |	 j"                  |
|      S |	S )a%   Gets a list of contacts from this address book

        To use query an order_by check the OData specification here:
        http://docs.oasis-open.org/odata/odata/v4.0/errata03/os/complete/
        part2-url-conventions/odata-v4.0-errata03-os-part2-url-conventions
        -complete.html

        :param limit: max no. of contacts to get. Over 999 uses batch.
        :type limit: int or None
        :param query: applies a OData filter to the request
        :type query: Query or str
        :param order_by: orders the result set based on this condition
        :type order_by: Query or str
        :param int batch: batch size, retrieves items in
         batches allowing to retrieve more items than the limit.
        :return: list of contacts
        :rtype: list[Contact] or Pagination
        r   r  r   N$top$orderby$filterparams c              3   ^   K   | ]$  } j                   dd ij                  |i & yw)r   Nr  )contact_constructorrE   ).0r   rd   s     rl   	<genexpr>z1BaseContactFolder.get_contacts.<locals>.<genexpr>  sC      : -D,, PD P040D0Dg/NP :s   *-r   )r   r   constructor	next_linklimit)r  r   r   rD   r   r   r   max_top_valuer   r   update	as_paramsr   iterr   r	   r   r  )rd   r  r  r  r  r   r  r   r   contactsr  s   `          rl   get_contactszBaseContactFolder.get_contacts  sT   ( 99..!4!4_!EFC..##$56==~~ > '(C =EDMM$?$??MM//E5%e4!)F:%%$)y!eoo/088<<F<38O}}:#'88GR#8: HH.5	YT*.*B*B(1@ @ Ort   c                     |sy| j                         j                  dd|j                         d      }t        | j	                  d|            }|r|d   S dS )	z Returns a Contact by it's email

        :param email: email to get contact for
        :return: Contact for specified email
        :rtype: Contact
        Nemail_addressesr2   eq)
collection	attributeword	operationr   r  r  r   )qanyr   r   r!  )rd   r5   r  r   s       rl   get_contact_by_emailz&BaseContactFolder.get_contact_by_email  s_     (9Y"'++-4  A)))?@&x{0D0rt   )d   )r   r   r   r   r   r   r  r   r   rC   rr   rp   r{   r!  r,  r   r   s   @rl   r   r   z  s]    6 %:,)<J "!!%4 'JR61;td$ ;z1rt   r   c                   j    e Zd ZdZddZdddddZd Zd Zd Zd	 Z	d
 Z
ej                  fdddZy)ContactFolderz! A Contact Folder representation Nc                 6   |r|rt        d      |s|st        d      |r=| j                  | j                  j                  d      j	                  |            }d}n| j
                  r+| j                  | j                  j                  d            }nD| j                  | j                  j                  d      j	                  | j                              }dj	                  | j                  d	      |      d
d}| j                  j                  ||      }|sy|r|j                         }n+|j                         j                  d      }|r|d   nd}|y | j                  d| j                  | j                  | j                  d| j                  |iS )aY   Returns a Contact Folder by it's id or child folders by name

        :param folder_id: the folder_id to be retrieved.
         Can be any folder Id (child or not)
        :param folder_name: the folder name to be retrieved.
         Must be a child of this folder
        :return: a single contact folder
        :rtype: ContactFolder
        zProvide only one of the optionszProvide one of the optionsr  r   Nr  r  z
{} eq '{}'r#   r   )r  r  r  r   r   )r   r   r   r  )r   r   r   rD   r   r  r   rF   r   r   rk   r   r   rE   )rd   r   folder_namer   r  r   folders          rl   r  zContactFolder.get_folder
  s    @AA;<<..##L188I8FHCF yynnT__%8%8%HInnOO''8??>> @ +, ".!4!4TXXm5L5@"BKLNF 88<<F<3]]_F]]_((1F"(VAYdF~ t~~ @$((T]],0,>,>@!%!5!5v >@ 	@rt   )r  r  c          	      v   | j                   r+| j                  | j                  j                  d            }nD| j                  | j                  j                  d      j	                  | j
                              }i }|r||d<   |r||d<   |r5t        |t              r||d<   n|j                  |j                                | j                  j                  ||xs d      }|sg S |j                         }|j                  d	g       D cg c]"  } | j                  dd
| i| j                  |i$ c}S c c}w )a   Returns a list of child folders

        :param int limit: max no. of folders to get. Over 999 uses batch.
        :param query: applies a OData filter to the request
        :type query: Query or str
        :param order_by: orders the result set based on this condition
        :type order_by: Query or str
        :return: list of folders
        :rtype: list[ContactFolder]
        r  r  r   r  r  r  Nr  r   r   r  )r  r   r   rD   r   r   r   r   r  r  r   r   rk   rE   )	rd   r  r  r  r   r  r   r   r2  s	            rl   get_folderszContactFolder.get_folders>  s    99..!4!4^!DEC..##O4;;t~~;NPC "F6N!)F:%%$)y!eoo/088<<FNd<;I}} #hhw35 MdMt/C/CV.LM 5 	5 5s   'D6c                    |sy| j                   r+| j                  | j                  j                  d            }nD| j                  | j                  j                  d      j	                  | j
                              }| j                  j                  || j                  d      |i      }|sy|j                         } | j                  dd| i| j                  |iS )	z Creates a new child folder

        :param str folder_name: name of the new folder to create
        :return: newly created folder
        :rtype: ContactFolder or None
        Nr  r  r   r#   r   r   r  )r  r   r   rD   r   r   r   r   rF   r   rk   rE   )rd   r1  r   r   r2  s        rl   create_child_folderz!ContactFolder.create_child_folderf  s     99..!4!4^!DEC..##O4;;t~~;NPC 88=='+xx'>&L ! N t~~LTLd.B.BF-KLLrt   c                    | j                   ry|sy| j                  | j                  j                  d      j	                  | j
                              }| j                  j                  || j                  d      |i      }|sy|j                         }|j                  | j                  d      d      | _
        |j                  | j                  d      d      | _        y	)
z Change this folder name

        :param str name: new name to change to
        :return: Updated or Not
        :rtype: bool
        Fr  r   r#   r   r$   r;   NT)r  r   r   rD   r   r   r   r   rF   r   r1   r  )rd   r1   r   r   r2  s        rl   update_folder_namez ContactFolder.update_folder_name  s     99nnOO-444GI 88>>#TXXm-Dd,K>LJJtxx6;	DHH-=$>Ert   c                 (   | j                   ry|sy| j                  | j                  j                  d      j	                  | j
                              }t        |t              r|j
                  }nt        |t              r|}ny| j                  j                  || j                  d      |i      }|sy|j                         }|j                  | j                  d      d      | _        |j                  | j                  d      d      | _        y	)
z Change this folder name

        :param to_folder: folder_id/ContactFolder to move into
        :type to_folder: str or ContactFolder
        :return: Moved or Not
        :rtype: bool
        Fr  r   r;   r   r#   r$   NT)r  r   r   rD   r   r   r   r/  r   r   r   rF   r   r1   r  )rd   	to_folderr   r   r   r2  s         rl   move_folderzContactFolder.move_folder  s     99nnOO-444GI i/!++I	3'!I88>>#(,1A(BI'N " PJJtxx6;	DHH-=$>Ert   c                    | j                   s| j                  sy| j                  | j                  j	                  d      j                  | j                              }| j                  j                  |      }|syd| _        y)zS Deletes this folder

        :return: Deleted or Not
        :rtype: bool
        Fr  r   NT)r  r   r   r   rD   r   r   r   r   s      rl   r   zContactFolder.delete  sk     99DNNnnOO-444GI 88??3'rt   c                 d    | j                  |       }| j                  s| j                  |_        |S )z Creates a new contact to be saved into it's parent folder

        :return: newly created contact
        :rtype: Contact
        )r   )r  r  r   _ContactFolder__folder_id)rd   r   s     rl   new_contactzContactFolder.new_contact  s/     **$*7yy"&..Grt   )r  c                v   t        |t              rt        |      }| j                  d|      D cg c]6  }|j                  r(|j                  d   j
                  r|j                  d   8 }}|sy| j                  | d      }t        |t        |j                              }|j                  |       |S c c}w )ad   This method returns a new draft Message instance with all the
        contacts first email as a recipient

        :param RecipientType recipient_type: section to add recipient into
        :param query: applies a OData filter to the request
        :type query: Query or str
        :return: newly created message
        :rtype: Message or None
        Nr)  r   Tr   )
r   r   r   r!  rc   r2   r   rA   r   r[   )rd   r   r  r   r3   r   r   s          rl   r   zContactFolder.new_message  s     nc**>:N &*%6%6T%6%OG! GNN1,=,E,E nnQ' G
 G ..dT.J#K^5I5I1JKj)Gs   ;B6)NNrn   )r   r   r   r   r  r4  r6  r8  r;  r   r?  r   r   r   r  rt   rl   r/  r/    sM    +2@h&5td &5PM64!F*	 *7)9)9 D rt   r/  c                   0     e Zd ZdZddd fd
Zd Z xZS )AddressBookz& A class representing an address book Nr   c                ,    t        |   d||dd| y )NT)r   r   r  r  )rB   rC   )rd   r   r   re   rk   s       rl   rC   zAddressBook.__init__  s    ECdEfErt   c                 8    dj                  | j                        S )NzAddress Book resource: {})r   r   rq   s    rl   rp   zAddressBook.__repr__  s    *11$2D2DEErt   )r   r   r   r   rC   rp   r   r   s   @rl   rB  rB    s    0!%4 FFrt   rB  )r   r   loggingdateutil.parserr   requests.exceptionsr   categoryr   messager   r   utilsr	   r
   r   r   r   r   	getLoggerr   r   r   r   r/  rB  r  rt   rl   <module>rL     ss      ! )  +  g!b	lO b	JJ1 J1Zp% pfF- Frt   