
    vg                         d dl Z d dlmZ d dlmZ d dlmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZ  e j                   e      Z G d	 d
e      Z G d de      Zy)    N)Thread)messages)EmailMessage)Q)render_to_string)gettext)ConfiguredEmailBackendc                   .     e Zd Zd ZddZd fdZ xZS )LeaveMailSendThreadc                     t        j                  |        || _        || _        || _        |j                         | _        |j                         rd| _        y d| _        y )Nhttpshttp)	r   __init__requestleave_requesttypeget_hosthost	is_secureprotocol)selfr   r   r   s       #/var/www/horilla/leave/threading.pyr   zLeaveMailSendThread.__init__   sL    *	$$&	#*#4#4#6F    c                    t               }|j                  }| j                  r]	 | j                  j                  j                  j                          d| j                  j                  j                  j                   d}| j                  }| j                  }|dk7  rt        |      }	|D ]L  }
|
st        d	|
||||d      }t        ||||
j                  g|g      }d|_        	 |j!                          N y #  t        j                  t               Y xY w#  t#        j                  | j                  d|
j                                 Y xY w)	Nz <>#z/base/mail_templates/leave_request_template.html)linkinstancer   r   subjectcontent)r   body
from_emailtoreply_tohtmlzMail not sent to )r	   $dynamic_from_email_with_display_namer   useremployee_getget_full_nameemailloggererror	Exceptionr   r   intr   r   content_subtypesendr   )r   r   r    
recipientsleave_request_idemail_backenddisplay_email_namer   r   r   	recipienthtml_messager*   s                r   
send_emailzLeaveMailSendThread.send_email   sL   .0*OO<<((,(9(9(F(F(T(T(V'WWYZ^ZfZfZkZkZxZxZ~Z~Y  @A  &B" yy==s"'(D# 	I/E $$- $$,#*#*
  %#%1!(01 )/%JJL/	(Y'<NN(9):Q:Q:S9T&Us   AC: 'D:D3Ec                    t         	|           | j                  dk(  r| j                  j                  }| j                  j                  j                         }d| d| d}d| }| j                  |||g| j                  j                         d| d}d}| j                  |||g| j                  j                         y | j                  dk(  rl| j                  j                  }| j                  j                  j                         }d	}d
| d}| j                  |||g| j                  j                         y | j                  dk(  rl| j                  j                  }| j                  j                  j                         }d}d| d}| j                  |||g| j                  j                         y | j                  dk(  r| j                  j                  }| j                  j                  j                         }d| d| d}d}| j                  |||g| j                  j                         d| d}d}| j                  |||g| j                  j                         y )Nr   zAThis is to inform you that a leave request has been requested by z. Take the necessary actions for the leave request. Should you have any additional information or updates, please feel free to communicate directly with the .z$Leave request has been requested by a  This is to inform you that the leave request you created has been successfully logged into our system. The manager will now take the necessary actions to address leave request. Should you have any additional information or updates, please feel free to communicate directly with the z"Leave request created successfullyapprovez0The Leave request has been successfully approvedzThis is to inform you that the leave request has been approved. If you have any questions or require further information, feel free to reach out to the rejectz#The Leave request has been rejectedzThis is to inform you that the leave request has been rejected. If you have any questions or require further information, feel free to reach out to the cancelzKThis is to inform you that a leave request has been requested to cancel by zLeave request cancellationa6  This is to inform you that a cancellation request created for your leave request has been successfully logged into our system. The manager will now take the necessary actions to address the leave request. Should you have any additional information or updates, please feel free to communicate directly with the z$Leave request cancellation requested)superrunr   r   employee_idget_reporting_managerr7   id)
r   ownerreporting_managercontent_managersubject_managercontent_ownersubject_ownerr   r    	__class__s
            r   r>   zLeaveMailSendThread.runC   s   99	!&&22E $ 2 2 > > T T V abgah  iF  GL  FM  MN  OO DUGLOOO"#""%%	 y  zK  yL  LM  NM@MOO}ugt7I7I7L7LV 	O YY)#&&22E $ 2 2 > > T T VHG q  rC  qD  DE  FGOOGWugt7I7I7L7LM@ 	= YY("&&22E $ 2 2 > > T T V;G q  rC  qD  DE  FGOOGWugt7I7I7L7LM. 	+ YY("&&22E $ 2 2 > > T T V klqkr  sP  QV  PW  WX  YO :OOO"#""%%	 U  Vg  Uh  hi  jMBMOO}ugt7I7I7L7L 	r   )r   returnN)__name__
__module____qualname__r   r7   r>   __classcell__rH   s   @r   r   r      s    C(T> >r   r   c                   ,     e Zd Zd Zd Zd fdZ xZS )LeaveClashThreadc                 <    t        j                  |        || _        y )N)r   r   r   )r   r   s     r   r   zLeaveClashThread.__init__   s    *r   c                    ddl m} 	 |j                  j                  | j                  j
                        j                  t        | j                  j                  j                  j                        t        | j                  j                  j                  j                        z  | j                  j                  | j                  j                        }|j                         S )Nr   LeaveRequest)rA   ).employee_id__employee_work_info__department_id)0employee_id__employee_work_info__job_position_id)start_date__lteend_date__gte)leave.modelsrU   objectsexcluder   rA   filterr   r?   employee_work_infodepartment_idjob_position_idend_date
start_datecount)r   rU   overlapping_requestss      r   count_leave_clashesz$LeaveClashThread.count_leave_clashes   s    -	  ,33;;!!$$  <  

&?C?Q?Q?]?]?p?p?~?~ AEASASA_A_ArAr  BC  BC !..77,,77  	
 	 $))++r   c                    ddl m} t        |           | j                  j                         }|j                  j                  t        |      t        |      z        }|D ]'  }| j                         |_
        |j                          ) y )Nr   rT   )start_date__in)end_date__in)rZ   rU   r=   r>   r   requested_datesr[   r]   r   re   leave_clashes_countsave)r   rU   datesleave_requests_to_updater   rH   s        r   r>   zLeaveClashThread.run   sz    -""224#/#7#7#>#>U#aU&;;$
  6 	!M040H0H0JM- 	!r   rI   )rK   rL   rM   r   re   r>   rN   rO   s   @r   rQ   rQ      s    +,,! !r   rQ   )logging	threadingr   django.contribr   django.core.mailr   django.db.modelsr   django.template.loaderr   django.utils.translationr   _base.backendsr	   	getLoggerrK   r+   r   rQ    r   r   <module>ry      sJ      # )  3 1 0			8	$r& rj'!v '!r   