
    rUf                     6   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mZ d dlZd dl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mZ e	d             Ze	d             Ze	d             Ze	d             Z e	d             Z!e	d             Z"dS )    )render)status)settings)JsonResponse)csrf_exempt)check_passwordmake_passwordN)render_to_string)
strip_tags)	send_mailEmailMultiAlternatives)User)ProfileSerializer   )generate_access_tokenverify_tokenc                 `   | j         dk    rt          j        | j                  }	 t          j                            |d                   }t          |d         |j                  r9t          |          }t          d|t          j        ddt          j                  S t          d	t          j        d
dt          j                  S # t          j        $ r, t          dt          j        d
dt          j                  cY S w xY wt          dt          j        d
dt          j                  S )NPOSTemailr   passwordzLogin successfully.)messagetokenr   Fsafer   zInvalid password.r   r   zInvalid email id.Invalid HTTP request.)methodjsonloadsbodyr   objectsgetr   r   r   r   r   HTTP_200_OKHTTP_400_BAD_REQUESTDoesNotExistHTTP_405_METHOD_NOT_ALLOWED)requestrequest_datauserr   s       FC:\Users\hp\python\driverzz\driverzz-admin-api\authentication\views.pyloginr,      s   ~z',//	Y<##,w*?#@@Dl:6FF ]-d33#1FRWbhbt$u$u  }B  KQ  K]  ^  ^  ^  ^#1DvOj$k$krw  AG  A\  ]  ]  ]  ]  	Y 	Y 	Y-@6Kf g gns  }C  }X  Y  Y  Y  Y  Y  Y	Y )@6Kmnnuz  DJ  D_  `  `  `  	`s   A9C	  (C	 	8DDc                    t          |           }|d k    r)t          dt          j        ddt          j                  S | j        dk    rt          j        | j                  }t          j	        
                    |          }t          |d                   |_        |                                 t          dt          j        ddt          j                  S t          d	t          j        ddt          j                  S )
NUser not logged in.r   Fr   r   idr   zPassword Updated successfully.r   )r   r   r   HTTP_401_UNAUTHORIZEDr   r   r    r!   r   r"   r#   r	   r   saver$   r'   r%   )r(   user_idr)   r*   s       r+   changePasswordr4   #   s   7##G$'<vGcddkpy  zV  W  W  W  	W>V##:gl33L<##w#//D),z*BCCDMIIKKK-MX^Xj k krw  AG  AS  T  T  T  T-DvOq r ry~  HN  Hc  d  d  d  d    c                 T   	 t          j        | j                  }t          j                            |d                   }d                    d t          d          D                       }|| j        d<   |j	        | j        d<   d}|j
        g}t          d	t          j        |j        |d
          }t          |          }t!          ||t          j        |          }|                    |d           |                                 t)          dt*          j        ddt*          j                  S # t          j        $ r, t)          dt*          j        ddt*          j                  cY S t2          $ r3}	t)          dt*          j        ddt*          j                  cY d }	~	S d }	~	ww xY w)Nr   r    c              3   >   K   | ]}t          j        d           V  dS )
1234567890N)randomchoice).0is     r+   	<genexpr>zrequestOtp.<locals>.<genexpr>7   s,      EEqv}\22EEEEEEr5      otpr3   zDRIVERZZ OTP requestz authentication/otp-template.html)logonamer@   z	text/htmlzOTP sent to registered Emailr   Fr   zOTP unable to sent.-Something went wrong. Please try again later.)r   r    r!   r   r"   r#   joinrangesessionr0   r   r
   r   LOGO_URLrB   r   r   EMAIL_HOST_USERattach_alternativesendr   r   r$   r&   r%   	Exception)
r(   r)   r*   r@   subjectto_emailhtml_templatetext_contentr   es
             r+   
requestOtprQ   2   s   qz',//|W(=>>ggEEE!HHEEEFF!$%)W	"(J<()KW_Whsws|  HK  NL  NL  M  M!-00&wh>VX`aa  <<<

)GRXRdeelq  {A  {M  N  N  N  	N W W W)>Ideelq  {A  {V  W  W  W  	W  	W  	W q q q)Xcic~  GL  U[  Up  q  q  q  	q  	q  	q  	q  	q  	qqs$   D.D1 18F'+	F'4(F"F'"F'c                 
   t          j        | j                  }|d         }|| j        d         k    r)t	          dt
          j        ddt
          j                  S t	          dt
          j        ddt
          j                  S )Nr@   zOTP Matchedr   Fr   zOTP Not Matched)r   r    r!   rF   r   r   r$   r%   )r(   r)   r@   s      r+   	verifyOtprS   G   s    z',//5!'/%(((]VEW X X_dmsm  A  A  A  A->Id e elq  {A  {V  W  W  W  Wr5   c                 P   	 t          j        | j                  }|d         }t          j                            | j        d                   }|                    |           |                                 t          dt          j        ddt          j                  S # t          j        $ r, t          dt          j        ddt          j                  cY S t          $ rB}t          |           t          d	t          j        ddt          j                  cY d }~S d }~ww xY w)
Nr   r3   r/   zpassword changed successfullyr   Fr   zUser does't existrC   )r   r    r!   r   r"   r#   rF   set_passwordr2   r   r   r$   r&   r%   rK   print)r(   r)   r   r*   rP   s        r+   changeForgotPasswordrW   P   s   oz',//
+|W_Y%?@@(###		'FQWQcddkpy  zL  M  M  M  	M S S S':VE`aahmv|  wR  S  S  S  	S  	S  	S o o oa'Vaga|}}  EJ  SY  Sn  o  o  o  	o  	o  	o  	o  	o  	oos$   BB   8D%	D%#7D D% D%c                    t          |           }|d k    r)t          dt          j        ddt          j                  S | j        dk    rt
          j                            |          }| j                            d          |_	        | j                            d          |_
        | j                            d	          |_        | j        r| j        d
         |_        |                                 t          dt          j        ddt          j                  S t
          j                            |          }t!          |d          }t          |j        t          j        ddt          j                  S )Nr.   r   Fr   r   r/   rB   r   mobileprofile_piczProfile added successfully.)many)r*   r   )r   r   r   r1   r   r   r"   r#   r   rB   r   rY   FILESrZ   r2   r$   r   data)r(   r3   r*   
serializers       r+   profiler_   a   so   7##G$)>Ieffmr  |B  |X  Y  Y  Y  	Y>V##<##w#//D((00DI ))'22DJ!,**844DK} @#*=#? IIKKK-JU[Ug h hot  ~D  ~P  Q  Q  Q  Q<##w#//D*4e<<<J*/FDV W W^clrl~r5   )#django.shortcutsr   rest_frameworkr   django.confr   django.httpr   django.views.decorators.csrfr   django.contrib.auth.hashersr   r	   r   r:   django.template.loaderr
   django.utils.htmlr   django.core.mailr   r   authentication.modelsr   authentication.serializerr   authr   r   r,   r4   rQ   rS   rW   r_    r5   r+   <module>rm      s   # # # # # # ! ! ! ! ! !             $ $ $ $ $ $ 4 4 4 4 4 4 E E E E E E E E   3 3 3 3 3 3 ( ( ( ( ( ( > > > > > > > > & & & & & & 7 7 7 7 7 7 5 5 5 5 5 5 5 5 ` ` `  d d d q q q( W W W o o o  @ @ @ @ @r5   