3
h*                 @   sr  d dl mZmZmZmZmZmZmZmZ d dl	m	Z	m
Z
mZ d dlZd dlZd dl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T d d
lmZ dd Zejddgddd Zejddd Z ejddd Z!ejddgddd Z"ejddgddd Z#ejddgdd d! Z$ejd"d#d$ Z%ejd%dgdd&d' Z&dS )(    )Flaskjsonifyrender_templateurl_forrequestredirectjsonmake_response)datetime	timedeltadateN)app)db)db_cache	FromCache)Destination)PartnerDestination)*)fieldValidationc             C   s   | j j  d S )N)sessionclose)self r   0/booking_service/app/routes/destinations_urls.pyr      s    r   z/destination/newPOST)methodsc           :   C   s  g } y&t jd j  t jd s(| jd W n, tk
rV } z| jd W Y d d }~X nX y&t jd j  t jd s|| jd W n, tk
r } z| jd W Y d d }~X nX y&t jd j  t jd s| jd W n. tk
r  } z| jd W Y d d }~X nX | r(g }|jd	 t|| d
dfS tttj	 t jd t jd t jd d}t
jj| y0t
jj  tt
 g }|jd td|idfS  tk
r } z8t| t
jj  tt
 g }|jd td|idfS d }~X nX d S )NnamezName is empty.zName is missing.
session_idzSession ID is empty.zSession ID is missing.codezDestination Code is empty.z5You appear to be missing some data. Please try again.)messagemessagesi  )gatepass_destination_public_idgatepass_destination_namer   r   z0You have successfully created a new destination.r      zMUnfortunately, you cannot add a destination at this moment. Please try again.)r   r   stripappendKeyErrorr   r   struuiduuid4r   r   addcommitr   	Exceptionprintrollback)r    er   destinationoutputr   r   r   add_new_destination   sT    








r2   z/destination/viewc              C   s   g } g }t jjtjtjd kjttj	tj
j j }|sX| jd td| idfS x|D ]}| j|j  q^W td| idfS d S )Nz>There are no destinations in the system at this point in time.r      data)r   r   queryr   filterdeletion_markeroptionsr   r   order_byr"   ascallr%   r   return_json)r1   id_arrayall_destinationssingler   r   r   get_all_destinationsP   s    


r@   z"/destination/view/<destination_id>c             C   s   g }t jjtjtjd kjtj| kjtt	j
 }|sT|jd td|idfS x|D ]}|j|j  qZW td|idfS d S )NzLThat destination does not seem to exist in the system at this point in time.r   r3   r4   )r   r   r5   r   r6   r7   r!   r8   r   r   r;   r%   r   r<   )destination_idr1   r>   r?   r   r   r   get_single_destinationg   s    

rB   z/destination/modifyPATCHc           H   C   s4  g } y&t jd j  t jd s(| jd W n, tk
rV } z| jd W Y d d }~X nX y&t jd j  t jd s|| jd W n, tk
r } z| jd W Y d d }~X nX y&t jd j  t jd s| jd W n. tk
r  } z| jd	 W Y d d }~X nX y(t jd
 j  t jd
 s(| jd W n. tk
rX } z| jd W Y d d }~X nX tjjtj	tj
d kj	tjt jd
 kj }|sddi}tt|dfS t jd |_t jd |_t jd |_y*tjj  tt ddi}tt|dfS  tk
r. } zdt|i}tt|dfS d }~X nX d S )Nr   zName is empty.zName is missing.r   zSession ID is empty.zSession ID is missing.r   zDestination Code is empty.zDestination Code is missing.rA   zDestination ID is empty.zdestination ID is missing.r   z!No such desination could be foundi  zSuccesfully alteredr3   i  )r   r   r$   r%   r&   r   r   r5   r   r6   r7   r!   firstr	   r   r   r"   r   r+   r   r,   r'   )r    r/   r0   ZreponseObjecterrorr   r   r   modify_destination}   sT    



rF   z/destination/deletec           ,   C   s  g } y&t jd j  t jd s(| jd W n, tk
rV } z| jd W Y d d }~X nX y&t jd j  t jd s|| jd W n, tk
r } z| jd W Y d d }~X nX | rg }|jd t|| dd	fS tjjt	j
t	jd kj
t	jt jd kj }d
|_t jd |_y0tjj  tt g }|jd td|idfS  tk
r } z8t| tjj  tt g }|jd td|id	fS d }~X nX d S )NrA   zDestination ID is empty.zDestination ID is missing.r   zSession ID is empty.zSession ID is missing.z5You appear to be missing some data. Please try again.)r   r    i     z.You have successfully deleted the destination.r   r3   zPUnfortunately, you cannot delete a destination at this moment. Please try again.)r   r   r$   r%   r&   r   r   r   r5   r   r6   r7   r!   rD   r   r+   r   r,   r-   r.   )r    r/   r   get_destinationr1   r   r   r   delete_single_destination   sF    






rI   z/destination/partner/newc              C   s  ddddddg} t tj| }|r4tdtidfS xHtjd D ]:}tttj tjd |tjd t	j
 d	}tjj| q@W y0tjj  tt g }|jd
 td|idfS  tk
r } z6tjj  tt g }|jd t|t|ddfS d }~X nX d S )N
partner_idz
Partner ID)fieldaliasr   z
Session IDr    i  destinations)partner_destination_public_idrJ   rA   r   
created_atz,Successfully saved the partner destinations.r   r#   z9Unable to save the partner destinations. Try again later.)r   rE   )r   r   r   r   mesagesr   r'   r(   r)   r
   nowr   r   r*   r+   r   r%   r,   r.   )validation_listr    Zsingle_destinationp_destinationr   r/   r   r   r   add_partner_destinations   s2    




rT   z&/destination/partner/<partner_id>/viewc             C   s  t jjtjtjd kjtj| kj }|st jjtjtjd kj	t
tjtjj j }g }xf|D ]^}i }d|d< | |d< |j|d< |j|d< |j|d< |j|d< |j|d< |j|d	< |j| qhW td
|idfS g }x|D ]}|j|j  qW t|dd d}td
|idfS )N rN   rJ   rA   destination_public_iddestination_namer   rO   
updated_atr4   r3   c             S   s   | d S )NrW   r   )orderr   r   r   <lambda>A  s    z2view_single_partner_destinations.<locals>.<lambda>)key)r   r   r5   r   r6   r7   rJ   r;   r   r8   r   r   r9   r"   r:   r!   r   rO   rX   r%   r   r<   sorted)rJ   Zget_partner_destinationsr>   r4   r?   return_dataZsorted_datar   r   r    view_single_partner_destinations  s4    








r^   z/destination/partner/modifyc              C   s  ddddddg} t tj| }|r4tdtidfS g }tjjtj	tj
d kj	tjtjd kj }x|D ]}|j|j qlW xj|D ]b}|ttjd krqtjjtj	tjtjd kj	tj|kj }d	|_
tjd |_tj |_qW xXttjd D ]F}||krqtttj tjd |tjd tj d
}tjj| qW y0tjj  tt g }	|	jd td|	idfS  tk
r }
 z6tjj  tt g }	|	jd t|	t|
ddfS d }
~
X nX d S )NrJ   z
Partner ID)rK   rL   r   z
Session IDr    i  rM   rG   )rN   rJ   rA   r   rO   z,Successfully saved the partner destinations.r   r3   z9Unable to save the partner destinations. Try again later.)r   rE   )r   r   r   r   rP   r   r   r5   r   r6   r7   rJ   r;   r%   rA   setrD   r   r
   rQ   rX   r'   r(   r)   r*   r+   r   r,   r.   )rR   r    Zdestinations_array_dbZget_existing_destinationsZsingle_existingZeach_existingrH   Zsent_destinationrS   r   r/   r   r   r   modify_partner_destinationsF  sT    







r`   )'flaskr   r   r   r   r   r   r   r	   r
   r   r   pymysqlosmathrequestsr(   routesr   r   r   r   database.destinationr   Zdatabase.partner_destinationr   	variablesfunctions.validationr   r   router2   r@   rB   rF   rI   rT   r^   r`   r   r   r   r   <module>   s$   ((;?7(+