3
؀h                 @   s,  d dl 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 d dlmZ d dlmZ d d	lT d d	lT d d	lT 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"dS )    )Flaskjsonifyrender_templateurl_forrequestredirectjson)datetime	timedeltadateN)app)db)	GatesSeed)Gate)	EntryMode)*c             C   s   | j j  d S )N)sessionclose)self r   (/booking_service/app/routes/gate_urls.pyr      s    r   z
/gates/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td
| idfS tttj	 t jd t jd t jd t
j d}tjj| 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namezName is empty.zName is missing.
entry_modezEntry mode is empty.zEntry mode is missing.
session_idzSession ID is empty.zSession ID is missing.messagesi  )gate_public_id	gate_nameentry_mode_idr   
created_atz(You have added a new gate to the system.message   z6The gate could not be added at this moment. Try later.)r!   error)r   r   stripappendKeyErrorr   r   struuiduuid4r	   nowr   r   addcommitr   	Exceptionrollback)r   egater!   r   r   r   add_new_gate   sP    







r1   z/gates/viewc              C   s  t jjtjtjd kjtjj j	 } | sJg }|j
d td|idfS g }x| D ]}i }|j|d< |j|d< t jjtjtjd kjtj|jkj }y|j|d< |j|d< W n2 tk
r } zd|d< d|d< W Y d d }~X nX |j
| qTW td	|id
fS d S )Nz!There are no gates in the system.r!   i  r   r   entry_mode_nameentry_mode_public_id data   )r   r   queryr   filterdeletion_markerorder_byr   ascallr%   r   r   r   r3   r   firstr2   r-   )get_all_gatesr!   r5   singleZ
singe_gateget_entry_moder/   r   r   r   view_all_gatesQ   s.    




rA   z/gates/view/<gate_id>c             C   s   t jjtjtjd kjtjdkj dkr4tj	  t jjtjtjd kjtj| kj
 }|s~g }|jd td|idfS g }x|D ]}|j|j  qW td|idfS d S )N9ac19eb6r   z'That gate does not exist in the system.r!   i  r5   r6   )r   r   r7   r   r8   r9   r   countr   seed_default_gatesr<   r%   r   return_json)gate_idr>   r!   r5   r?   r   r   r   view_single_gatet   s     

rG   z/gates/modifyc           H   C   s  t tj g } y&tjd j  tjd s2| 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
r
 } z| jd	 W Y d d }~X nX y(tjd
 j  tjd
 s2| jd W n. tk
rb } 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 }|sg }|jd td|idfS tjd |_tjd |_tjd
 |_tj |_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 )Nr   zGate ID is empty.zGate ID is missing.r   zEntry mode is empty.zEntry mode is missing.r   zName is empty.zName 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'That gate does not exist in the system.r!   i  z#You have modified the gate details.r"   zAThe gate details could not be modified at this moment. Try later.)r!   r#   )printr   r   r$   r%   r&   r   r   r   r7   r   r8   r9   r   r=   r   r   r   r	   r*   
updated_atr,   r   r-   r.   r'   )r   r/   r!   get_one_gater   r   r   modify_gate   sj    










rK   z/gates/deletePATCHc           ,   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 | rtd| idfS tjjt	j
t	jd kj
t	jt jd kj }|sg }|jd	 td
|idfS d|_t jd |_tj |_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 )NrF   zGate ID is empty.zGate ID is missing.r   zSession ID is empty.zSession ID is missing.r   i  z'That gate does not exist in the system.r!   i     z3The gate details have been deleted from the system.r"   z@The gate details could not be deleted at this moment. Try later.)r!   r#   )r   r   r$   r%   r&   r   r   r   r7   r   r8   r9   r   r=   r   r	   r*   rI   r,   r   r-   r.   r'   )r   r/   rJ   r!   r   r   r   delete_gate   sJ    







rN   )#flaskr   r   r   r   r   r   r   r	   r
   r   pymysqlosmathrequestsr(   routesr   r   routes.seed_functionsr   database.gater   database.entry_moder   	variablesfunctions.date_operatorsfunctions.currency_operatorsr   router1   rA   rG   rK   rN   r   r   r   r   <module>   s    $(7#H