
    vg$                         d Z 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 dd
lmZ  ej$                  e      Z G d de      Z G d de      Z G d de      Zy)z>
threading.py

This module is used handle mail sent in thread
    N)Thread)messages)EmailMessage)render_to_string)ConfiguredEmailBackend)
Department)EmployeeWorkInformation)Ticketc                   2     e Zd ZdZd ZddZd fdZ xZS )TicketSendThread
    MailSend
    c                    t        j                  |        || _        || _        || _        |j
                  j                         | _        |j                         | _	        |j                         rdnd| _        |j                         }t        |t              rZ|j                  j                         r8|j                  j                         j!                         j"                  | _        y d | _        y y Nhttpshttp)r   __init__tickettyperequestassigned_toall	assigneesget_hosthost	is_secureprotocolget_raised_on_object
isinstancer   dept_managerfirstmanagerdepartment_manager)selfr   r   r   	raised_ons        &/var/www/horilla/helpdesk/threading.pyr   zTicketSendThread.__init__   s    	++//1$$&	#*#4#4#6F//1	i,%%))+*3*@*@*D*D*F*L*L*N*V*V'*.'	 -    c                    | j                   }| j                  }d}t               }|j                  }	| j                  r]	 | j                  j
                  j                  j                          d| j                  j
                  j                  j                   d}	|dk7  r| d| d| d}|D ]I  }
t        d||
||||d      }t        |||	|
j                  g|	g	      }d
|_        	 |j                          K y #  t        j                  t               Y |xY w#  t!        j                  | j                  d|
j                                 Y xY w)N# <>:///helpdesk/ticket-detail//(helpdesk/mail_templates/ticket_mail.htmllinkinstancer   r   subjectcontentr2   body
from_emailtoreply_tohtmlMail not sent to )r   r   r   $dynamic_from_email_with_display_namer   useremployee_getget_full_nameemailloggererror	Exceptionr   r   content_subtypesendr   )r#   r2   r3   
recipients	ticket_idr   r   r0   email_backenddisplay_email_name	recipienthtml_messager?   s                r%   
send_emailzTicketSendThread.send_email*   s\   yy==.0*OO<<((,(9(9(F(F(T(T(V'WWYZ^ZfZfZkZkZxZxZ~Z~Y  @A  &B" Zs4&(@1MD# 	I+:  )  (&&
L !!-OO$,-E %+E!

-	(Y'6LL$5i6M6M6O5P"Qs   AC9 &D9D3Ec                    t         |           | j                  dk(  r| j                  j                  }| j
                  }d| d}d}| j                  |||g| j                  j                         d}d}| j                  |||g| j                  j                         y | j                  dk(  r| j                  }| j                  j                  }| j
                  }| j                  j                         }|d   d	   }	|d   d
   d   d   }
|d   d
   d   d   }d}d|	 d| d|
 d}| j                  ||t        |      |hz  |hz  | j                  j                         y | j                  dk(  rn| j                  }| j                  j                  }| j
                  }d}d| j                  j                   d}| j                  ||t        |      |hz  |hz         y )Ncreatea  This is to inform you that a ticket has been raised on your department. Take the necessary actions to address the issue or request outlined in the ticket. Should you have any additional information or updates, please feel free to communicate directly with the .z(Ticket created raised on your departmentaX  This is to inform you that the ticket you created has been successfully logged into our system. The assigned team/individual will now take the necessary actions to address the issue or request outlined in the ticket. Should you have any additional information or updates, please feel free to communicate directly with the Support/Helpdesk team.zTicket created successfullystatus_changer   
updated_bychangesnewoldz)The Status of the Ticket has been updatedzRThis is to inform you that the status of the following ticket has been updated by z from z to zp. If you have any questions or require further information, feel free to reach out to the Support/Helpdesk team.deletezThe Ticket has been deletedz'This is to inform you that the Ticket "z" has been deleted. If you have any questions or require further information, feel free to reach out to the Support/Helpdesk team.)superrunr   r   employee_idr"   rK   idr   trackingsettitle)r#   ownerr!   content_managersubject_managercontent_ownersubject_ownerr   rY   rP   
new_status
old_statusr2   r3   	__class__s                 r%   rV   zTicketSendThread.runU   s   99 KK++E--G !e  fk  el  lm  nOHOOOO_wiX wM9MOOM=5'4;;>>R: 	7 YY/)IKK++E--G{{++-H!!\2J!!Y/259J!!Y/259JAGjkujvv|  ~H  }I  IM  NX  MY  YI  JGOO#i.E7":gY"F 	 YY("IKK++E--G3G?@Q@Q?R  SU  VGOOGWc)nw.F'.RSr&   )r(   returnN)__name__
__module____qualname____doc__r   rK   rV   __classcell__rc   s   @r%   r   r      s    /)V, ,r&   r   c                   *     e Zd ZdZd Zd fdZ xZS )AddAssigneeThreadr   c                     t        j                  |        || _        || _        || _        |j                         | _        |j                         rd| _        y d| _        y r   	r   r   r   rE   r   r   r   r   r   r#   r   r   rI   s       r%   r   zAddAssigneeThread.__init__   K    #$$&	#*#4#4#6Fr&   c                    t         |           d}d}| j                  }| j                  }t	               }|j
                  }| j                  r]	 | j                  j                  j                  j                          d| j                  j                  j                  j                   d}| d| d| j                  j                   d}| j                  D ]I  }t        d||||||d	      }	t        ||	||j                  g|g
      }
d|
_        	 |
j#                          K y #  Y |xY w#  t%        j&                  | j                  d|j                                 Y xY w)NPlease review the ticket details and take appropriate action accordingly. If you have any questions or require further information, feel free to reach out to the owner or the Support/Helpdesk team.z"You have been assigned to a Ticketr)   r*   r+   r,   r-   r.   r/   r4   r9   r:   )rU   rV   r   r   r   r;   r   r<   r=   r>   r?   r   rX   rE   r   r   rC   rD   r   rA   )r#   r3   r2   r   r   rG   rH   r0   rI   rJ   r?   rc   s              r%   rV   zAddAssigneeThread.run   sj    Z6yy==.0*OO<<(,(9(9(F(F(T(T(V'WWYZ^ZfZfZkZkZxZxZ~Z~Y  @A  &B" 3tf$<T[[^^<LAN 	I+:  )  (&&
L !!-OO$,-E %+E!

-	2LL$5i6M6M6O5P"Qs   AD# D*#D'*3Erd   rf   rg   rh   ri   r   rV   rj   rk   s   @r%   rm   rm          C* *r&   rm   c                   *     e Zd ZdZd Zd fdZ xZS )RemoveAssigneeThreadr   c                     t        j                  |        || _        || _        || _        |j                         | _        |j                         rd| _        y d| _        y r   ro   rp   s       r%   r   zRemoveAssigneeThread.__init__   rq   r&   c                    t         |           d}d}t               }|j                  }| j                  r]	 | j                  j
                  j                  j                          d| j                  j
                  j                  j                   d}| j                  }| j                  }| d| d| j                  j                   d}| j                  D ]I  }t        d||||||d	      }	t        ||	||j                  g|g
      }
d|
_        	 |
j#                          K y #  Y xY w#  t%        j&                  | j                  d|j                                 Y xY w)Nrs   z#You have been removed from a Ticketr)   r*   r+   r,   r-   r.   r/   r4   r9   r:   )rU   rV   r   r;   r   r<   r=   r>   r?   r   r   r   rX   rE   r   r   rC   rD   r   rA   )r#   r3   r2   rG   rH   r   r   r0   rI   rJ   r?   rc   s              r%   rV   zRemoveAssigneeThread.run   sj    Z7.0*OO<<(,(9(9(F(F(T(T(V'WWYZ^ZfZfZkZkZxZxZ~Z~Y  @A  &B" yy==3tf$<T[[^^<LAN 	I+:  )  (&&
L !!-OO$,-E %+E!

-	6LL$5i6M6M6O5P"Qs   AD# D*#D'*3Erd   rt   rk   s   @r%   rw   rw      ru   r&   rw   )ri   logging	threadingr   django.contribr   django.core.mailr   django.template.loaderr   base.backendsr   base.modelsr   employee.modelsr	   helpdesk.modelsr
   	getLoggerrf   r@   r   rm   rw    r&   r%   <module>r      sa      # ) 3 0 " 3 "			8	$kv k\7 7t76 7r&   