U
    ä«Fc�  ã                   @   s   G d d„ de ƒZdS )c                   @   sl  e Zd ZdZdd„ Zdd„ Zdd„ Zedd	„ ƒZej	d
d	„ ƒZdd„ Z
edd„ ƒZej	dd„ ƒZdd„ Zedd„ ƒZej	dd„ ƒZdd„ Zedd„ ƒZej	dd„ ƒZdd„ Zedd„ ƒZej	dd„ ƒZedd „ ƒZej	d!d „ ƒZd"d#„ Zed$d%„ ƒZej	d&d%„ ƒZd'd(„ Zed)d*„ ƒZej	d+d*„ ƒZd,d-„ Zed.d/„ ƒZej	d0d/„ ƒZed1d2„ ƒZej	d3d2„ ƒZd4d5„ Zd6S )7ÚPersonalizationzsA Personalization defines who should receive an individual message and
    how that message should be handled.
    c                 C   s@   g | _ d| _g | _g | _d| _g | _g | _g | _d| _d| _	dS )z@Create an empty Personalization and initialize member variables.N)
Ú_tosÚ_from_emailÚ_ccsÚ_bccsÚ_subjectÚ_headersÚ_substitutionsÚ_custom_argsÚ_send_atÚ_dynamic_template_data©Úself© r   úI/tmp/pip-unpacked-wheel-7j3043uk/sendgrid/helpers/mail/personalization.pyÚ__init__   s    zPersonalization.__init__c                 C   sp   t |ƒ}|jdkr |  |¡ d S |jdkr8|  |¡ d S |jdkrP|  |¡ d S |jdkrd|| _d S tdƒ‚d S )NÚToÚCcZBccÚFromz(Please use a To, From, Cc or Bcc object.)ÚtypeÚ__name__Úadd_toÚadd_ccÚadd_bccÚ
from_emailÚ
ValueError)r   ÚemailZ
email_typer   r   r   Ú	add_email   s    






zPersonalization.add_emailc                    sh   g }|D ]Z}t |tƒr"|d  ¡ n|j ¡ ‰ t‡ fdd„|D ƒƒrt |tƒrP|n| ¡ }| |¡ q|S )Nr   c                 3   s   | ]}|d    ¡ ˆ kV  qdS )r   N)Úlower)Ú.0Zunique_recipient©Zrecipient_emailr   r   Ú	<genexpr>(   s    z9Personalization._get_unique_recipients.<locals>.<genexpr>)Ú
isinstanceÚdictr   r   ÚallÚgetÚappend)r   Z
recipientsZunique_recipientsZ	recipientZnew_unique_recipientr   r   r   Ú_get_unique_recipients#   s     ÿz&Personalization._get_unique_recipientsc                 C   s   |   | j¡S )zSA list of recipients for this Personalization.

        :rtype: list(dict)
        )r&   r   r   r   r   r   Útos1   s    zPersonalization.tosc                 C   s
   || _ d S ©N)r   ©r   Úvaluer   r   r   r'   9   s    c                 C   s€   |j r6t|j tƒr*|j D ]}|  |¡ qn|  |j ¡ |jrD|j| _|jrlt|jtƒr`|j| _n|j ¡ | _| j 	| ¡ ¡ dS )zTAdd a single recipient to this Personalization.

        :type email: Email
        N)
Úsubstitutionsr!   ÚlistÚadd_substitutionÚdynamic_template_dataÚsubjectÚstrr$   r   r%   )r   r   Úsubstitutionr   r   r   r   =   s    

zPersonalization.add_toc                 C   s   | j S r(   ©r   r   r   r   r   r   T   s    zPersonalization.from_emailc                 C   s
   || _ d S r(   r2   r)   r   r   r   r   X   s    c                 C   s   |  ¡ | _d S r(   )r$   r   ©r   r   r   r   r   Úset_from\   s    zPersonalization.set_fromc                 C   s   |   | j¡S )z`A list of recipients who will receive copies of this email.

        :rtype: list(dict)
        )r&   r   r   r   r   r   Úccs_   s    zPersonalization.ccsc                 C   s
   || _ d S r(   )r   r)   r   r   r   r5   g   s    c                 C   s   | j  | ¡ ¡ dS )z‹Add a single recipient to receive a copy of this email.

        :param email: new recipient to be CCed
        :type email: Email
        N)r   r%   r$   r3   r   r   r   r   k   s    zPersonalization.add_ccc                 C   s   |   | j¡S )zmA list of recipients who will receive blind carbon copies of this email.

        :rtype: list(dict)
        )r&   r   r   r   r   r   Úbccss   s    zPersonalization.bccsc                 C   s
   || _ d S r(   )r   r)   r   r   r   r6   {   s    c                 C   s   | j  | ¡ ¡ dS )z™Add a single recipient to receive a blind carbon copy of this email.

        :param email: new recipient to be BCCed
        :type email: Email
        N)r   r%   r$   r3   r   r   r   r      s    zPersonalization.add_bccc                 C   s   | j S )z¾The subject of your email (within this Personalization).

        Char length requirements, according to the RFC:
        https://stackoverflow.com/a/1592310

        :rtype: string
        ©r   r   r   r   r   r/   ‡   s    	zPersonalization.subjectc                 C   s
   || _ d S r(   r7   r)   r   r   r   r/   ’   s    c                 C   s   | j S )zTThe headers for emails in this Personalization.

        :rtype: list(dict)
        ©r   r   r   r   r   Úheaders–   s    zPersonalization.headersc                 C   s
   || _ d S r(   r8   r)   r   r   r   r9   ž   s    c                 C   s   | j  | ¡ ¡ dS )zSAdd a single Header to this Personalization.

        :type header: Header
        N)r   r%   r$   )r   Úheaderr   r   r   Ú
add_header¢   s    zPersonalization.add_headerc                 C   s   | j S )z]Substitutions to be applied within this Personalization.

        :rtype: list(dict)
        ©r   r   r   r   r   r+   ©   s    zPersonalization.substitutionsc                 C   s
   || _ d S r(   r<   r)   r   r   r   r+   ±   s    c                 C   s"   t |tƒs| ¡ }| j |¡ dS )zbAdd a new Substitution to this Personalization.

        :type substitution: Substitution
        N)r!   r"   r$   r   r%   )r   r1   r   r   r   r-   µ   s    
z Personalization.add_substitutionc                 C   s   | j S )ziThe CustomArgs that will be carried along with this Personalization.

        :rtype: list(dict)
        ©r	   r   r   r   r   Úcustom_args¿   s    zPersonalization.custom_argsc                 C   s
   || _ d S r(   r=   r)   r   r   r   r>   Ç   s    c                 C   s   | j  | ¡ ¡ dS )zVAdd a CustomArg to this Personalization.

        :type custom_arg: CustomArg
        N)r	   r%   r$   )r   Z
custom_argr   r   r   Úadd_custom_argË   s    zPersonalization.add_custom_argc                 C   s   | j S )zÍA unix timestamp allowing you to specify when you want emails from
        this Personalization to be delivered. Scheduling more than 72 hours in
        advance is forbidden.

        :rtype: int
        ©r
   r   r   r   r   Úsend_atÒ   s    zPersonalization.send_atc                 C   s
   || _ d S r(   r@   r)   r   r   r   rA   Ü   s    c                 C   s   | j S )z�Data for dynamic transactional template.
        Should be JSON-serializable structure.

        :rtype: JSON-serializable structure
        )r   r   r   r   r   r.   à   s    z%Personalization.dynamic_template_datac                 C   s   t |tƒs| ¡ }|| _d S r(   )r!   r"   r$   r   r)   r   r   r   r.   é   s    
c                 C   sž   i }dD ]"}t | |ƒ}|r|||dd… < qt | dƒ}|rB||d< dD ]}t | |ƒ}|rF|||< qFdD ]2}t | |ƒ}|rfi }|D ]}| |¡ |||< q€qf|S )z©
        Get a JSON-ready representation of this Personalization.

        :returns: This Personalization, ready for use in a request body.
        :rtype: dict
        )r'   r5   r6   Néÿÿÿÿr   Úfrom)r/   rA   r.   )r9   r+   r>   )ÚgetattrÚupdate)r   ZpersonalizationÚkeyr*   Ú
from_valueZ	prop_nameÚpropÚobjr   r   r   r$   ð   s(    





zPersonalization.getN)r   Ú
__module__Ú__qualname__Ú__doc__r   r   r&   Úpropertyr'   Úsetterr   r   r4   r5   r   r6   r   r/   r9   r;   r+   r-   r>   r?   rA   r.   r$   r   r   r   r   r      sh   


















	


r   N)Úobjectr   r   r   r   r   Ú<module>   ó    