o
    /h                     @   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_trueF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   k/var/www/html/env_mimamsha/lib/python3.10/site-packages/django_extensions/management/email_notifications.pyadd_arguments2   s   
z&EmailNotificationCommand.add_argumentsc                    s   d || _t | dS )z8Overriden in order to access the command line arguments. N)joinargv_stringsuperrun_from_argv)r   argv	__class__r   r   r   B   s   z&EmailNotificationCommand.run_from_argvc                    sJ   zt  j|i | W dS  ty$   |d st| ddr#| jdd  w )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optionsr   r   r   r   G   s   	z EmailNotificationCommand.executeNF   c              	   C   s@  |durz	t j|i }W n ty   i }Y nw i }|s-|dds-t| jd dS |dd}| jdd }|d	d
| }|rf|ddsfzt	
 \}}	}
dt|
}|d| 7 }W ~
n~
w |dt j}t|dg }|dds|t j |s|dkrt| jd dS t|||||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.
        Nnotification_levelr   z%Exiting, not in 'notify always' mode.subjectz%Django extensions email notification..bodyz$Reporting execution of command: '%s'no_tracebackF z

Traceback:

%s

from_email
recipients	no_adminszNo email recipients available.fail_silentlyT)r-   )r   EMAIL_NOTIFICATIONSgetAttributeErrorprintstyleERROR
__module__splitsysexc_infor   	traceback	format_tbDEFAULT_FROM_EMAILlistextendADMINSr   )r   notification_idr   	verbosityemail_settingsr$   command_namer'   exc_type	exc_valueexc_tracebacktrbr*   r+   r   r   r   r   W   sH   

z0EmailNotificationCommand.send_email_notification)NFr"   )	__name__r4   __qualname____doc__r   r   r   r   __classcell__r   r   r   r   r   
   s    'r   )	r6   r8   django.confr   django.core.mailr   django.core.managementr   r   r   r   r   r   <module>   s   