
    jg                     N    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e      Zy)    N)settings)	send_mail)BaseCommandc                   :     e Zd ZdZd Z fdZ fdZddZ x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                 \    |j                  ddddd       |j                  ddddd	       y )
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parsers     k/var/www/html/Stanley/venv/lib/python3.12/site-packages/django_extensions/management/email_notifications.pyadd_argumentsz&EmailNotificationCommand.add_arguments2   sJ    3#/$)!6!H	 	 	J
 	/#/$)!2!E	 	 	G    c                 P    dj                  |      | _        t        |   |       y)z8Overriden in order to access the command line arguments. N)joinargv_stringsuperrun_from_argv)r   argv	__class__s     r   r   z&EmailNotificationCommand.run_from_argv>   s     88D>d#r   c                     	 t        |   |i | y# t        $ r& |d   st        | dd      r| j	                  d        w xY 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   s      r   r    z EmailNotificationCommand.executeC   sQ    	GOT-W- 	()WT;Le-T,,t,D	s	    /Ac           
         |"	 t         j                  j                  |i       }ni }|s7|j                  dd      s%t	        | j
                  j                  d             y|j                  dd      }| j                  j                  d      d   }|j                  d	d
|z        }|rX|j                  dd      sF	 t        j                         \  }}	}
dj                  t        j                  |
            }|d|z  z  }~
|j                  dt         j                        }t        |j                  dg             }|j                  dd      s|j!                  t         j"                         |s*|dkD  r$t	        | j
                  j                  d             yt%        |||||j                  dd             y# t        $ r i }Y w xY w# ~
w xY w)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)r1   )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/   s                 r   r#   z0EmailNotificationCommand.send_email_notificationS   s    &$!)!=!=!A!A/SU!V  N !););<PRS)T$**""#JKL !$$Y0WX,,S1"5!!2\A
 ^%7%7%N"58\\^2)]ggi11-@A0366! $''h6Q6QR
 .,,\2>?
!!+u5hoo.1}djj&&'GHI 	'4Z . 2 2?D I	KY " $!#$6 "s    F8 :AG
 8GG
G)NF   )	__name__r8   __qualname____doc__r   r   r    r#   __classcell__)r   s   @r   r   r   
   s    %N
G$
 9Kr   r   )	r:   r<   django.confr   django.core.mailr   django.core.managementr   r    r   r   <module>rS      s$    
    & .BK{ BKr   