o
    }ºi  ã                   @   sX   d dl Z d dlmZmZ d dlmZ dd„ Zdd„ Zdd	„ Zd dlZd dlZd
d„ Z	dS )é    N)ÚdatetimeÚ	timedelta)Úsettingsc                   C   s   t t dd¡ƒS )zGenerate a 6-digit OTP.i † i?B )ÚstrÚrandomÚrandint© r   r   ú>/var/www/html/Moveonwheels/move_on_wheels/accounts/services.pyÚgenerate_otp   s   r
   c                 C   sN   t ƒ }|| _t ¡ tdd� | _|  ¡  |  ¡ }t| j	||ƒ dd| j	› �iS )z&Assign OTP to user and send via MSG91.é   )ÚminutesÚmessagezOTP sent successfully to )
r
   Úotpr   Úutcnowr   Ú
otp_expiryÚsaveÚget_full_nameÚsend_otp_via_msg91Úphone_number)Úuserr   Únamer   r   r	   Úset_otp	   s   r   c                 C   sB   | j |kr| jr| jt ¡ krtdƒ d| _ d| _|  ¡  dS dS )z&Check if OTP is valid and not expired.zverified otpNTF)r   r   r   r   Úprintr   )r   r   r   r   r	   Ú
verify_otp   s   r   c              
   C   s¢   d}ddt jdœ}dd| › �||dœgdœ}ztj|||d�}td	|jƒ | ¡  d
| ¡ dœW S  tjj	yP } ztd|ƒ dt
|ƒdœW  Y d}~S d}~ww )zSend OTP using MSG91 Flow API.z&https://control.msg91.com/api/v5/flow/zapplication/json)Úacceptzcontent-typeÚauthkeyÚ65bb4b91d6fc0562db225202Ú91)Úmobilesr   r   )Útemplate_idÚ
recipients)ÚheadersÚjsonzMSG91 Response:Úsuccess)ÚstatusÚresponsezError sending OTP via MSG91:Úfailed)r$   ÚerrorN)r   ÚMSG91_AUTH_KEYÚrequestsÚpostr   ÚtextÚraise_for_statusr"   Ú
exceptionsÚRequestExceptionr   )r   r   r   Úurlr!   Úpayloadr%   Úer   r   r	   r   )   s,   ýýÿþ
€þr   )
r   r   r   Údjango.confr   r
   r   r   r)   r   r   r   r   r	   Ú<module>   s    