ó
rÁìVc           @   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 m Z d d	 l m Z d d
 l m Z m Z m Z m Z d d l m Z m Z m Z m Z d d l m Z m Z m Z m Z m Z d d l m  Z  y d  d l! m" Z" e" j# Z# Wn) e$ k
 rFd  d l% Z% e% j% j# Z# n Xe  d ƒ Z& d d d d d d d d d d d e( e) d d „ Z* d d d d d d d d d d d e( d e) d d d d d „ Z+ d „  Z, d „  Z- d d d „ Z. e) d d „ Z/ d S(   iÿÿÿÿN(   t   Pool(   t   settings(   t   ValidationError(   t
   connection(   t   Q(   t   Contextt   Templatei   (   t   connections(   t   Emailt   EmailTemplatet   PRIORITYt   STATUS(   t   get_available_backendst   get_batch_sizet   get_log_levelt   get_sending_order(   t   get_email_templatet   parse_emailst   parse_priorityt   split_emailst   create_attachments(   t   setup_loghandlers(   t   timezonet   INFOt    c         C   s¯  t  | ƒ } | t j k r! d n t j } | d k r? g  } n  | d k rT g  } n  | d k ri g  } n  | d k r~ d } n  | rÒ t d |  d | d | d | d | d |	 d | d	 | d
 | d |
 d | ƒ } nÆ |
 rö |
 j } |
 j } |
 j	 } n  t
 | pi  ƒ } t | ƒ j | ƒ } t | ƒ j | ƒ } t | ƒ j | ƒ } t d |  d | d | d | d | d | d | d | d |	 d | d	 | d | ƒ } | r«| j ƒ  n  | S(   s•   
    Creates an email from supplied keyword arguments. If template is
    specified, email subject and content will be rendered during delivery.
    R   t
   from_emailt   tot   cct   bcct   scheduled_timet   headerst   priorityt   statust   contextt   templatet   backend_aliast   subjectt   messaget   html_messageN(   R   R
   t   nowt   NoneR   t   queuedR   R$   t   contentt   html_contentR   R   t   rendert   save(   t   sendert
   recipientsR   R   R$   R%   R&   R!   R   R   R"   R   t   render_on_deliveryt   committ   backendR    t   emailt   _context(    (    si   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_post_office-2.0.6-py2.7.egg/post_office/mail.pyt   create   sR    							c         C   sw  y t  |  ƒ }  Wn& t k
 r8 } t d | j ƒ ‚ n Xy t  | ƒ } Wn& t k
 rq } t d | j ƒ ‚ n Xy t  | ƒ } Wn& t k
 rª } t d | j ƒ ‚ n X| d  k rÃ t j } n  t |	 ƒ }	 | d  k rç t ƒ  } n  | s#|	 t j	 k rt
 d ƒ ‚ n  |
 r#t
 d ƒ ‚ q#n  | rÂ| r>t
 d ƒ ‚ n  | rSt
 d ƒ ‚ n  | rht
 d ƒ ‚ n  t | t ƒ r°| } | r¿| j | k r­| j j d	 | ƒ } q­q¿qÂt | | ƒ } n  | rð| t ƒ  j ƒ  k rðt
 d
 | ƒ ‚ n  t | |  | | | | | | | | | |	 | d | d | ƒ} |
 rQt |
 ƒ }
 | j j |
 Œ  n  |	 t j	 k rs| j d | ƒ n  | S(   Ns   recipients: %ss   c: %ss   bcc: %ss/   send_many() can't be used with priority = 'now's&   Can't add attachments with send_many()s9   You can't specify both "template" and "subject" argumentss9   You can't specify both "template" and "message" argumentss>   You can't specify both "template" and "html_message" argumentst   languages   %s is not a valid backend aliasR1   R2   t	   log_level(   R   R   R%   R(   R   t   DEFAULT_FROM_EMAILR   R   R
   R'   t
   ValueErrort
   isinstanceR	   R6   t   translated_templatest   getR   R   t   keysR5   R   t   attachmentst   addt   dispatch(   R/   R.   R"   R!   R$   R%   R&   R   R   R   R>   R0   R7   R1   R   R   R6   R2   t   eR3   (    (    si   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_post_office-2.0.6-py2.7.egg/post_office/mail.pyt   send^   s\    c         C   sD   g  } x' |  D] } | j  t d t |  ƒ q Wt j j | ƒ d S(   sè   
    Similar to mail.send(), but this function accepts a list of kwargs.
    Internally, it uses Django's bulk_create command for efficiency reasons.
    Currently send_many() can't be used to send emails with priority = 'now'.
    R1   N(   t   appendRB   t   FalseR   t   objectst   bulk_create(   t   kwargs_listt   emailst   kwargs(    (    si   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_post_office-2.0.6-py2.7.egg/post_office/mail.pyt	   send_many¤   s    c           C   s]   t  j j d t j ƒ j d ƒ j t d t ƒ  ƒ t d d ƒ Bƒ j	 t
 ƒ  Œ  j d ƒ t ƒ   S(   s   
    Returns a list of emails that should be sent:
     - Status is queued
     - Has scheduled_time lower than the current time or None
    R    R"   t   scheduled_time__lteR   R>   N(   R   RE   t   filterR   R)   t   select_relatedR   R'   R(   t   order_byR   t   prefetch_relatedR   (    (    (    si   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_post_office-2.0.6-py2.7.egg/post_office/mail.pyt
   get_queued°   s    	"c         C   s8  t  ƒ  } d \ } } t | ƒ } t j d | |  f ƒ | d k rP t ƒ  } n  | r| |  k  rk | }  n  |  d k r˜ t | d t d | ƒ\ } } qt | |  ƒ } t	 |  ƒ } | j
 t | ƒ } t g  | D] }	 |	 d ^ qÏ ƒ } t g  | D] }	 |	 d ^ qò ƒ } n  d | | | f }
 t j |
 ƒ | | f S(	   sR   
    Sends out all queued mails that has scheduled_time less than now or None
    i    s,   Started sending %s emails with %s processes.i   t   uses_multiprocessingR7   s'   %s emails attempted, %s sent, %s failed(   i    i    N(   RP   t   lent   loggert   infoR(   R   t
   _send_bulkRD   R   R    t   mapt   sum(   t	   processesR7   t   queued_emailst
   total_sentt   total_failedt   total_emailt   email_listst   poolt   resultst   resultR%   (    (    si   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_post_office-2.0.6-py2.7.egg/post_office/mail.pyt   send_queued¼   s2    				#&
c   	      C   s9  | r t  j ƒ  n  | d  k r+ t ƒ  } n  d \ } } t |  ƒ } t j d | ƒ y{ xt |  D]l } | j d | d t ƒ } | t	 j
 k r¬ | d 7} t j d | j ƒ q^ | d 7} t j d | j ƒ q^ WWn9 t k
 r
} t j | d t j ƒ  d	 i d
 d 6ƒn Xt j ƒ  t j d | | | f ƒ | | f S(   Ni    s"   Process started, sending %s emailsR7   t   disconnect_after_deliveryi   s   Successfully sent email #%ds   Failed to send email #%dt   exc_infot   extraiô  t   status_codes2   Process finished, %s attempted, %s sent, %s failed(   i    i    (   t   db_connectiont   closeR(   R   RR   RS   RT   R@   RD   R   t   sentt   debugt   idt	   Exceptiont   errort   sysRc   R   (	   RH   RQ   R7   t
   sent_countt   failed_countt   email_countR3   R    RA   (    (    si   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_post_office-2.0.6-py2.7.egg/post_office/mail.pyRU   ã   s,    	

*
	(0   Rm   t   multiprocessingR    t   django.confR   t   django.core.exceptionsR   t	   django.dbR   Rf   t   django.db.modelsR   t   django.templateR   R   R   t   modelsR   R	   R
   R   R   R   R   R   t   utilsR   R   R   R   R   t   logutilsR   t   django.utilsR   R'   t   ImportErrort   datetimeRS   R(   RD   t   TrueR5   RB   RJ   RP   Ra   RU   (    (    (    si   /opt/seafile/seafile-server-5.1.3/seahub/thirdpart/django_post_office-2.0.6-py2.7.egg/post_office/mail.pyt   <module>   s>   ""(=	B		'