o
    Uph&                     @   s  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 ddlmZmZ e	dd Ze	dd Z e	dd Z!e	dd Z"e	dd Z#e	dd Z$e	dd Z%e	dd Z&dS )     )render)status)settings)JsonResponse)csrf_exempt)check_passwordmake_passwordN)render_to_string)
strip_tags)	send_mailEmailMultiAlternatives)Q)User)ProfileSerializer   )generate_access_tokenverify_tokenc                 C   s   | j dkrRt| j}z0tjj|d d}t|d |jr/t	|}t
d|tjddtjdW S t
d	tjd
dtjdW S  tjyQ   t
dtjd
dtjd Y S w t
dtjd
dtjd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    r*   9/var/www/html/DRIVERZZ_ADMIN_APIS/authentication/views.pylogin   s   
r,   c                 C   s   t | }|d krtdtjddtjdS | jdkr=t| j}tj	j
|d}t|d |_|  tdtjddtjdS td	tjddtjd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)   r*   r*   r+   changePassword#   s   
r3   c           
   
   C   s  zZt | j}tjj|d d}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dW S  tjyp   tdtjddtjd Y S  ty }	 ztdtjddtjdW  Y d }	~	S d }	~	ww )Nr   r    c                 s       | ]}t d V  qdS )
1234567890Nrandomchoice.0ir*   r*   r+   	<genexpr>7       zrequestOtp.<locals>.<genexpr>   otpr2   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sessionr/   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   er*   r*   r+   
requestOtp2   s(   
$rQ   c                 C   sR   t | j}|d }|| jd krtdtjddtjdS tdtjddtjdS )Nr@   zOTP Matchedr   Fr   zOTP Not Matched)r   r   r    rF   r   r   r#   r$   )r'   r(   r@   r*   r*   r+   	verifyOtpG   s
   rR   c              
   C   s   z*t | j}|d }tjj| jd d}|| |  t	dt
jddt
jdW S  tjy@   t	dt
jddt
jd Y S  tya } zt| t	d	t
jddt
jdW  Y d }~S d }~ww )
Nr   r2   r.   zpassword changed successfullyr   Fr   zUser does't existrC   )r   r   r    r   r!   r"   rF   set_passwordr1   r   r   r#   r%   r$   rK   print)r'   r(   r   r)   rP   r*   r*   r+   changeForgotPasswordP   s   
$rU   c                 C   s   t | }|d krtdtjddtjdS | jdkrNtjj|d}| jd|_	| jd|_
| jd	|_| jr>| jd
 |_|  tdtjddtjdS tjj|d}t|dd}t|jtjddtjdS )Nr-   r   Fr   r   r.   rB   r   mobileprofile_piczProfile added successfully.many)r)   r   )r   r   r   r0   r   r   r!   r"   r   rB   r   rV   FILESrW   r1   r#   r   data)r'   r2   r)   
serializerr*   r*   r+   profilea   s   
r]   c                 C   sH  t | }|d krtdtjddtjdS | jdkr3tjjdd}t|dd	}t|j	tj
d
dtj
dS t| j}z!tjt|d ddt|d ddB }tdtjddtjdW S  tjy   t }|d |_|d |_|d |_d|_d|_d|_ddd tdD }d| |_d|_|  tdtj
ddtj
d Y S w )Nr-   r   Fr   GET   )	user_typeTrX   )	customersr   rV   rV   r`   r   r   r`   $Email ID or mobile no already exist.rB   r   r   r4   c                 s   r5   )$1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZNr7   r:   r*   r*   r+   r=      r>   zCustomers.<locals>.<genexpr>   zUSER-zCustomer created successfully.)r   r   r   r0   r   r   r!   filterr   r[   r#   r   r   r    r"   r   r$   r%   rV   rB   r   	is_activeis_staffis_superuserrD   rE   usernamer`   r1   )r'   r2   ra   r\   r(   r)   rk   r*   r*   r+   	Customersw   s:   




rl   c                 C   sr  t | }|d krtdtjddtjdS | jdkrd S t| j}zgtj	
t|d ddt|d	 dd
B }| r>| nd }|d u rGtjt|j t| |j|kr|tj	j|d}|d |_|d |_|d	 |_|  tdtjddtjdW S tdtjddtjdW S  tjy   tj	j|d}|d |_|d |_|d	 |_|  tdtjddtjd Y S w )Nr-   r   Fr   r^   rV   r_   rb   r   rc   r.   rB   zCustomer updated successfully.rd   )r   r   r   r0   r   r   r   r    r   r!   rg   r   existsfirstr%   rT   r/   r"   rV   rB   r   r1   r#   r$   )r'   r/   r2   r(   usersr)   r*   r*   r+   Customer   sB   








rp   )'django.shortcutsr   rest_frameworkr   django.confr   django.httpr   django.views.decorators.csrfr   django.contrib.auth.hashersr   r   r   r8   django.template.loaderr	   django.utils.htmlr
   django.core.mailr   r   django.db.modelsr   authentication.modelsr   authentication.serializerr   authr   r   r,   r3   rQ   rR   rU   r]   rl   rp   r*   r*   r*   r+   <module>   s>    






!