3
g                 @   sH   d dl Z d dlZd dlmZ d dlmZ d dlmZ G dd deZdS )    N)settings)	send_mail)BaseCommandc                   s>   e Zd ZdZdd Z fddZ fddZdddZ  ZS )EmailNotificationCommanda   
    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   s,   |j dddddd |j ddddd	d d S )
Nz--email-notifications
store_trueFZemail_notificationsz%Send email notifications for command.)actiondefaultdesthelpz--email-exceptionemail_exceptionz"Send email for command exceptions.)add_argument)selfparser r   n/var/www/tester-filtro-web/env/lib/python3.6/site-packages/django_extensions/management/email_notifications.pyadd_arguments2   s    z&EmailNotificationCommand.add_argumentsc                s   dj || _t j| dS )z8Overriden in order to access the command line arguments. N)joinZargv_stringsuperrun_from_argv)r   argv)	__class__r   r   r   >   s    z&EmailNotificationCommand.run_from_argvc                sN   yt  j|| W n6 tk
rH   |d s6t| ddrB| jdd  Y nX dS )aJ  
        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   FT)include_tracebackN)r   execute	Exceptiongetattrsend_email_notification)r   argsoptions)r   r   r   r   C   s    	z EmailNotificationCommand.executeNF   c             C   sN  |dk	r6yt jj|i }W q: tk
r2   i }Y q:X ni }| rb|jdd rbt| jjd dS |jdd}| jjdd }|jd	d
| }|r|jdd rz.t	j
 \}}	}
djtj|
}|d| 7 }W d~
X |jdt j}t|jdg }|jdds|jt j |s0|dkr,t| jjd dS t|||||jddd dS )z
        Send email notifications.

        Reads settings from settings.EMAIL_NOTIFICATIONS dict, if available,
        using ``notification_id`` as a key or else provides reasonable
        defaults.
        NZnotification_levelr   z%Exiting, not in 'notify always' mode.subjectz%Django extensions email notification..r   bodyz$Reporting execution of command: '%s'Zno_tracebackF z

Traceback:

%s

from_email
recipientsZ	no_adminszNo email recipients available.fail_silentlyT)r&   )r   ZEMAIL_NOTIFICATIONSgetAttributeErrorprintstyleERROR
__module__splitsysexc_infor   	traceback	format_tbZDEFAULT_FROM_EMAILlistextendZADMINSr   )r   Znotification_idr   	verbosityZemail_settingsr    command_namer"   exc_type	exc_valueexc_tracebackZtrbr$   r%   r   r   r   r   S   s<    	


z0EmailNotificationCommand.send_email_notification)NFr   )	__name__r-   __qualname____doc__r   r   r   r   __classcell__r   r   )r   r   r   
   s
   &r   )	r/   r1   Zdjango.confr   Zdjango.core.mailr   Zdjango.core.managementr   r   r   r   r   r   <module>   s
   