
    <i                     D   d dl Z d dlZ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  e              e e j        dd                    Z e j        dd	                                          d
v Zd ZddedefdZdededefdZdedededefdZdedededdfdZdS )    N)Path)load_dotenv)get_col)
send_emailOTP_EXP_SECONDSiX  EMAIL_STRICTfalse>   1onyestruec                     t          d          t          t                    j        j        dz  dz  t          t                    j        j        j        dz  dz  dz  g} | D ]y}|                                rct	          |d          5 }t          j        |                                                              d          cd d d            c S # 1 swxY w Y   zdS )Nzapp/logo/NbitzLogo.pnglogozNbitzLogo.pngapprbzutf-8 )	r   __file__parentexistsopenbase64	b64encodereaddecode)possible_paths	logo_path
image_files      )D:\Nbitz\nbitez_python\app\utility\otp.pyload_logo_base64r      s+    	%&&X$v-?X$+e3f<NN $ K K	 	Ki&& K*'
(9(9::AA'JJK K K K K K K K K K K K K K K K K K 2s   9CC	C	   lengthreturnc                 j    | dk     rd} d                     d t          |           D                       S )N   r   c              3   Z   K   | ]&}t          t          j        d d                    V  'dS )r   	   N)strrandomrandint).0_s     r   	<genexpr>z'generate_numeric_otp.<locals>.<genexpr>$   s6      DD3v~a++,,DDDDDD    )joinrange)r!   s    r   generate_numeric_otpr0   !   s7    zz77DDeFmmDDDDDDr-   emailpurposec           
         t          d          }t                      }t          t          j                              }|                    |                                 |dd|                                 ||||t          z   did           |S )z:
    purpose: e.g. "verify_email" or "reset_password"
    otpsr1   r2   z$set)r1   r2   otp	createdAt	expiresAtT)upsert)r   r0   inttime
update_onelowerr   )r1   r2   colr6   nows        r   create_email_otpr@   '   s     &//C

 
 C
dikk

CNN++--G44	5;;==WSWZilo~i~  	A    
 Jr-   r6   c                    t          d          }|                    |                                 |d          }|sdS t          t	          j                              }|t          |                    dd                    k    rdS t          |                    d                    t          |          k    rdS |                    d|d         i           dS )	Nr4   r5   Fr8   r   r6   _idT)r   find_oner=   r:   r;   getr'   
delete_one)r1   r2   r6   r>   recr?   s         r   verify_email_otprG   6   s    
&//C
,,7CC
D
DC u
dikk

C
Sa(())))u
3775>>c#hh&&uNNE3u:&'''4r-   c           	         t                      }d}d|                    dd           d| dt          dz   d}d	}|rd
| d}d| d|                    dd                                           d| dt          dz   d	}	 t	          || g||           d S # t
          $ r=}t          d|            t          d|  d| d|            t          r Y d }~d S d }~ww xY w)NzYour OTP CodezYour OTP for r+    z is: z
This code will expire in <   z	 minutes.r   z <img src="data:image/png;base64,z=" alt="Nbitz Logo" style="width:150px;margin-bottom:20px;" />zG
    <div style="font-family:Arial,sans-serif;line-height:1.5;">
      z
      <h2>OTP for zn</h2>
      <p>Your one-time code is:</p>
      <p style="font-size:22px;font-weight:700;letter-spacing:3px;">z#</p>
      <p>This code expires in za minutes.</p>
      <p>If you didn't request this, you can ignore this email.</p>
    </div>
    )htmltextz[EMAIL OTP FAILED] z[EMAIL OTP] To: z | Purpose: z | OTP: )r   replacer   titler   	Exceptionprintr   )	r1   r6   r2   logo_base64subjectrL   	logo_htmlrK   excs	            r   send_otp_via_emailrU   D   s   "$$KGy7??344yy3yy[jln[nyyyD I B B{  B  B  B			 	 ??3,,2244	 	
 FI	 	  /2	 	 	D7UG$T::::::   )C))***JJJGJJSJJKKK 		 	 	 	 	 		s   B 
C!2CC)r    )osr(   r;   r   pathlibr   dotenvr   	app.db.dbr   app.utility.emailr   r:   getenvr   r=   r   r   r'   r0   r@   boolrG   rU    r-   r   <module>r^      s   				                      ( ( ( ( ( ( #ibi 137788ry117799=WW  "E E ES E E E EC # #    C # C D    c  c d      r-   