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	 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mZ d dlmZ d dlm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dS )    )Flaskjsonifyrender_templateurl_forrequestredirectjson)datetime	timedeltaN)appbookings_logger)ContactList)dbz/contact/newPOST)methodsc              C   sR  y&t jd j  t jd s$tjd W n, tk
rR }  zt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 }|rg }|jd td|idfS t	ttj t jd tj tj d}tjj| y2tjj  tjj  g }|jd td|id	fS  tk
rL   tjj  tjj  g }|jd
 td|idfS X d S )Nemail_addresszEmail address is empty.zEmail address is missing.z8The email address provided already exists in the system.messagei  )contact_public_idcontact_email
created_at
updated_atz)The contact email address has been added.   z4There was an error adding the contact email address.i  )r   r   stripmessagesappendKeyErrorr   sessionqueryr   filterdeletion_markerr   firstr   struuiduuid4r	   nowaddcommitclose	Exceptionrollback)eget_contactr   Zcontact r,   0/booking_service/app/routes/contact_list_urls.pyaddNewContactEmail   s>    








r.   z/contact/viewGETc              C   sr   t jjtjtjd kj } | s>g }|jd td|idfS g }x| D ]}|j|j	  qHW td|idfS d S )Nz)There are no contact email addresses yet.r   i  datar   )
r   r   r   r   r   r   allr   r   return_json)get_contactsr   r0   singler,   r,   r-   viewContactEmails:   s    

r5   z/contact/deletePATCHc              C   s0  y&t jd j  t jd s$tjd W n, tk
rR }  zt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 }|sg }|jd td|idfS d|_tj |_y2tjj  tjj  g }|jd td|id	fS  tk
r*   tjj  tjj  g }|jd
 td|idfS X d S )N
contact_idzContact ID is empty.zContact ID is missing.z0The selected contact email is not in the system.r   i     z+The contact email address has been deleted.r   z6There was an error deleting the contact email address.i  )r   r   r   r   r   r   r   r   r   r   r   r   r   r    r   r	   r$   r   r&   r'   r(   r)   )r*   r+   r   r,   r,   r-   deleteContactEmailN   s6    








r9   )flaskr   r   r   r   r   r   r   r	   r
   pymysqlosmathrequestsr"   routesr   r   database.contact_listr   r   router.   r5   r9   r,   r,   r,   r-   <module>   s   $(.