o
    QhW                     @   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 ddlmZ dd	lmZmZ dd
lmZ e	dd Ze	dd Zdd Ze	dd Ze	dd Ze	dd ZdS )    N)JsonResponse)render)status)csrf_exempt)settings   )send_otp_to_phone)generate_access_tokenverify_token)Userc              
   C   s   zCt | j}ddd tdD }d}t|d |}|r7|| jd< |d | jd< tdtj	d	d
tj	dW S tdtj
d	d
tj
dW S  tya } ztdtj
d	d
tj
dW  Y d }~S d }~ww )N c                 s       | ]}t d V  qdS )
1234567890Nrandomchoice.0i r   </var/www/html/DRIVERZZ_CUSTOMER_APIS/authentication/views.py	<genexpr>       zrequestOtp.<locals>.<genexpr>   1234mobileotpzOTP sent to registered Mobilemessager   Fsafer   z-Something went wrong. Please try again later.)jsonloadsbodyjoinranger   sessionr   r   HTTP_200_OKHTTP_400_BAD_REQUEST	Exception)requestrequest_datar   responseer   r   r   
requestOtp   s   
$r.   c                 C   s   t | j}|d }|| jd krdztjj| jd dd}W n4 tjyR   t }| jd |_d	dd t
dD }d	| |_d
|_d|_d|_d|_|  Y nw t|}td|tjddtjdS tdtjddtjdS )Nr   r      )r   	user_typer   c                 s   r   )$1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZNr   r   r   r   r   r   +   r   zverifyOtp.<locals>.<genexpr>   zUSER-r   r   zOtp Matched successfully)r   tokenr   Fr   zOtp not Matchedr   )r!   r"   r#   r&   r   objectsgetDoesNotExistr   r$   r%   username	is_activeis_staffis_superuserr0   saver	   r   r   r'   r(   )r*   r+   r   userr7   r3   r   r   r   	verifyOtp!   s&   
r=   c                 C   s   t tjtjddtjdS )N)show_deleter   Fr   )r   r   SHOW_DELETEr   r'   )r*   r   r   r   
showDelete9   s   r@   c              
   C   s   t | }|d krtdtjddtjdS ztjj|d  W n ty5 } z
t	| W Y d }~nd }~ww tdtj
ddtj
dS )NUser not logged in.r   Fr   idzAccount deleted successfully.)r
   r   r   HTTP_401_UNAUTHORIZEDr   r4   r5   deleter)   printr'   )r*   user_idr-   r   r   r   deleteAccount=   s   rH   c                 C   s   t | }|d krtdtjddtjdS ztjj|d}td|j|j|j	tj
ddtj
dW S  tjyD   tdtjddtjd Y S w )NrA   r   Fr   rB   zuser is verified.)r   nameemailr   r   )r
   r   r   rD   r   r4   r5   rI   rJ   r   r'   r6   )r*   rG   r<   r   r   r   
verifyUserJ   s   &rK   c                 C   s   t | }|d krtdtjddtjdS z(t| j}tjj	|d}|d |_
|d |_|  tdtjddtjdW S  tjyR   tdtjddtjd Y S w )	NrA   r   Fr   rB   rI   rJ   zProfile updated successfully.)r
   r   r   rD   r!   r"   r#   r   r4   r5   rI   rJ   r;   r'   r6   )r*   rG   r+   r<   r   r   r   profileX   s   

rL   )r!   r   django.httpr   django.shortcutsr   rest_frameworkr   django.views.decorators.csrfr   DRIVERZZ_CUSTOMER_APISr   helpersr   authr	   r
   modelsr   r.   r=   r@   rH   rK   rL   r   r   r   r   <module>   s*    



