o
    w+f                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  G dd de!Z"dS )z2Twilio SendGrid v3/mail/send response body builder   )Bcc)Cc)Content)	CustomArg)DynamicTemplateData)Email)From)Header)MimeType)Personalization)ReplyTo)SendAt)Subject)Substitution)
TemplateId)Toc                   @   s|  e Zd ZdZ								d}ddZdd Zd~d	d
Zdd Zdd Zdd Z		dddZ
edd Zd~ddZedd ZejdddZ	dddZedd ZejdddZ	ddd Zed!d" Zej			dd#d"Z			dd$d%Zed&d' Zejd(d' Zed)d* Zed+d, Zejd-d, Zd.d/ Zed0d1 Zejd2d1 Zd3d4 Zed5d6 Zed7d8 Zejd9d8 Zd:d; Zed<d= Zejd>d= Zed?d@ ZejdAd@ ZedBdC Z e jdDdC Z edEdF Z!e!jdGdF Z!edHdI Z"e"jdJdI Z"edKdL Z#edMdN Z$e$jdOdN Z$ddPdQZ%edRdS Z&edTdU Z'e'jdVdU Z'dWdX Z(edYdZ Z)e)jd[dZ Z)ed\d] Z*ed^d_ Z+e+jd`d_ Z+dadb Z,edcdd Z-ededf Z.e.jdgdf Z.dhdi Z/edjdk Z0e0jdldk Z0edmdn Z1e1jdodn Z1edpdq Z2e2jdrdq Z2edsdt Z3e3jdudt Z3edvdw Z4e4jdxdw Z4dydz Z5e6d{d| Z7dS )Mailz*Creates the response body for v3/mail/sendNFc	           	      C   s   d| _ d| _d| _d| _d| _g | _d| _d| _d| _d| _	d| _
d| _d| _d| _d| _d| _d| _d| _|dur=|| _|durH| ||| |durO|| _|durZ| |tj |dure| |tj |durr| |tj dS dS )a  
        Creates the response body for a v3/mail/send API call

        :param from_email: The email address of the sender
        :type from_email: From, tuple, optional
        :param subject: The subject of the email
        :type subject: Subject, optional
        :param to_emails: The email address of the recipient
        :type to_emails: To, str, tuple, list(str), list(tuple),
                         list(To), optional
        :param plain_text_content: The plain text body of the email
        :type plain_text_content: string, optional
        :param html_content: The html body of the email
        :type html_content: string, optional
        :param amp_html_content: The amp-html body of the email
        :type amp_html_content: string, optional
        N)_attachments_categories	_contents_custom_args_headers_personalizations	_sections_asm	_batch_id_from_email_ip_pool_name_mail_settings	_reply_to_reply_to_list_send_at_subject_template_id_tracking_settings
from_emailadd_tosubjectadd_contentr
   textamphtml)	selfr%   	to_emailsr'   plain_text_contenthtml_contentamp_html_contentglobal_substitutionsis_multiple r3   M/var/www/html/venv/lib/python3.10/site-packages/sendgrid/helpers/mail/mail.py__init__   s>   zMail.__init__c                 C   s   t |  S )zA JSON-ready string representation of this Mail object.

        :returns: A JSON-ready string representation of this Mail object.
        :rtype: string
        )strgetr,   r3   r3   r4   __str__S   s   zMail.__str__    c                 C   s   |pg }| || |S )aZ  Ensure an item is appended to a list or create a new empty list

        :param new_items: the item(s) to append
        :type new_items: list(obj)
        :param append_to: the list on which to append the items
        :type append_to: list()
        :param index: index of the list on which to append the items
        :type index: int
        insert)r,   	new_items	append_toindexr3   r3   r4   _ensure_append[   s   
zMail._ensure_appendc                 C   s   |pg }| d| |S )a  Ensure an item is inserted to a list or create a new empty list

        :param new_items: the item(s) to insert
        :type new_items: list(obj)
        :param insert_to: the list on which to insert the items at index 0
        :type insert_to: list()
        r:   r;   )r,   r=   	insert_tor3   r3   r4   _ensure_inserti   s   zMail._ensure_insertc                 C   s&   t  }dd |p	g D }dd |D S )z]Flatten a dict

        :param dicts: Flatten a dict
        :type dicts: list(dict)
        c                 S      g | ]}|  qS r3   r7   ).0dr3   r3   r4   
<listcomp>|       z'Mail._flatten_dicts.<locals>.<listcomp>c                 S   s$   i | ]}|  D ]\}}||qqS r3   )items)rE   rF   kvr3   r3   r4   
<dictcomp>}   s   $ z'Mail._flatten_dicts.<locals>.<dictcomp>)dict)r,   dictsrF   list_of_dictsr3   r3   r4   _flatten_dictsu   s   zMail._flatten_dictsc                 C   s   |dur|  S dS )zGet the JSON representation of the object, else return None

        :param from_obj: Get the JSON representation of the object,
        else return None
        :type from_obj: obj
        NrD   )r,   from_objr3   r3   r4   _get_or_none   s   zMail._get_or_nonec           	      C   sL  |du rSt |tr|D ]}t }|| | | qnt }|| | | |durOt |trD|D ]}| jD ]}|| q9q4dS | jD ]	}|| qGdS dS z	| j| }d}W n tyj   t }d}Y nw t |tr{|D ]}|| qrn|| |durt |tr|D ]}|| qn|| |s| j||d dS dS )a  Adds emails to the Personalization object

        :param emails: An Email or list of Email objects
        :type emails: Email, list(Email)
        :param global_substitutions: A dict of substitutions for all recipients
        :type global_substitutions: dict
        :param is_multiple: Create a new personalization for each recipient
        :type is_multiple: bool
        :param p: p is the Personalization object or Personalization object
                  index
        :type p: Personalization, integer, optional
        TNFr?   )	
isinstancelistr   	add_emailadd_personalizationpersonalizationsadd_substitutionr   
IndexError)	r,   emailsr1   r2   pemailpersonalizationsubstitutionhas_internal_personalizationr3   r3   r4   _set_emails   sT   











zMail._set_emailsc                 C      | j S )z]A list of one or more Personalization objects

        :rtype: list(Personalization)
        )r   r8   r3   r3   r4   rX         zMail.personalizationsc                 C   s   |  || j|| _dS )zAdd a Personalization object

        :param personalization: Add a Personalization object
        :type personalization: Personalization
        :param index: The index where to add the Personalization
        :type index: int
        N)r@   r   )r,   r^   r?   r3   r3   r4   rW      s   
zMail.add_personalizationc                 C      d S Nr3   r8   r3   r3   r4   to      zMail.toc                 C      t |tr,|D ]"}t |trt|d}t |tr!t|d |d }| |||| qdS t |tr6t|d}t |trDt|d |d }| |||| dS )a>  Adds To objects to the Personalization object

        :param to_emails: The email addresses of all recipients
        :type to_emails: To, str, tuple, list(str), list(tuple), list(To)
        :param global_substitutions: A dict of substitutions for all recipients
        :type global_substitutions: dict
        :param is_multiple: Create a new personalization for each recipient
        :type is_multiple: bool
        :param p: p is the Personalization object or Personalization object
                  index
        :type p: Personalization, integer, optional
        Nr:   r   )rT   rU   r6   r   tupler&   )r,   r-   r1   r2   r\   r]   r3   r3   r4   rf         






c                 C   s   t |tr7|D ]-}t |trt|d}nt |tr#t|d |d }n	t |ts,td| |||| qdS t |trAt|d}t |trOt|d |d }t |trW|j}| |||| dS )a#  Adds a To object to the Personalization object

        :param to_email: A To object
        :type to_email: To, str, tuple, list(str), list(tuple), list(To)
        :param global_substitutions: A dict of substitutions for all recipients
        :type global_substitutions: dict
        :param is_multiple: Create a new personalization for each recipient
        :type is_multiple: bool
        :param p: p is the Personalization object or Personalization object
                  index
        :type p: Personalization, integer, optional
        Nr:   r   z<Please use a To/Cc/Bcc, tuple, or a str for a to_email list.)	rT   rU   r6   r   ri   r   
ValueErrorra   r^   )r,   to_emailr1   r2   r\   r]   r3   r3   r4   r&      s&   







zMail.add_toc                 C   rd   re   r3   r8   r3   r3   r4   cc  rg   zMail.ccc                 C   s   t |tr,|D ]"}t |trt|d}t |tr!t|d |d }| |||| qdS t |tr6t|d}t |trDt|d |d }| |||| dS )a  Adds Cc objects to the Personalization object

        :param cc_emails: An Cc or list of Cc objects
        :type cc_emails: Cc, list(Cc), tuple
        :param global_substitutions: A dict of substitutions for all recipients
        :type global_substitutions: dict
        :param is_multiple: Create a new personalization for each recipient
        :type is_multiple: bool
        :param p: p is the Personalization object or Personalization object
                  index
        :type p: Personalization, integer, optional
        Nr:   r   )rT   rU   r6   r   ri   add_ccr   )r,   	cc_emailsr1   r2   r\   r]   r3   r3   r4   rm     rj   c                 C   V   t |tr
t|d}t |trt|d |d }t |tr |j}| j||||d dS )a  Adds a Cc object to the Personalization object

        :param to_emails: An Cc object
        :type to_emails: Cc
        :param global_substitutions: A dict of substitutions for all recipients
        :type global_substitutions: dict
        :param is_multiple: Create a new personalization for each recipient
        :type is_multiple: bool
        :param p: p is the Personalization object or Personalization object
                  index
        :type p: Personalization, integer, optional
        Nr:   r   r2   r\   )rT   r6   r   ri   r   r^   ra   )r,   cc_emailr1   r2   r\   r3   r3   r4   rn   7  s   




zMail.add_ccc                 C   rd   re   r3   r8   r3   r3   r4   bccN  rg   zMail.bccc                 C   rh   )a  Adds Bcc objects to the Personalization object

        :param bcc_emails: An Bcc or list of Bcc objects
        :type bcc_emails: Bcc, list(Bcc), tuple
        :param global_substitutions: A dict of substitutions for all recipients
        :type global_substitutions: dict
        :param is_multiple: Create a new personalization for each recipient
        :type is_multiple: bool
        :param p: p is the Personalization object or Personalization object
                  index
        :type p: Personalization, integer, optional
        Nr:   r   )rT   rU   r6   r   ri   add_bcc)r,   
bcc_emailsr1   r2   r\   r]   r3   r3   r4   rs   R  s   






c                 C   rp   )a  Adds a Bcc object to the Personalization object

        :param to_emails: An Bcc object
        :type to_emails: Bcc
        :param global_substitutions: A dict of substitutions for all recipients
        :type global_substitutions: dict
        :param is_multiple: Create a new personalization for each recipient
        :type is_multiple: bool
        :param p: p is the Personalization object or Personalization object
                  index
        :type p: Personalization, integer, optional
        Nr:   r   rq   )rT   r6   r   ri   r   r^   ra   )r,   	bcc_emailr1   r2   r\   r3   r3   r4   rt   s  s   




zMail.add_bccc                 C   rb   )z;The global Subject object

        :rtype: Subject
        )r"   r8   r3   r3   r4   r'     rc   zMail.subjectc                 C      t |tr:|jdur5z
| j|j }d}W n ty"   t }d}Y nw |j|_|s3| j||jd dS dS || _dS t|| _dS )z|The subject of the email(s)

        :param value: The subject of the email(s)
        :type value: Subject, string
        NTFrS   )	rT   r   r^   r   rZ   r   r'   rW   r"   r,   valuer^   r`   r3   r3   r4   r'     s&   




c                 C   rb   )zFA list of global Header objects

        :rtype: list(Header)
        )r   r8   r3   r3   r4   headers  rc   zMail.headersc                 C   rd   re   r3   r8   r3   r3   r4   header  rg   zMail.headerc                 C   0   t |tr|D ]}| | qdS | | dS )zAdd headers to the email

        :param value: A list of Header objects or a dict of header key/values
        :type value: Header, list(Header), dict
        N)rT   rU   
add_header)r,   rz   hr3   r3   r4   r{     
   
c                 C      |j durIz
| j|j  }d}W n ty   t }d}Y nw t|tr6t| d \}}|t	|| n|| |sG| j
||j d dS dS t|tret| d \}}| t	||| j| _dS | || j| _dS )zAdd headers to the email globaly or to a specific Personalization

        :param value: A Header object or a dict of header key/values
        :type value: Header, dict
        NTFr:   rS   )r^   r   rZ   r   rT   rM   rU   rI   r}   r	   rW   r@   r   )r,   r{   r^   r`   rJ   rK   r3   r3   r4   r}     s2   






zMail.add_headerc                 C   rd   re   r3   r8   r3   r3   r4   r_     rg   zMail.substitutionc                 C   r|   )zAdd substitutions to the email

        :param value: Add substitutions to the email
        :type value: Substitution, list(Substitution)
        N)rT   rU   rY   )r,   r_   sr3   r3   r4   r_     r   c                 C   s   |j r/z
| j|j  }d}W n ty   t }d}Y nw || |s-| j||j d dS dS t|trF|D ]}| jD ]}|| q;q6dS | jD ]}|| qIdS )zAdd a substitution to the email

        :param value: Add a substitution to the email
        :type value: Substitution
        TFrS   N)	r^   r   rZ   r   rY   rW   rT   rU   rX   )r,   r_   r^   r`   r   r\   r3   r3   r4   rY     s0   





zMail.add_substitutionc                 C   rb   )zLA list of global CustomArg objects

        :rtype: list(CustomArg)
        r   r8   r3   r3   r4   custom_args  rc   zMail.custom_argsc                 C   rb   re   r   r8   r3   r3   r4   
custom_arg"  s   zMail.custom_argc                 C   r|   )zAdd custom args to the email

        :param value: A list of CustomArg objects or a dict of custom arg
                      key/values
        :type value: CustomArg, list(CustomArg), dict
        N)rT   rU   add_custom_arg)r,   r   cr3   r3   r4   r   &  s
   
c                 C   r   )zAdd custom args to the email globaly or to a specific Personalization

        :param value: A CustomArg object or a dict of custom arg key/values
        :type value: CustomArg, dict
        NTFr:   rS   )r^   r   rZ   r   rT   rM   rU   rI   r   r   rW   r@   r   )r,   r   r^   r`   rJ   rK   r3   r3   r4   r   4  s4   







zMail.add_custom_argc                 C   rb   )z9The global SendAt object

        :rtype: SendAt
        )r!   r8   r3   r3   r4   send_atT  rc   zMail.send_atc                 C   rw   )zA unix timestamp specifying when your email should
        be delivered.

        :param value: A unix timestamp specifying when your email should
        be delivered.
        :type value: SendAt, int
        NTFrS   )	rT   r   r^   r   rZ   r   r   rW   r!   rx   r3   r3   r4   r   \  s$   
	



c                 C   rd   re   r3   r8   r3   r3   r4   dynamic_template_datax  rg   zMail.dynamic_template_datac                 C   sh   t |ts	t|}z
| j|j }d}W n ty!   t }d}Y nw |j|_|s2| j||jd dS dS )zData for a transactional template

        :param value: Data for a transactional template
        :type value: DynamicTemplateData, a JSON-serializable structure
        TFrS   N)rT   r   r   r^   rZ   r   r   rW   rx   r3   r3   r4   r   |  s   

c                 C   rb   )z>The email address of the sender

        :rtype: From
        )r   r8   r3   r3   r4   r%     rc   zMail.from_emailc                 C   :   t |tr
t|d}t |trt|d |d }|| _dS )zThe email address of the sender

        :param value: The email address of the sender
        :type value: From, str, tuple
        Nr:   r   )rT   r6   r   ri   r   r,   ry   r3   r3   r4   r%     
   



c                 C   rb   )z<The reply to email address

        :rtype: ReplyTo
        )r   r8   r3   r3   r4   reply_to  rc   zMail.reply_toc                 C   r   )z~The reply to email address

        :param value: The reply to email address
        :type value: ReplyTo, str, tuple
        Nr:   r   )rT   r6   r   ri   r   r   r3   r3   r4   r     r   c                 C   rb   )zPA list of ReplyTo email addresses

        :rtype: list(ReplyTo), tuple
        )r    r8   r3   r3   r4   reply_to_list  rc   zMail.reply_to_listc                 C   sH   t |tr"|D ]}t |trt |jtstdqtd|| _dS dS )zA list of ReplyTo email addresses

        :param value: A list of ReplyTo email addresses
        :type value: list(ReplyTo), tuple
        z;You must provide an email for each entry in a reply_to_listz2Please use a list of ReplyTos for a reply_to_list.N)rT   rU   r   r]   r6   rk   r    )r,   ry   replyr3   r3   r4   r     s   


c                 C   rb   )zAThe contents of the email

        :rtype: list(Content)
        )r   r8   r3   r3   r4   contents  rc   zMail.contentsc                 C   rd   re   r3   r8   r3   r3   r4   content  rg   zMail.contentc                 C   r|   )zThe content(s) of the email

        :param contents: The content(s) of the email
        :type contents: Content, list(Content)
        N)rT   rU   r(   )r,   r   r   r3   r3   r4   r     r   c                 C   s   t |tr
t||}|jtjkr| || j| _dS |jtjkrL| jr>| jD ]}|jtjkr2d} n|jtj	kr<d} nq&nd}| j
|| j|d| _dS | jrUt| j}nd}| j
|| j|d| _dS )zAdd content to the email

        :param contents: Content to be added to the email
        :type contents: Content
        :param mime_type: Override the mime type
        :type mime_type: MimeType, str
        r   r:   rS   N)rT   r6   r   	mime_typer
   r)   rB   r   r*   r+   r@   len)r,   r   r   _contentr?   r3   r3   r4   r(     s2   


zMail.add_contentc                 C   rb   )zHThe attachments to this email

        :rtype: list(Attachment)
        )r   r8   r3   r3   r4   attachments  rc   zMail.attachmentsc                 C   rd   re   r3   r8   r3   r3   r4   
attachment  rg   zMail.attachmentc                 C   r|   )zAdd attachment(s) to this email

        :param attachment: Add attachment(s) to this email
        :type attachment: Attachment, list(Attachment)
        N)rT   rU   add_attachment)r,   r   ar3   r3   r4   r     r   c                 C      |  || j| _dS )zAdd an attachment to this email

        :param attachment: Add an attachment to this email
        :type attachment: Attachment
        N)r@   r   )r,   r   r3   r3   r4   r   *     zMail.add_attachmentc                 C   rb   )zQThe transactional template id for this email

        :rtype: TemplateId
        )r#   r8   r3   r3   r4   template_id2  rc   zMail.template_idc                 C   s"   t |tr
|| _dS t|| _dS )zThe transactional template id for this email

        :param value: The transactional template id for this email
        :type value: TemplateId
        N)rT   r   r#   r   r3   r3   r4   r   :  s   

c                 C   rb   )zXThe block sections of code to be used as substitutions

        :rtype: Section
        )r   r8   r3   r3   r4   sectionsF  rc   zMail.sectionsc                 C   rd   re   r3   r8   r3   r3   r4   sectionN  rg   zMail.sectionc                 C   r|   )zgThe block sections of code to be used as substitutions

        :rtype: Section, list(Section)
        N)rT   rU   add_section)r,   r   r~   r3   r3   r4   r   R  
   
c                 C   r   )zA block section of code to be used as substitutions

        :param section: A block section of code to be used as substitutions
        :type section: Section
        N)r@   r   )r,   r   r3   r3   r4   r   ^  r   zMail.add_sectionc                 C   rb   )zPThe categories assigned to this message

        :rtype: list(Category)
        )r   r8   r3   r3   r4   
categoriesf  rc   zMail.categoriesc                 C   rd   re   r3   r8   r3   r3   r4   categoryn  rg   zMail.categoryc                 C   r|   )zPAdd categories assigned to this message

        :rtype: list(Category)
        N)rT   rU   add_category)r,   r   r   r3   r3   r4   r   r  r   c                 C   r   )zJAdd a category assigned to this message

        :rtype: Category
        N)r@   r   )r,   r   r3   r3   r4   r   ~  s   zMail.add_categoryc                 C   rb   )z=The batch id for this email

        :rtype: BatchId
        r   r8   r3   r3   r4   batch_id  rc   zMail.batch_idc                 C   
   || _ dS )ztThe batch id for this email

        :param value: The batch id for this email
        :type value: BatchId
        Nr   r   r3   r3   r4   r        
c                 C   rb   )zHAn object specifying unsubscribe behavior.

        :rtype: Asm
        r   r8   r3   r3   r4   asm  rc   zMail.asmc                 C   r   )zAn object specifying unsubscribe behavior.

        :param value: An object specifying unsubscribe behavior.
        :type value: Asm
        Nr   r   r3   r3   r4   r     r   c                 C   rb   )z\The IP Pool that you would like to send this email from

        :rtype: IpPoolName
        r   r8   r3   r3   r4   ip_pool_name  rc   zMail.ip_pool_namec                 C   r   )zThe IP Pool that you would like to send this email from

        :paran value: The IP Pool that you would like to send this email from
        :type value: IpPoolName
        Nr   r   r3   r3   r4   r     r   c                 C   rb   )zGThe mail settings for this email

        :rtype: MailSettings
        r   r8   r3   r3   r4   mail_settings  rc   zMail.mail_settingsc                 C   r   )zThe mail settings for this email

        :param value: The mail settings for this email
        :type value: MailSettings
        Nr   r   r3   r3   r4   r     r   c                 C   rb   )zOThe tracking settings for this email

        :rtype: TrackingSettings
        r$   r8   r3   r3   r4   tracking_settings  rc   zMail.tracking_settingsc                 C   r   )zThe tracking settings for this email

        :param value: The tracking settings for this email
        :type value: TrackingSettings
        Nr   r   r3   r3   r4   r     r   c                 C   s<  i d|  | jd|  | jddd | jpg D ddd | jp"g D dd	d | jp-g D d
|  | jd| | jd| | j	ddd | j
pMg D d| | jd|  | jd|  | jd|  | jd|  | jd|  | jd|  | jd|  | jddd | jpg D i}dd | D S )z
        Get a JSON-ready representation of this Mail object.

        :returns: This Mail object, ready for use in a request body.
        :rtype: dict
        fromr'   rX   c                 S   rC   r3   rD   )rE   r\   r3   r3   r4   rG     rH   zMail.get.<locals>.<listcomp>r   c                 S   rC   r3   rD   rE   r   r3   r3   r4   rG     rH   r   c                 S   rC   r3   rD   )rE   r   r3   r3   r4   rG     rH   r   r   rz   r   c                 S   rC   r3   rD   r   r3   r3   r4   rG     rH   r   r   r   r   r   r   r   r   r   c                 S   rC   r3   rD   )rE   rr3   r3   r4   rG     rH   c                 S   s.   i | ]\}}|d ur|g kr|i kr||qS re   r3   )rE   keyry   r3   r3   r4   rL     s    zMail.get.<locals>.<dictcomp>)rR   r%   r'   rX   r   r   r   rP   r   rz   r   r   r   r   r   r   r   r   r   r   rI   )r,   mailr3   r3   r4   r7     sL   	
zMail.getc                 C   s   | t |d|dt |dd}z| }W n ty'   | }Y nw |t| |  |	 D ]\}}|
t|| q8|S )zCreate a Mail object from an instance of
        email.message.EmailMessage.

        :type message: email.message.EmailMessage
        :rtype: Mail
        r   r   r   )r%   r'   r-   )r   r7   get_contentAttributeErrorget_payloadr(   r   get_content_typestriprI   r}   r	   )clsmessager   bodyrJ   rK   r3   r3   r4   from_EmailMessage  s"   zMail.from_EmailMessage)NNNNNNNF)r:   )NFr:   re   )8__name__
__module____qualname____doc__r5   r9   r@   rB   rP   rR   ra   propertyrX   rW   rf   setterr&   rm   rn   rs   rt   r'   rz   r{   r}   r_   rY   r   r   r   r   r   r%   r   r   r   r   r(   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r7   classmethodr   r3   r3   r3   r4   r      s   
<



9



#


#





 




 













#




















r   N)#r   rv   r   rr   r   r   r   r   r   r   r   r]   r   r%   r   r{   r	   r   r
   r^   r   r   r   r   r   r'   r   r_   r   r   r   rl   r   objectr   r3   r3   r3   r4   <module>   s$    