a
    Dg;                     @   s.  d dl 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mZmZmZ d d
lmZ e dZG dd dZd"ddZdd ZG dd dZG dd dZG dd deeZG dd deZG dd deZG dd deZ G dd deeZ!G d d! d!e!Z"dS )#    Nsettings)get_user_model)get_connection)EmailMultiAlternatives)render_to_string)override)camelcase_to_underscore)AbstractGroupApprovalTaskPage	TaskStateWorkflowState)UserProfilezwagtail.adminc                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	OpenedConnectionzSContext manager for mail connections to ensure they are closed when manually openedc                 C   s
   || _ d S N)
connection)selfr    r   Q/var/www/lab.imftr.de/x/nb_venv/lib/python3.9/site-packages/wagtail/admin/mail.py__init__   s    zOpenedConnection.__init__c                 C   s   | j   | j S r   )r   openr   r   r   r   	__enter__   s    
zOpenedConnection.__enter__c                 C   s   | j   | j S r   )r   close)r   typevalue	tracebackr   r   r   __exit__   s    
zOpenedConnection.__exit__N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r      s   r   c           	      K   s   |s,t tdrtj}nt tdr(tj}nd}|ddp\t|dd|dd|d	dd
}|ddi|dd|dd|ddd}t| |||fi |}|dd}|r||d | S )z
    Wrapper around Django's EmailMultiAlternatives as done in send_mail().
    Custom from_email handling and special Auto-Submitted header.
    $WAGTAILADMIN_NOTIFICATION_FROM_EMAILDEFAULT_FROM_EMAILzwebmaster@localhostr   FZ	auth_userNZauth_passwordfail_silently)usernamepasswordr$   zAuto-Submittedzauto-generatedbccccreply_to)r   headersr'   r(   r)   html_messagez	text/html)	hasattrr   r"   r#   getr   r   Zattach_alternativesend)	subjectmessageZrecipient_listZ
from_emailkwargsr   Zmulti_alt_kwargsmailr+   r   r   r   	send_mail    s6    







	r3   c              
      sX   fdd| D }|sdS d  d }d  d }d  d }dt i}|| t }t|}	d	}
|D ]}||d
< t|j , t|| }t|| }W d    n1 s0    Y  i }t	t ddrt|||d< z(t
|||jgfd|	i| |
d7 }
W qh ty(   td||j Y qh0 qhW d    n1 sB0    Y  |
t|kS )Nc                    s0   g | ](}|j r|jrtt| d  r|qS Z_notifications)	is_activeemailgetattrr   get_for_user.0	recipientnotificationr   r   
<listcomp>H   s   z%send_notification.<locals>.<listcomp>Twagtailadmin/notifications/_subject.txt.txt.htmlr   r   user"WAGTAILADMIN_NOTIFICATION_USE_HTMLFr+   r      ,Failed to send notification email '%s' to %s)r   updater   r   r   wagtail_userprofileget_preferred_languager   stripr7   r3   r6   	Exceptionlogger	exceptionlen)Zrecipient_usersr=   Zextra_contextZemail_recipientstemplate_subjecttemplate_texttemplate_htmlcontextr   open_connection
sent_countr;   email_subjectemail_contentr1   r   r<   r   send_notificationF   sP    


,,rW   c                   @   sZ   e Zd ZdZdZdZdd Zdd Zdd	 Zd
d Z	dd Z
dd Zdd ZdddZdS )NotifierzGeneric class for sending event notifications: callable, intended to be connected to a signal to send
    notifications using rendered templates. r?   c                 C   s
   || _ d S r   )valid_classes)r   rZ   r   r   r   r      s    zNotifier.__init__c                 K   s   t || jS )zcReturns True if the Notifier can handle sending the notification from the instance, otherwise False)
isinstancerZ   r   instancer1   r   r   r   
can_handle   s    zNotifier.can_handlec                 K   s   t  S )zJReturns a set of the final list of recipients for the notification messagesetr\   r   r   r   get_valid_recipients   s    zNotifier.get_valid_recipientsc                 K   s   t t|jd S )N_)r	   r   r   r\   r   r   r   get_template_base_prefix   s    z!Notifier.get_template_base_prefixc                 K   s   dt iS )Nr   r   r\   r   r   r   get_context   s    zNotifier.get_contextc                 K   s&   |  || j }| j| d }d|iS )zSReturn a dictionary of template paths for the templates: by default, a text messagerA   textrc   r=   template_directory)r   r]   r1   template_baserP   r   r   r   get_template_set   s    zNotifier.get_template_setc                 K   s   t d S r   )NotImplementedErrorr   template_setrR   
recipientsr1   r   r   r   send_notifications   s    zNotifier.send_notificationsNc                 K   sj   | j |fi |sdS | j|fi |}|s0dS | j|fi |}| j|fi |}| j|||fi |S )zSend notifications from an instance (intended to be the signal sender), returning True if all sent correctly
        and False otherwiseFT)r^   ra   ri   rd   rn   )r   r]   r1   rm   rl   rR   r   r   r   __call__   s    zNotifier.__call__)N)r   r   r    r!   r=   rg   r   r^   ra   rc   rd   ri   rn   ro   r   r   r   r   rX      s   
rX   c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )EmailNotificationMixinz1Mixin for sending email notifications upon eventsc                 K   s   t  S )zqGets the ideal set of recipient users, without accounting for notification preferences or missing email addressesr_   r\   r   r   r   get_recipient_users   s    z*EmailNotificationMixin.get_recipient_usersc                    s     fdd j |fi |D S )zFilters notification recipients to those allowing the notification type on their UserProfile, and those
        with an email addressc                    s6   h | ].}|r|j r|jrtt| jd  r|qS r4   )r5   r6   r7   r   r8   r=   r9   r   r   r   	<setcomp>   s   z>EmailNotificationMixin.get_valid_recipients.<locals>.<setcomp>)rq   r\   r   r   r   ra      s    
z+EmailNotificationMixin.get_valid_recipientsc                 K   sF   |  || j }| j| d }| j| d }| j| d }|||dS )zxReturn a dictionary of template paths for the templates for the email subject and the text and html
        alternativesr@   rA   rB   )r/   re   htmlrf   )r   r]   r1   rh   rO   rP   rQ   r   r   r   ri      s    z'EmailNotificationMixin.get_template_setc              
   K   s8  t  }d}zt|}|D ]}||d< t|j 4 t|d | }	t|d | }
W d    n1 sn0    Y  i }ttddrt|d ||d< z(t	|	|
|j
gfd	|i| |d
7 }W q ty   td|	|j
 Y q0 qW d    n1 s0    Y  W n" ttfy*   td Y n0 |t|kS )Nr   rC   r/   re   rD   Frs   r+   r   rE   rF   z3Mail connection error, notification sending skipped)r   r   r   rH   rI   r   rJ   r7   r   r3   r6   rK   rL   rM   TimeoutErrorConnectionErrorrN   )r   rl   rR   rm   r1   r   rT   rS   r;   rU   rV   r   r   r   send_emails   sP    
&.z"EmailNotificationMixin.send_emailsc                 K   s   | j |||fi |S r   )rv   rk   r   r   r   rn     s    z)EmailNotificationMixin.send_notificationsN)	r   r   r    r!   rq   ra   ri   rv   rn   r   r   r   r   rp      s   1rp   c                       s2   e Zd ZdZ fddZed fddZ  ZS )BaseWorkflowStateEmailNotifierz>A base notifier to send email updates for WorkflowState eventsc                    s   t  tf d S r   )superr   r   r   	__class__r   r   r     s    z'BaseWorkflowStateEmailNotifier.__init__workflow_statec                    sX   t  j|fi |}|j|d< |j|d< |d jj|d< t|d trT|d j|d< |S )Nworkflowobject
model_namepage)	rx   rd   r}   content_object_metaverbose_namer[   r   specificr   r|   r1   rR   ry   r   r   rd   "  s    

z*BaseWorkflowStateEmailNotifier.get_context)r   r   r    r!   r   r   rd   __classcell__r   r   ry   r   rw     s   rw   c                   @   s"   e Zd ZdZdZedddZdS )"WorkflowStateApprovalEmailNotifierzBA notifier to send email updates for WorkflowState approval eventsZapprovedr{   c                 K   s2   | dd }t }|j}|d ur.||kr.|h}|S NrC   r-   r`   requested_byr   r|   r1   triggering_userrm   r   r   r   r   rq   1  s    z6WorkflowStateApprovalEmailNotifier.get_recipient_usersN)r   r   r    r!   r=   r   rq   r   r   r   r   r   ,  s   r   c                       s2   e Zd ZdZdZedddZ fddZ  ZS )#WorkflowStateRejectionEmailNotifierzCA notifier to send email updates for WorkflowState rejection eventsZrejectedr{   c                 K   s2   | dd }t }|j}|d ur.||kr.|h}|S r   r   r   r   r   r   rq   @  s    z7WorkflowStateRejectionEmailNotifier.get_recipient_usersc                    s>   t  j|fi |}|jj}|j|d< ||d< | |d< |S )Ntask
task_statecomment)rx   rd   Zcurrent_task_stater   r   Zget_comment)r   r|   r1   rR   r   ry   r   r   rd   I  s    
z/WorkflowStateRejectionEmailNotifier.get_context	r   r   r    r!   r=   r   rq   rd   r   r   r   ry   r   r   ;  s   	r   c                       s2   e Zd ZdZdZedddZ fddZ  ZS )$WorkflowStateSubmissionEmailNotifierzDA notifier to send email updates for WorkflowState submission events	submittedr{   c                 K   sN   | dd }t j }ttdd}|r8t jjdd}|rJ|j|jd |S )NrC   ,WAGTAILADMIN_NOTIFICATION_INCLUDE_SUPERUSERSTZis_superuserpk)	r-   r   objectsnoner7   r   filterexcluder   )r   r|   r1   r   rm   include_superusersr   r   r   rq   W  s    z8WorkflowStateSubmissionEmailNotifier.get_recipient_usersc                    s"   t  j|fi |}|j|d< |S )Nr   )rx   rd   r   r   ry   r   r   rd   d  s    
z0WorkflowStateSubmissionEmailNotifier.get_contextr   r   r   ry   r   r   R  s   r   c                       sF   e Zd ZdZ fddZ fddZ fddZedd	d
Z  Z	S )'BaseGroupApprovalTaskStateEmailNotifierzBA base notifier to send email updates for GroupApprovalTask eventsc                    s   t  tf d S r   )rx   r   r   r   ry   r   r   r   m  s    z0BaseGroupApprovalTaskStateEmailNotifier.__init__c                    s*   t  j|fi |r&t|jjtr&dS dS )NTF)rx   r^   r[   r   r   r
   r\   ry   r   r   r^   p  s
    z2BaseGroupApprovalTaskStateEmailNotifier.can_handlec                    s\   t  j|fi |}|jj|d< |jj|d< |d jj|d< t|d t	rX|d j|d< |S )Nr   r~   r   r   )
rx   rd   r   r   r|   r   r   r   r[   r   )r   r   r1   rR   ry   r   r   rd   w  s    z3BaseGroupApprovalTaskStateEmailNotifier.get_context)r   c                 K   sh   | dd }t jj|jjj d}|}tt	dd}|rRt jjdd}||B }|rd|j
|jd}|S )NrC   )Z
groups__inr   Tr   r   )r-   r   r   r   r   r   groupsallr7   r   r   r   )r   r   r1   r   Zgroup_membersrm   r   Z
superusersr   r   r   rq     s    z;BaseGroupApprovalTaskStateEmailNotifier.get_recipient_users)
r   r   r    r!   r   r^   rd   r   rq   r   r   r   ry   r   r   j  s
   	r   c                   @   s   e Zd ZdZdZdS )-GroupApprovalTaskStateSubmissionEmailNotifierzHA notifier to send email updates for GroupApprovalTask submission eventsr   N)r   r   r    r!   r=   r   r   r   r   r     s   r   )N)#loggingZdjango.confr   Zdjango.contrib.authr   Zdjango.core.mailr   Zdjango.core.mail.messager   Zdjango.template.loaderr   Zdjango.utils.translationr   Zwagtail.coreutilsr	   Zwagtail.modelsr
   r   r   r   Zwagtail.users.modelsr   	getLoggerrL   r   r3   rW   rX   rp   rw   r   r   r   r   r   r   r   r   r   <module>   s.   

&B9[,
