o
    ~h&                      @   s   d dl 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
 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mZmZmZ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!e
dd Z"e
dd Z#dS )    N)render)status)JsonResponse)datetime	timedelta)csrf_exempt)settings)verify_token)Daytimes	RateLists)calculate_farecalculate_estimated_farecreate_ordertotal_daytime_minutescalculate_taxes   )Trips)TripSerializerTripsSerializer)Userc                 C   s  t | }|d krtddttjidtjdS | jdkrt| j}ztj	j
|d d}W n3 tjy^   t }|d |_d|_d	|_d	|_d
dd tdD }d| |_d|_|  Y nw t }|j|_|d |_|d |_|d |_|d |_|d |_|d |_t|d |_t|d |_ |d |_!|d |_"|d |_#|d |_$|d |_%|d |_&|d |_'|d |_(|d  |_)|d! |_*|d" |_+|d# |_,tt-||_.tt/|j.|_0|  td$tj1d%dtj1dS tj	2 3d&}t4|d'd(}t|j5tj1d)dtj1dS )*NmessageUser not loginFsafer   POSTmobile)r   r   r    c                 s   s    | ]}t d V  qdS )$1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZN)randomchoice).0i r"   0/var/www/html/DRIVERZZ_ADMIN_APIS/trips/views.py	<genexpr>&   s    ztrips.<locals>.<genexpr>   zUSER-   vehicle_model_idvehicle_type_idtransmission_type_idvehicle_model_namevehicle_type_nametransmission_type_name	trip_typestation_typepickup_addresspickup_latitudepickup_longitudedestination_addressdestination_latitudedestination_longitude
start_dateend_date
start_timeend_timeuniform_typeinstructionszTrip Created successfullyr   r   z-idTmany)tripsr   )6r	   r   r   HTTP_401_UNAUTHORIZEDmethodjsonloadsbodyr   objectsgetDoesNotExistr   	is_activeis_staffis_superuserjoinrangeusername	user_typesaver   iduser_idr'   r(   r)   r*   r+   r,   intr-   r.   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r   amountr   
net_amountHTTP_200_OKallorder_byr   data)requestrP   request_datauserrL   trip
serializerr"   r"   r#   r>      s^   




















r>   c              
   C   s0  | j dkrXztjj| jdd}t|dd}t|jtj	ddtj	dW S  tj
y:   tdtjd	dtjd Y S  tyW } ztd
tjd	dtjdW  Y d }~S d }~ww zt| j}tjj|d d}|d |_|d |_|d |_|d |_|d |_|d |_t|d |_|d |_|d |_|d |_|d |_|d |_|d |_|d |_|d |_|d |_ |d |_!|d |_"tt#||_$tt%|j$|_&|'  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y } ztd
tjd	dtjdW  Y d }~S d }~ww )NGETrO   rO   Fr<   )r[   r   r   zInvalid trip id.r;   z-Something went wrong. Please try again later.r'   r(   r)   r*   r+   r,   r-   r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r:   zTrip Edited successfully)(r@   r   rD   rE   r]   r   r   rW   r   rT   rF   HTTP_400_BAD_REQUEST	ExceptionrA   rB   rC   r'   r(   r)   r*   r+   r,   rQ   r-   r/   r0   r1   r2   r3   r4   trip_start_datetrip_end_datetrip_start_timetrip_end_timer:   r   rR   r   rS   rN   )rX   r[   r\   erY   r"   r"   r#   editTripL   sT   
$
















$rf   c                 C   sv   t | }|d krtdtjddtjdS t| j}tjj	|d d}d|_|d |_
|  td	tjddtjdS )
Nr   r;   Fr   r[   r^   r   driverzTrip assigned successfully)r	   r   r   r?   rA   rB   rC   r   rD   rE   	driver_idrN   rT   )rX   rP   rY   r[   r"   r"   r#   
assignTripy   s   
ri   c              
   C   s*  z}t | }|d krtdtjddtjdW S t| j}tjj	|d d}|d |_|d dkrBt
  |_t
  d	|_n+|d d
krmt
  |_t
  d	|_tt||_tt|j|_t||_|  tdtjddtjdW S  ty } zt| W Y d }~d S d }~ww )Nr   r;   Fr   r[   r^   r      z%I:%M %p   z Trip status changed successfully)r	   r   r   r?   rA   rB   rC   r   rD   rE   r   nowdatera   timestrftimerc   rb   rd   rQ   r   rR   r   rS   r   reference_idrN   rT   r`   print)rX   rP   rY   r[   re   r"   r"   r#   changeTripStatus   s,   

rr   )$r   django.shortcutsr   rest_frameworkr   django.httpr   r   r   django.views.decorators.csrfr   rA   DRIVERZZ_ADMIN_APISr   authentication.authr	   rates.modelsr
   r   trips.helpersr   r   r   r   r   modelsr   serializersr   r   authentication.modelsr   r>   rf   ri   rr   r"   r"   r"   r#   <module>   s,    
7
,
