o
    	whx                      @   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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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)DriverPartnersDriverVehiclesUser)DriverSerializerc              
   C   s
  zMt | j}tjj|d dd}ddd tdD }d}t|d |}|rA|| 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jyc   t
dtjddtjd Y S  ty } zt| t
dtjddtjdW  Y d }~S d }~ww )Nmobile   )r   	user_type c                 s   s    | ]}t d V  qdS )
1234567890N)randomchoice).0i r   :/var/www/html/DRIVERZZ_DRIVER_APIS/authentication/views.py	<genexpr>   s    zrequestOtp.<locals>.<genexpr>   1234otpzOTP sent to registered Mobilemessager   Fsafer   z-Something went wrong. Please try again later.zMobile no. doesn't exists)jsonloadsbodyr   objectsgetjoinranger   sessionr   r   HTTP_200_OKHTTP_400_BAD_REQUESTDoesNotExist	Exceptionprint)requestrequest_datauserr   responseer   r   r   
requestOtp   s$   
$r4   c                 C   s   t | j}|d }|| jd krDztjj| jd d}t|}td|t	j
ddt	j
dW S  tjyC   tdt	jd	dt	jd Y S w td
t	jd	dt	jdS )Nr   r   )r   zOtp Matched successfully)r   tokenr   Fr    zUser doesn't exists.r   zOtp not Matched)r"   r#   r$   r)   r   r%   r&   r	   r   r   r*   r,   r+   )r/   r0   r   r1   r5   r   r   r   	verifyOtp%   s   r6   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   
showDelete4   s   r9   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   r%   r&   deleter-   r.   r*   )r/   user_idr3   r   r   r   deleteAccount8   s   r@   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 )Nr:   r   Fr    r;   zuser is verified.)r   nameemailr   r   )r
   r   r   r=   r   r%   r&   rA   rB   r   r*   r,   )r/   r?   r1   r   r   r   
verifyUserE   s   &rC   c                 C   s   t | }|d krtdtjddtjdS ztjj|d}t|dd}t|jdtj	ddtj	dW S  t
jyF   tdtjddtjd Y S w )	Nr:   r   Fr    r?   )manyzProfile updated successfully.)driverr   r   )r
   r   r   r=   r   r%   r&   r   datar*   r   r,   )r/   r?   rF   
serializerr   r   r   profileS   s   rI   c                 C   s   t | }|d krtdtjddtjdS zPtjj|d}| jd|j_	| jd|j_
| jd|j_| jd	|_| jd
|_d| jv rO| jd|j_|  |j  tdtjddtjdW S  tjyz   tdtjddtjd Y S w )Nr:   r   Fr    rD   rA   r   rB   gender
experienceprofile_picz Basic info updated successfully.)r
   r   r   r=   r   r%   r&   POSTr1   rA   r   rB   rJ   rK   FILESrL   saver*   r   r,   )r/   r?   rF   r   r   r   	basicInfoe   s$   

rP   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 |_|d |_|  td	tjddtjdW S  tjyW   tdtjddtjd Y S w )
Nr:   r   Fr    rD   locationhouse_numberpincodeAddress updated successfully.)r
   r   r   r=   r"   r#   r$   r   r%   r&   rQ   rR   rS   rO   r*   r   r,   )r/   r?   r0   rF   r   r   r   addressInfo|   s   


rU   c                    s   t | }|d krtdtjddtjdS zOt| j}tjj	|d |d  _
|d  _|d  _|d	  _   tjj jd
  |d rWtj fdd|d D  tdtjddtjdW S  tjyy   tdtjddtjd Y S w )Nr:   r   Fr    rD   uniform_type	trip_typestation_typetransmission_type)	driver_idvehicle_modelsc                    s   g | ]	}t  j|d qS ))rZ   vehicle_model)r   r?   )r   r\   rF   r   r   
<listcomp>   s    zprefrences.<locals>.<listcomp>rT   )r
   r   r   r=   r"   r#   r$   r   r%   r&   rV   rW   rX   rY   rO   r   filterr?   r>   bulk_creater*   r   r,   )r/   r?   r0   r   r]   r   
prefrences   s(   



ra   ) r"   r   django.httpr   django.shortcutsr   rest_frameworkr   django.views.decorators.csrfr   DRIVERZZ_DRIVER_APISr   helpersr   authr	   r
   modelsr   r   r   serializersr   r4   r6   r9   r@   rC   rI   rP   rU   ra   r   r   r   r   <module>   s8    






