o
    uvXj,  ã                   @   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   úm/var/www/html/myproject/venv/lib/python3.10/site-packages/django_extensions/management/email_notifications.pyÚadd_arguments2   s   ü
üz&EmailNotificationCommand.add_argumentsc                    s    d  |¡| _tt| ƒ |¡ dS )z8Overriden in order to access the command line arguments.ú N)ÚjoinÚargv_stringÚsuperr   Úrun_from_argv)r   Úargv©Ú	__class__r   r   r   >   s   z&EmailNotificationCommand.run_from_argvc                    sN   zt 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   r   ÚexecuteÚ	ExceptionÚgetattrÚsend_email_notification)r   ÚargsÚoptionsr   r   r   r   C   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r�t| 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   S   sB   	ÿþ


ÿ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   