
    \f                        d dl mZmZmZmZmZmZ d dlmZ d dl	m
Z
 d dlZ ee      Zde_        ej                  d      d        Zdej"                  d	<   d
ej"                  d<    ee      Z G d dej&                        Zej                  dddg      d        Zej                  d      d        Zej                  dg d      d        Zej                  d      d        Zedk(  rej3                  d       yy)    )Flaskrequestjsonifysessionredirecturl_for)
SQLAlchemy)IntegrityErrorNsupersecretkey/c                       y)NzWelcome to the Home Page! r       "/var/www/highground/backend/app.pyhomer   
   s    &r   z4postgresql://app_user:your_password@localhost/app_dbSQLALCHEMY_DATABASE_URIFSQLALCHEMY_TRACK_MODIFICATIONSc                       e Zd Zej	                  ej
                  d      Zej	                  ej                  d      dd      Zej	                  ej                  d      d      Z	d Z
d	 Zy
)UserT)primary_keyP   F)uniquenullablex   )r   c                     t        j                  |j                  d      t        j                               j	                  d      | _        y)z%Hash the password and store the hash.utf-8N)bcrypthashpwencodegensaltdecodepassword_hashselfpasswords     r   set_passwordzUser.set_password   s1    #]]8??7+CV^^EUV]]^efr   c                 ~    t        j                  |j                  d      | j                  j                  d            S )z.Check if the password matches the stored hash.r   )r   checkpwr   r"   r#   s     r   check_passwordzUser.check_password   s.    ~~hoog68J8J8Q8QRY8Z[[r   N)__name__
__module____qualname__dbColumnIntegeridStringusernamer"   r&   r)   r   r   r   r   r      sZ    	2::4	0Byy2teyDHIIbiinuI=Mg\r   r   z/loginGETPOST)methodsc                  D   t         j                  dk(  rt         j                  d   } t         j                  d   }t        j                  j                  |       j                         }|r6|j                  |k(  r'|j                  t        d<   t        t        d            S yy)	Nr4   r2   r%   r2   user_idwelcomezInvalid username or passwordz
        <form method="POST">
            <input type="text" name="username" placeholder="Username"/>
            <input type="password" name="password" placeholder="Password"/>
            <input type="submit" value="Login"/>
        </form>
    )r   methodformr   query	filter_byfirstr%   r0   r   r   r   )r2   r%   users      r   loginr@   "   s    ~~<<
+<<
+ zz##X#6<<>DMMX-!%GIGI.//1r   z/welcomec                  <    dt         v ryt        t        d            S )Nr8   zWelcome to the landing page!r@   )r   r   r   r   r   r   r9   r9   9   s    G-())r   z/admin/users)r3   r4   DELETEPUTc                     dt         vrt        t        d            S t        j                  dk(  rNt
        j                  j                         } t        | D cg c]  }|j                  |j                  d c}      S t        j                  dk(  rt        j                  }|j                  d      }|j                  d      }|rk|rit        |      }|j                  |       t        j                   j                  |       t        j                   j!                          t        d	d
i      dfS t        d	di      dfS t        j                  dk(  rt        j                  }|j                  d      }t
        j                  j#                  |      j%                         }|rLt        j                   j'                  |       t        j                   j!                          t        d	di      dfS t        d	di      dfS t        j                  dk(  rt        j                  }|j                  d      }|j                  d      }|j                  d      }t
        j                  j#                  |      j%                         }|rI|r||_
        |r|j                  |       t        j                   j!                          t        d	di      dfS t        d	di      dfS y c c}w )Nr8   r@   r3   )r0   r2   r4   r2   r%   r7   messagezUser created successfully   zUsername and password requiredi  rB   zUser deleted successfully   zUser not foundi  rC   new_usernamenew_passwordzUser updated successfully)r   r   r   r   r:   r   r<   allr   r0   r2   r;   getr&   r-   addcommitr=   r>   delete)	usersudatar2   r%   new_userr?   rH   rI   s	            r   manage_usersrS   @   sS   ()) ~~

 eLqtt<LMM 
6	!||88J'88J'X.H!!(+JJNN8$JJI'BCDcII	#CDEsJJ 
8	#||88J'zz##X#6<<>JJd#JJI'BCDcII	#345s:: 
5	 ||88J'xx/xx/zz##X#6<<> ,!!,/JJI'BCDcII	#345s:: 
!; Ms    Kz/reset_userc                     d} d}d}t         j                  j                  |       j                         }|r=t        j
                  j                  |       t        j
                  j                          t        |      }|j                  |       t        j
                  j                  |       t        j
                  j                          d| d| dS )Nold_usernamenewusernewpassword123r7   zUser 'z' created with password '')
r   r<   r=   r>   r-   r   rN   rM   r&   rL   )rU   rH   rI   old_userrR   s        r   
reset_userrZ   v   s     "LL#L zz##\#:@@BH


(#


 \*H,'JJNN8JJL>!:<.JJr   __main__T)debug)flaskr   r   r   r   r   r   flask_sqlalchemyr	   sqlalchemy.excr
   r   r*   app
secret_keyrouter   configr-   Modelr   r@   r9   rS   rZ   runr   r   r   <module>rf      s   E E ' ) Ho!3' ' )_

$ %/4

+ ,_\288 \ 8eV_- ., :* * >#CD3; E3;j =K K* zGG$G r   