U
    Vc                     @   s   d dl 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mZ d dlmZ d dlmZ d d	lmZ d
d Zdd Zdd Zdd Zdd Zedd Zdd Zdd Zdd Zdd ZdS )    N)SMTPException)settings)	send_mail)
strip_tags)SessionRequest)task)	EmailCall)SMSCallc                 C   s   t j}t jr| |}|S )N)r   
STATIC_URLSTATIC_ROOTbuild_absolute_uri)requestZ
static_url r   )/var/www/wikiazma_server/utils/pushers.pybuild_absolute_static_url   s    
r   c              
   C   sb   t tjtjdddd>}| }|dt| dt	|d|}|W  5 Q R  S Q R X dS )	z
    This function is responsible for building html content to sending in email for the users whom registering to our site.
    it takes a random and an email, and build a html content with these and returns the content.
    z<authenticate/templates/register_email_verification_code.htmlrutf8encoding__STATIC_URL__Z__REGISTER_VERIFICATION_CODE____TARGET_USER_EMAIL__N
openospathjoinr   BASE_DIRreadreplacer   str)r   randnumemailfZregister_verification_code_htmlcontentr   r   r   0register_verification_code_email_content_builder   s     r%   c              
   C   sb   t tjtjdddd>}| }|dt| dt	|d|}|W  5 Q R  S Q R X dS )	z
    This function is responsible for building html content to sending in email for the users whom changing or forgetting their passwords.
    it takes a randnum and an email, and build a html content with these and returns the content.
    zBauthenticate/templates/reset_password_email_verification_code.htmlr   r   r   r   Z$__RESET_PASSWORD_VERIFICATION_CODE__r   Nr   )r   r!   r"   r#   Z%reset_password_verification_code_htmlr$   r   r   r   6reset_password_verification_code_email_content_builder*   s      r&   c              
   C   s^   t tjtjdddd:}| }|dt| d|d|}|W  5 Q R  S Q R X dS )	z
    This function is responsible for building html content to sending in email of inviting another user to a project.
    it takes a link and an email, and build a html content with these and returns the content.
    z0collaborators/templates/invite_collaborator.htmlr   r   r   r   __INSTITUTE_NAME__Z__CALLER_FULL_NAME__N	r   r   r   r   r   r   r   r   r   )r   institute_nameZcaller_full_namer#   Zinvite_collaborator_htmlr$   r   r   r   )invite_collaborator_email_content_builder7   s      r*   c           
   
   C   s~   t tjtjddddZ}| }|dt| d|d|d|d	|d
|d|}	|	W  5 Q R  S Q R X d S )Nzexam/templates/score_exam.htmlr   r   r   r   Z__EXAMINEE_NAME__Z__EXAM_NAME__r'   Z	__SCORE__Z__MAX_POSITIVE_SCORE__Z__REPORT_CARD_URL__r(   )
r   examinee_name	exam_namer)   scoremax_positive_scorereport_card_urlr#   Zcorrection_htmlr$   r   r   r    correction_email_content_builderC   s,          r0   c              
   C   sr   t || dd}z(t| }td|tj|g| d}||_W n. tk
rd } zd|_||_W 5 d}~X Y nX |  dS )z
    This function used for sending email to clients. it takes a content and an email
    as its arguments and send the content to that email
    T)r"   messagesuccessZWikiAzma)html_messageFN)	r	   r   r   r   SENDER_EMAIL_ADDRESSresponser   r2   save)r$   r"   Z
email_callZplain_messager   er   r   r   send_html_emailL   s"    

r8   c                 C   s   t jrt jrt }tdd|d }|j}|t jdt jd}z(||}|j	
 r^d}nd}|j	}W n   d }d}Y nX tjj| |d ||d ||d	 d S )
NGETz$https://ippanel.com/patterns/pattern)paramsz*****TF
input_datapattern_code)phoner1   urlr<   r5   r2   )r   SMS_USERNAMESMS_PASSWORDr   r   preparer>   r   sendr$   isdigitr
   objectscreate)toZpatternt_paramssprepared_requestr>   r5   r2   r   r   r   send_sms_with_patternb   s>      
 



rI   c                 C   s4   t jt jt j| gtd|it jd}t| | d S )Nverification_codeusernamepasswordfromrF   r;   r<   )r   r?   r@   	SMS_PHONEjsondumpsVERIFICATION_PATTERN_CODErI   )rF   coder:   r   r   r   send_verification_code_sms~   s    rT   c              	   C   s6   t jt jt j| gt||dt jd}t| | d S )N)caller_namer)   rK   )r   r?   r@   rO   rP   rQ   INVITATION_PATTERN_CODErI   )rF   rU   r)   r:   r   r   r   send_sms_invite   s    rW   c                 C   s>   t jt jt j| gt||||||dt jd}t| | d S )N)r+   r,   r)   Ztotal_posetive_scorer.   r/   rK   )r   r?   r@   rO   rP   rQ   #SCORE_PUBLISHED_NOTIFY_PATTERN_CODErI   )rF   r+   r,   r)   Ztotal_positive_scorer.   r/   r:   r   r   r   send_sms_correction   s    
 
rY   )rP   r   smtplibr   django.confr   django.core.mailr   django.utils.htmlr   requestsr   r   Zzappa.asynchronousr   authenticate.modelsr	   r
   r   r%   r&   r*   r0   r8   rI   rT   rW   rY   r   r   r   r   <module>   s(   
	
