
    )g                          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 d dlmZ e
d        Ze
d        Z e
d        Z!e
d        Z"y)    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)Userc                    t        |       }|d k(  r2t        ddt        t        j                  idt        j                        S | j                  dk(  rt        j                  | j                        }	 t        j                  j                  |d         }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        |j)                          t        d#t        jb                  d$dt        jb                        S t*        j                  je                         }tg        |d%&      }t        |jh                  t        jb                  d'dt        jb                        S # t        j                  $ ro t               }|d   |_        d|_        d	|_        d	|_        d
j!                  d t#        d      D              }d|z   |_        d|_        |j)                          Y Qw xY w)(NmessageUser not loginFsafer   POSTmobile)r   r   r    c              3   F   K   | ]  }t        j                  d         yw)$1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZN)randomchoice).0is     J/mnt/ACDE16A4DE16673C/PROJECTS/DRIVERZZ/DRIVERZZ_ADMIN_APIS/trips/views.py	<genexpr>ztrips.<locals>.<genexpr>&   s     #l^_FMM2X$Y#ls   !   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   Tmany)tripsr   )5r
   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/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   r;   r   amountr   
net_amountHTTP_200_OKallr   data)requestrQ   request_datauserrM   trip
serializers          r$   r?   r?      s   7#G$Y)96&B^B^_fktz  uQ  uQ  R  	R >>V#::gll3L||''h1G'H 7D77DL$01C$DD!#/0A#BD (45K(LD%&23G&HD#%12E%FD"*67O*PD' k!:;DN #L$@ AD"./?"@D#/0A#BD $01C$DD!'34I'JD$(45K(LD%)56M)ND&*<8DO(4DM*<8DO(4DM ,^ <D ,^ <D6t<=DK!/$++">?DOIIK,GQWQcQc dkpy  zL  zL  M  M ==$$&D'48J:??VEWEW X^clrl~l~S $$ 	v*84!" !$%!77#lchijck#lm '( 2!"			s   3#I& &A>K('K(c                    | j                   dk(  r~	 t        j                  j                  | j                  j                  d            }t        |d      }t        |j                  t        j                  ddt        j                        S 	 t        j                  | j                        }t        j                  j                  |d         }|d   |_        |d   |_        |d   |_        |d   |_        |d   |_        |d   |_        t-        |d         |_        |d   |_        |d   |_        |d   |_        |d   |_        |d   |_        |d   |_        |d   |_        |d   |_        |d   |_         |d   |_!        |d   |_"        t-        tG        |            |_$        t-        tK        |jH                              |_&        |jO                          t        dt        j                  d	dt        j                        S # t        j                  $ r0 t        dt        j                  d	dt        j                        cY S t        $ r7}t        d
t        j                  d	dt        j                        cY d }~S d }~ww xY w# t        j                  $ r0 t        dt        j                  d	dt        j                        cY S t        $ r7}t        d
t        j                  d	dt        j                        cY d }~S d }~ww xY w)NGETrP   rP   Fr=   )r[   r   r   zInvalid trip id.r<   z-Something went wrong. Please try again later.r(   r)   r*   r+   r,   r-   r.   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r;   zTrip Edited successfully)(rA   r   rE   rF   r^   r   r   rW   r   rU   rG   HTTP_400_BAD_REQUEST	ExceptionrB   rC   rD   r(   r)   r*   r+   r,   r-   rR   r.   r0   r1   r2   r3   r4   r5   trip_start_datetrip_end_datetrip_start_timetrip_end_timer;   r   rS   r   rT   rO   )rX   r[   r\   erY   s        r$   editTriprg   M   s*   ~~	s==$$'++//$*?$@D'U;J&BTBT U\ajpj|j|}}	s::gll3L==$$,t*<$=D$01C$DD!#/0A#BD (45K(LD%&23G&HD#%12E%FD"*67O*PD' k!:;DN"./?"@D#/0A#BD $01C$DD!'34I'JD$(45K(LD%)56M)ND&#/#=D !-j!9D#/#=D !-j!9D ,^ <DnT23DK!/$++">?DOIIK,FPVPbPb cjox~  yK  yK  L  L= !! 	V,>HcHc dkpy  zU  zU  V  V 	s,[ek  fA  fA  !B  IN  W]  Wr  Wr  s  s	s: !! 	V,>HcHc dkpy  zU  zU  V  V 	s,[ek  fA  fA  !B  IN  W]  Wr  Wr  s  s	ssK   A<H E7J A J	J,J=JJA LL,LLLc                    t        |       }|d k(  r-t        dt        j                  ddt        j                        S t	        j
                  | j                        }t        j                  j                  |d         }d|_        |d   |_
        |j                          t        d	t        j                  ddt        j                        S )
Nr   r<   Fr   r[   r_   r   driverzTrip assigned successfully)r
   r   r   r@   rB   rC   rD   r   rE   rF   	driver_idrO   rU   )rX   rQ   rY   r[   s       r$   
assignTriprk   z   s    7#G$)9FD`D`ahmv|  wS  wS  T  	Tzz',,/}}  l6&: ;%h/		(DfN`N`ahmv|  wI  wI  J  	J    c                     	 t        |       }|d k(  r-t        dt        j                  ddt        j                        S t	        j
                  | j                        }t        j                  j                  |d         }|d   |_        |d   dk(  r^t        j                         j                         |_        t        j                         j                         j                  d	      |_        n|d   d
k(  rt        j                         j                         |_        t        j                         j                         j                  d	      |_        t'        t)        |            |_        t'        t-        |j*                              |_        t1        |      |_        |j5                          t        dt        j6                  ddt        j6                        S # t8        $ r}t;        |       Y d }~y d }~ww xY w)Nr   r<   Fr   r[   r_   r   r'   z%I:%M %p   z Trip status changed successfully)r
   r   r   r@   rB   rC   rD   r   rE   rF   r   nowdaterb   timestrftimerd   rc   re   rR   r   rS   r   rT   r   reference_idrO   rU   ra   print)rX   rQ   rY   r[   rf   s        r$   changeTripStatusru      s   w'd?-=HdHd elq  {A  {W  {W  X  X::gll3L==$$,v*>$?D&x0DKH%*'/||~':':'<$'/||~':':'<'E'Ej'Q$ h'1,%-\\^%8%8%:"%-\\^%8%8%:%C%CJ%O"!."67"%odkk&B"C$0$6!IIK,NX^XjXj krw  AG  AS  AS  T  T as   <G FG 	G=(G88G=)#r    django.shortcutsr   rest_frameworkr   django.httpr   r   r   django.views.decorators.csrfr   rB   DRIVERZZ_ADMIN_APISr	   authentication.authr
   rates.modelsr   r   trips.helpersr   r   r   r   r   modelsr   serializersr   authentication.modelsr   r?   rg   rk   ru    rl   r$   <module>r      s     # ! $ ( 4  ( , - x x  ' & 4@ 4@p (s (sX 
J 
J  rl   