3
؀h                 @   s  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 d dlmZ 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make_response)datetime	timedeltadateN)app)db)bookings_logger)Location)*c             C   s   | j j  d S )N)sessionclose)self r   4/booking_service/app/routes/pickup_locations_urls.pyr      s    r   z/pick-up/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 | rg }|jd td| id	fS tjjt	j
t	jd kj }xB|D ]:}|jj t jd j krg }|jd
 td|id	fS qW t	ttj t jd j 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.z5You appear to be missing some data. Please try again.messagesi  z/That pick-up location is already in the system.message)pickup_location_public_idpickup_location_namer   z-You have successfully created a new location.   zIUnfortunately, you cannot add a pick-up at this moment. Please try again.)r   r   stripappendKeyErrorr   r   r   queryr   filterdeletion_markerallr   lowerstruuiduuid4titleaddcommitr   	Exceptionprintrollback)r   er   Z
check_nameZ	each_namelocationoutputr   r   r   add_new_location   sV    









r4   z/pick-up/viewc              C   s~   g } g }t jjtjtjd kjtjj j	 }|sN| j
d td| idfS x|D ]}| j
|j  qTW td| idfS d S )Nz;There are no locations in the system at this point in time.r   i  data   )r   r   r#   r   r$   r%   order_byr   ascr&   r!   r   return_json)r3   id_arrayall_locationssingler   r   r   get_all_locationsR   s    

r=   z/pick-up/view/<location_id>c             C   s~   g }g }t jjtjtjd kjtj| kj }|sN|jd t	d|idfS x|D ]}|j|j
  qTW t	d|idfS d S )NzAThe selected location is not in the system at this point in time.r   i  r5   r6   )r   r   r#   r   r$   r%   r   r&   r!   r   r9   )location_idr3   r:   r;   r<   r   r   r   get_single_locationi   s    

r?   z/pick-up/modify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 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| idfS tjjt	j
t	jd kj
t	jt jd kj }|sxg }|jd td| idfS t jd j |_t jd |_t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 )Nr>   zLocation ID is empty.zLocation ID 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   i  z;The selected pick-up location does not exist in the system.i  z,You have successfully modified the location.r   r6   zGUnfortunately, you modify the pick-up at this moment. Please try again.)r   r   r    r!   r"   r   r   r   r#   r   r$   r%   r   firstr+   r   r   r
   now
updated_atr-   r   r.   r/   r0   )r   r1   r   Zcheck_locationr3   r   r   r   modify_location   s\    









rD   z/pick-up/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| i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 )Nr>   zLocation ID is empty.zLocation 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   i     z+You have successfully deleted the location.r   r6   zMUnfortunately, you cannot delete a location at this moment. Please try again.)r   r   r    r!   r"   r   r   r   r#   r   r$   r%   r   rA   r   r-   r   r.   r/   r0   )r   r1   r   Zget_locationr3   r   r   r   delete_single_location   sF    






rF   )flaskr   r   r   r   r   r   r   r	   r
   r   r   pymysqlosmathrequestsr)   routesr   r   r   database.pickup_locationr   	variablesr   router4   r=   r?   rD   rF   r   r   r   r   <module>   s   ((?@