
9^c           @   sb   d  d l  Z  d  d l Z d  d l m Z d  d l m Z d  d l m Z d e f d     YZ d S(   iN(   t   settings(   t	   send_mail(   t   BaseCommandt   EmailNotificationCommandc           B   s;   e  Z d  Z d   Z d   Z d   Z d e d d  Z RS(   s   
    A BaseCommand subclass which adds sending email fuctionality.

    Subclasses will have an extra command line option ``--email-notification``
    and will be able to send emails by calling ``send_email_notification()``
    if SMTP host and port are specified in settings. The handling of the
    command line option is left to the management command implementation.
    Configuration is done in settings.EMAIL_NOTIFICATIONS dict.

    Configuration example::

        EMAIL_NOTIFICATIONS = {
            'scripts.my_script': {
                'subject': 'my_script subject',
                'body': 'my_script body',
                'from_email': 'from_email@example.com',
                'recipients': ('recipient0@example.com',),
                'no_admins': False,
                'no_traceback': False,
                'notification_level': 0,
                'fail_silently': False
            },
            'scripts.another_script': {
                ...
            },
            ...
        }

    Configuration explained:
        subject:            Email subject.
        body:               Email body.
        from_email:         Email from address.
        recipients:         Sequence of email recipient addresses.
        no_admins:          When True do not include ADMINS to recipients.
        no_traceback:       When True do not include traceback to email body.
        notification_level: 0: send email on fail, 1: send email always.
        fail_silently:      Parameter passed to django's send_mail().
    c      
   C   sN   | j  d d d d t d d d d | j  d	 d d d t d d
 d d d  S(   Ns   --email-notificationst   actiont
   store_truet   defaultt   destt   email_notificationst   helps%   Send email notifications for command.s   --email-exceptiont   email_exceptions"   Send email for command exceptions.(   t   add_argumentt   False(   t   selft   parser(    (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/email_notifications.pyt   add_arguments2   s    c         C   s,   d j  |  |  _ t t |   j |  d S(   s8   Overriden in order to access the command line arguments.t    N(   t   joint   argv_stringt   superR   t   run_from_argv(   R   t   argv(    (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/email_notifications.pyR   >   s    c         O   sg   y t  t |   j | |   WnC t k
 rb | d sI t |  d t  r\ |  j d t  n    n Xd S(   sJ  
        Overriden in order to send emails on unhandled exception.

        If an unhandled exception in ``def handle(self, *args, **options)``
        occurs and `--email-exception` is set or `self.email_exception` is
        set to True send an email to ADMINS with the traceback and then
        reraise the exception.
        R
   t   include_tracebackN(   R   R   t   executet	   Exceptiont   getattrR   t   send_email_notificationt   True(   R   t   argst   options(    (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/email_notifications.pyR   C   s    	i   c      	   C   s  | d k	 rB y t j j | i   } WqH t k
 r> i  } qH Xn i  } | rw | j d d  rw |  j j d  GHd S| j d d  } |  j j d  d } | j d	 d
 |  } | r| j d t	  rz? t
 j   \ } }	 }
 d j t j |
   } | d | 7} Wd ~
 Xn  | j d t j  } t | j d g    } | j d t	  si| j t j  n  | s| d k r|  j j d  GHn  d St | | | | d | j d t  d S(   s   
        Send email notifications.

        Reads settings from settings.EMAIL_NOTIFICATIONS dict, if available,
        using ``notification_id`` as a key or else provides reasonable
        defaults.
        t   notification_leveli    s%   Exiting, not in 'notify always' mode.Nt   subjects%   Django extensions email notification.t   .it   bodys$   Reporting execution of command: '%s't   no_tracebackt    s   

Traceback:

%s
t
   from_emailt
   recipientst	   no_adminss   No email recipients available.t   fail_silently(   t   NoneR    t   EMAIL_NOTIFICATIONSt   gett   AttributeErrort   stylet   ERRORt
   __module__t   splitR   t   syst   exc_infoR   t	   tracebackt	   format_tbt   DEFAULT_FROM_EMAILt   listt   extendt   ADMINSR   R   (   R   t   notification_idR   t	   verbosityt   email_settingsR   t   command_nameR!   t   exc_typet	   exc_valuet   exc_tracebackt   trbR$   R%   (    (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/email_notifications.pyR   S   s<    	N(	   t   __name__R.   t   __doc__R   R   R   R(   R   R   (    (    (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/email_notifications.pyR   
   s
   &			(	   R0   R2   t   django.confR    t   django.core.mailR   t   django.core.managementR   R   (    (    (    sR   /tmp/pip-unpacked-wheel-a0M10Y/django_extensions/management/email_notifications.pyt   <module>   s
   