o
    h;                     @   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   
   || _ d S N)
connection)selfr    r   R/var/www/html/ndineBlogger/venv/lib/python3.10/site-packages/wagtail/admin/mail.py__init__   s   
zOpenedConnection.__init__c                 C      | j   | j S r   )r   openr   r   r   r   	__enter__      
zOpenedConnection.__enter__c                 C   r   r   )r   close)r   typevalue	tracebackr   r   r   __exit__   r   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   F	auth_userN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   attach_alternativesend)	subjectmessagerecipient_list
from_emailkwargsr   multi_alt_kwargsmailr0   r   r   r   	send_mail    s6   







	r<   c              
      sR   fdd| D }|sdS d  d }d  d }d  d }dt i}|| t }t|l}	d	}
|D ]_}||d
< t|j  t|| }t|| }W d    n1 sZw   Y  i }t	t ddrnt|||d< zt
|||jgfd|	i| |
d7 }
W q4 ty   td||j Y q4w W d    n1 sw   Y  |
t|kS )Nc                    s0   g | ]}|j r|jrtt| d  r|qS _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_HTMLFr0   r      ,Failed to send notification email '%s' to %s)r   updater   r   r   wagtail_userprofileget_preferred_languager   striprA   r<   r@   	Exceptionlogger	exceptionlen)recipient_usersrG   extra_contextemail_recipientstemplate_subjecttemplate_texttemplate_htmlcontextr   open_connection
sent_countrE   email_subjectemail_contentr9   r   rF   r   send_notificationF   sX   


"rd   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. rI   c                 C   r   r   )valid_classes)r   rg   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)
isinstancerg   r   instancer9   r   r   r   
can_handle   s   zNotifier.can_handlec                 K      t  S )zJReturns a set of the final list of recipients for the notification messagesetri   r   r   r   get_valid_recipients   s   zNotifier.get_valid_recipientsc                 K   s   t t|jd S )N_)r	   r   r!   ri   r   r   r   get_template_base_prefix      z!Notifier.get_template_base_prefixc                 K   s   dt iS )Nr   r   ri   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 messagerK   textrq   rG   template_directory)r   rj   r9   template_baser]   r   r   r   get_template_set   s   zNotifier.get_template_setc                 K   s   t r   )NotImplementedErrorr   template_setr_   
recipientsr9   r   r   r   send_notifications   s   zNotifier.send_notificationsNc                 K   sj   | j |fi |sdS | j|fi |}|sd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)rk   ro   rx   rs   r}   )r   rj   r9   r|   r{   r_   r   r   r   __call__   s   zNotifier.__call__r   )r!   r"   r#   r$   rG   rv   r   rk   ro   rq   rs   rx   r}   r~   r   r   r   r   re      s    
re   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   rl   )zqGets the ideal set of recipient users, without accounting for notification preferences or missing email addressesrm   ri   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 r=   )r?   r@   rA   r   rB   rG   rC   r   r   r   	<setcomp>   s    z>EmailNotificationMixin.get_valid_recipients.<locals>.<setcomp>)r   ri   r   r   r   ro      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
        alternativesrJ   rK   rL   )r5   rt   htmlru   )r   rj   r9   rw   r\   r]   r^   r   r   r   rx      s   z'EmailNotificationMixin.get_template_setc              
   K   s4  t  }d}z~t|p}|D ]e}||d< t|j  t|d | }	t|d | }
W d    n1 s7w   Y  i }ttddrMt|d ||d< zt	|	|
|j
gfd	|i| |d
7 }W q tyr   td|	|j
 Y qw W d    n1 s}w   Y  W n ttfy   td Y nw |t|kS )Nr   rM   r5   rt   rN   Fr   r0   r   rO   rP   z3Mail connection error, notification sending skipped)r   r   r   rR   rS   r   rT   rA   r   r<   r@   rU   rV   rW   TimeoutErrorConnectionErrorrX   )r   r{   r_   r|   r9   r   ra   r`   rE   rb   rc   r   r   r   send_emails   sd   
(z"EmailNotificationMixin.send_emailsc                 K   s   | j |||fi |S r   )r   rz   r   r   r   r}     s   z)EmailNotificationMixin.send_notificationsN)	r!   r"   r#   r$   r   ro   rx   r   r}   r   r   r   r   r      s    1r   c                       s2   e Zd ZdZ fddZdef fddZ  ZS )BaseWorkflowStateEmailNotifierz>A base notifier to send email updates for WorkflowState eventsc                       t  tf d S r   )superr   r   r   	__class__r   r   r     rr   z'BaseWorkflowStateEmailNotifier.__init__workflow_statec                    sX   t  j|fi |}|j|d< |j|d< |d jj|d< t|d tr*|d j|d< |S )Nworkflowobject
model_namepage)	r   rs   r   content_object_metaverbose_namerh   r   specificr   r   r9   r_   r   r   r   rs   "  s   

z*BaseWorkflowStateEmailNotifier.get_context)r!   r"   r#   r$   r   r   rs   __classcell__r   r   r   r   r     s    r   c                   @   s"   e Zd ZdZdZdefddZdS )"WorkflowStateApprovalEmailNotifierzBA notifier to send email updates for WorkflowState approval eventsapprovedr   c                 K   2   | dd }t }|j}|d ur||kr|h}|S NrM   r2   rn   requested_byr   r   r9   triggering_userr|   r   r   r   r   r   1     z6WorkflowStateApprovalEmailNotifier.get_recipient_usersN)r!   r"   r#   r$   rG   r   r   r   r   r   r   r   ,  s    r   c                       2   e Zd ZdZdZdefddZ fddZ  ZS )#WorkflowStateRejectionEmailNotifierzCA notifier to send email updates for WorkflowState rejection eventsrejectedr   c                 K   r   r   r   r   r   r   r   r   @  r   z7WorkflowStateRejectionEmailNotifier.get_recipient_usersc                    s>   t  j|fi |}|jj}|j|d< ||d< | |d< |S )Ntask
task_statecomment)r   rs   current_task_stater   r   get_comment)r   r   r9   r_   r   r   r   r   rs   I  s   
z/WorkflowStateRejectionEmailNotifier.get_context	r!   r"   r#   r$   rG   r   r   rs   r   r   r   r   r   r   ;  s
    	r   c                       r   )$WorkflowStateSubmissionEmailNotifierzDA notifier to send email updates for WorkflowState submission events	submittedr   c                 K   sN   | dd }t j }ttdd}|rt jjdd}|r%|j|jd |S )NrM   ,WAGTAILADMIN_NOTIFICATION_INCLUDE_SUPERUSERSTis_superuserpk)	r2   r   objectsnonerA   r   filterexcluder   )r   r   r9   r   r|   include_superusersr   r   r   r   W  s   z8WorkflowStateSubmissionEmailNotifier.get_recipient_usersc                    s"   t  j|fi |}|j|d< |S )Nr   )r   rs   r   r   r   r   r   rs   d  s   
z0WorkflowStateSubmissionEmailNotifier.get_contextr   r   r   r   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defd	d
Z  Z	S )'BaseGroupApprovalTaskStateEmailNotifierzBA base notifier to send email updates for GroupApprovalTask eventsc                    r   r   )r   r   r   r   r   r   r   r   m  rr   z0BaseGroupApprovalTaskStateEmailNotifier.__init__c                    s*   t  j|fi |rt|jjtrdS dS )NTF)r   rk   rh   r   r   r
   ri   r   r   r   rk   p  s
   z2BaseGroupApprovalTaskStateEmailNotifier.can_handlec                    s\   t  j|fi |}|jj|d< |jj|d< |d jj|d< t|d t	r,|d j|d< |S )Nr   r   r   r   )
r   rs   r   r   r   r   r   r   rh   r   )r   r   r9   r_   r   r   r   rs   w  s   z3BaseGroupApprovalTaskStateEmailNotifier.get_contextr   c                 K   sh   | dd }t jj|jjj d}|}tt	dd}|r)t jjdd}||B }|r2|j
|jd}|S )NrM   )
groups__inr   Tr   r   )r2   r   r   r   r   r   groupsallrA   r   r   r   )r   r   r9   r   group_membersr|   r   
superusersr   r   r   r     s   z;BaseGroupApprovalTaskStateEmailNotifier.get_recipient_users)
r!   r"   r#   r$   r   rk   rs   r   r   r   r   r   r   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$   rG   r   r   r   r   r     s    r   r   )#loggingdjango.confr   django.contrib.authr   django.core.mailr   django.core.mail.messager   django.template.loaderr   django.utils.translationr   wagtail.coreutilsr	   wagtail.modelsr
   r   r   r   wagtail.users.modelsr   	getLoggerrV   r   r<   rd   re   r   r   r   r   r   r   r   r   r   r   r   <module>   s0    

&B9[
,