
    3g3                        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mZmZmZ d dlZd dl	m
Z
 d dl	Z	dZd	Zd
Z eedd      Zej!                          dad Zej'                  d      d        Zej'                  d      d        Zej'                  d      d        Zej'                  d      d        Zej'                  ddg      d        Zej'                  ddg      d        Zej'                  ddg      d        Zej'                  dd g      d!        Zej'                  ddg      d"        Zej'                  d#dg      d$        Zej'                  d%dg      d&        Zej'                  d'dg      d(        Zej'                  d)dg      d*        Z ej'                  d+dg      d,        Z!ej'                  d-dg      d.        Z"d/ Z#d0 Z$ e$        y)1    )Flaskrender_template)url_for)current_app)requestredirectr   jsonifyN)Clientzdatabase.sqlite@   i:	 	templatesz/static)template_folderstatic_url_pathc                     d| j                   vry | j                   j                  d      }t        j                  |      }|S )Ntoken)cookiesgetdatabaseget_client_from_token)r   r   users      $/var/www/html/friend-locator/main.pyget_user_from_tokenr      s:    goo%OO(E))%0DK    z	/homepagec                      t        t              } | | j                         rt        d      S t	        t        d            S )Nzhomepage.htmlreturn_login_page)r   r   has_valid_tokenr   r	   r   r   s    r   homepager      s9    w'DD002// 3455r   z/logoutc                  l    t         j                  t        d            } | j                  dddd       | S )N/login-pager   0r   Texpiressecure)appmake_responser	   
set_cookie)resps    r   logoutr)   %   s0    Xm45DOOGSAO=Kr   z/add-friendsc                      t        t              } | | j                         st        t	        d            S t        d      S )Nr   zadd_friends.html)r   r   r   r	   r   r   r   s    r   add_friendsr+   ,   s9    w'D|4//1 3455-..r   /c                      t        t              } | $| j                         rt        t	        d            S t        t	        d            S )Nr   r   )r   r   r   r	   r   r   s    r   webpager.   4   s>    w'DD002
+,, 3455r   z/API/locationPOST)methodsc                      t        t              } | yt        j                  d   }t        j                  d   }| j                  ||       y)NzInvalid or missing token.  latlon)zLocation updated successfuly   )r   r   formset_location)r   latitude	longitudes      r   r8   r8   <   sC    w'D|/||E"HU#Ih	*.r   z/API/friend_requests/<which>GETc                     t        t              }|y| dk(  r|j                         }n| dk(  r|j                         }nyt        j                  |      }t        |      S )Nr2   incomingoutgoing)zInvalid path  )r   r   get_incoming_friend_requestsget_outgoing_friend_requestsr   user_id_list_to_name_listr
   )whichr   request_id_listuser_name_lists       r   get_friend_requestsrF   K   s^    w'D|/
;;=	*	;;="77HN>""r   z/API/friendc                      t        t              } | yt        j                  | j	                               }t        |      S )Nr2   )r   r   r   rB   get_friendsr
   )r   friend_lists     r   rH   rH   \   s8    w'D|/44T5E5E5GHK;r   DELETEc                      t        t              } | yt        j                  d   }t        j	                  |      }| j                  |      }|ryy)Nr2   name)Successr6   )Failiure   )r   r   r7   r   get_client_from_nameunfriend)r   usernamefriendsuccesss       r   remove_friendrU   f   sI    w'D|/||F#H**84FmmF#Gr   c                  j   t        t              } | yt        j                  d   }t        j	                  |      }| j                  |      }|xt        j                  k(  r yxt        j                  k(  r yxt        j                  k(  r yxt        j                  k(  r yt        j                  k(  ryy )Nr2   rL   )zCould not send friend request.i  )zRequest sentr6   )z,You have already sent a request to this userr6   )zAdded friendr6   )zECould not send frient request. You are already friends with this userr6   )r   r   r7   r   rP   send_friend_requestr   FAILUREREQUEST_SENTDUPLICATE_REQUESTFRIEND_ADDEDALREADY_FRIENDS)r   rR   rS   statuss       r   
add_friendr^   u   s    w'D|/||F#H**84F%%f-F
V^^8 V  &%V%%F V  &##_ $r   z/API/location/<username>c                     t        t              }|yt        j                  |       }|y|j	                  |      sy|j                         \  }}||y| ||d}t        |      dfS )Nr2   )zAccount does not exist.r3   )zThis is not your friend.r3   )zUser's location not available.r?   )rR   r9   r:   r6   )r   r   r   rP   is_friends_withget_locationr
   )rR   	requesterrS   r9   r:   user_locations         r   get_friend_locationrd      s    #G,I/**84F~-$$V,. --/Hi9,4 ! "M =!3&&r   r    c                      t        d      S )Nz
login.htmlr    r   r   r   r      s    <((r   z/create-accountc                      t        d      S )Nzaccount_creation.htmlr   rf   r   r   return_account_creatorrh      s    233r   z/API/sessionc                  x   t         j                  d   } t         j                  d   }t        j                  | |      }|sy|j	                         r|j                         }n|j                  t        t              }t        j                  d      }|j                  d|j                   |j                  d       |S )NrR   password)zUsername or password incorrect.r3   )zSuccessfuly logged inr6   r   Tr"   )r   r7   r   get_client_from_credentialsr   	get_tokengenerate_new_tokenTOKEN_LENGTHTOKEN_LIFETIME_SECr%   r&   r'   r   expiry)rR   rj   clientr   r(   s        r   create_sessionrr      s    ||J'H||J'H11(HEF9  ")),8JK;<DOOG}PTOUKr   z/API/users/<username>c                 l    t         j                  |       }|t        ddi      dfS t        ddi      dfS )NexistsFr6   T)r   rP   r
   )rR   r   s     r   user_existsru      sA    ((2D|%()3..Hd#$c))r   z
/API/usersc                      t         j                  d   } t         j                  d   }t        j                  |       }|yt        j	                  | |      }|yy)NrR   rj   )zUsername already in use.r3   )z&Server error. Could not create accounti  )zAccount successfuly created.r6   )r   r7   r   rP   create_client)rR   rj   r   s      r   add_userrx      sV     ||J'H||J'H((2D.!!(H5D|<.r   c                 V    t         j                          t        d       t                y )NzSuccessfuly closed server.)r   closeprintexit)signumframes     r   cleanupr      s    NN	
./Fr   c                      t        j                  t        t              at        j
                  t
        j                  t               y )N)db_interfaceDatabaseDATABASE_PATHrn   r   signalSIGINTr   rf   r   r   mainr      s(    $$]LAH MM&--)r   )%flaskr   r   r   r   r   r	   r
   r   r   r   r   rn   ro   __name__r%   app_contextr   r   router   r)   r+   r.   r8   rF   rH   rU   r^   rd   r   rh   rr   ru   rx   r   r   rf   r   r   <module>r      s]    !   5 5   ! Hk9M   ;6 6 9  >/ / 36 6 ?VH-
/ .
/ )E7;# <#  =5'*  +  =8*- . =6(+` ,`, %w7' 8'2 =5'*) +) ug.4 /4 >F8, -$ "UG4* 5* <&*/ +/ * r   