3
^u                 @   s  d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	m
Z
 d dlmZmZmZ d dlmZmZmZmZ d dlmZmZ d dlmZmZmZmZ ejdejd	d
d Zejddd Zejdddgddd Zejdddgddd Z ejddd Z!dd Z"ejdddgdedd Z#ejd dgdd!d" Z$ejd#ddgded$d% Z%ejd&d'd( Z&ejd)ddgded*d+ Z'ejd,dgded-d. Z(dS )/    N)Image)render_templateurl_forflashredirectrequestabort)appdbbcrypt)RegistrationForm	LoginFormUpdateAccountFormPostForm)UserPost)
login_usercurrent_userlogout_userlogin_required/z/homec              C   s   t jj } td| dS )Nz	home.html)posts)r   queryallr   )r    r   `/home/kgabalins/Documents/prakses_projekts/simple-project/project/Flask_Blog/flaskblog/routes.pyhome
   s    
r   z/aboutc               C   s   t dddS )Nz
about.htmlZAbout)title)r   r   r   r   r   about   s    r   z	/registerGETPOST)methodsc              C   s   t jrttdS t } | j rvtj| jj	j
d}t| jj	| jj	|d}tjj| tjj  tdd ttdS tdd| d	S )
Nr   zutf-8)usernameemailpasswordz.Your account has been registered successfully!successloginzregister.htmlZRegister)r   form)r   is_authenticatedr   r   r   validate_on_submitr   generate_password_hashr$   datadecoder   r"   r#   r
   sessionaddcommitr   r   )r'   hashed_passworduserr   r   r   register   s    

r2   z/loginc              C   s   t jrttdS t } | j rtjj| j	j
dj }|rtj|j| jj
rt|| jj
d tjjd}|rtt|S ttdS tdd tdd| d	S )
Nr   )r#   )remembernextz<Login Unsuccessful. Please check your email and/or password!Zdangerz
login.htmlZLogin)r   r'   )r   r(   r   r   r   r)   r   r   	filter_byr#   r+   firstr   check_password_hashr$   r   r3   r   argsgetr   r   )r'   r1   Z	next_pager   r   r   r&   &   s    
r&   z/logoutc               C   s   t   ttdS )Nr   )r   r   r   r   r   r   r   logout7   s    r:   c             C   s\   t jd}tjj| j\}}|| }tjjtjd|}d}t	j
| }|j| |j| |S )N   zstatic/profile_pics}   )r<   r<   )secretsZ	token_hexospathsplitextfilenamejoinr	   	root_pathr   openZ	thumbnailsave)Zform_pictureZ
random_hex_Zf_extZ
picture_fnZpicture_pathZoutput_sizeir   r   r   save_picture>   s    



rH   z/accountc              C   s   t  } | j r\| jjr(t| jj}|t_| jjt_| jjt_t	j
j  tdd ttdS tjdkrztj| j_tj| j_tddtj d}tdd	|| td
S )Nz+Your account has been updated successfully!r%   accountr   staticzprofile_pics/)rA   zaccount.htmlAccount)r   
image_filer'   r1   )r   r)   Zpicturer+   rH   r   rL   r"   r#   r
   r-   r/   r   r   r   r   methodr   )r'   Zpicture_filerL   r   r   r   rI   N   s    






rI   z/account/<user_name>c             C   sP   t jj| dj }|r@|tkr@tdd|j d}tdd||dS ttdS d	S )
N)r"   rJ   zprofile_pics/)rA   zaccount.htmlrK   )r   rL   r1   rI   zThis user doesn't exist!)	r   r   r5   r6   r   r   rL   r   r   )Z	user_namer1   rL   r   r   r   accountViewd   s    rN   z	/post/newc              C   s`   t  } | j rPt| jj| jjtd}tjj	| tjj
  tdd ttdS tddd| dS )N)r   contentauthorz(Your post has been created successfully!r%   r   zcreate_post.htmlzNew Post)r   legendr'   )r   r)   r   r   r+   rO   r   r
   r-   r.   r/   r   r   r   r   )r'   postr   r   r   new_posto   s    

rS   z/post/<int:post_id>c             C   s   t jj| }td|j|dS )Nz	post.html)r   rR   )r   r   
get_or_404r   r   )post_idrR   r   r   r   rR      s    rR   z/post/<int:post_id>/updatec             C   s   t jj| }|jtkrtd t }|j rf|jj	|_|j
j	|_
tjj  tdd ttd|jdS tjdkr|j|j_	|j
|j
_	tddd|d	S )
Ni  z(Your post has been updated successfully!r%   rR   )rU   r   zcreate_post.htmlzUpdate Post)r   rQ   r'   )r   r   rT   rP   r   r   r   r)   r   r+   rO   r
   r-   r/   r   r   r   idr   rM   r   )rU   rR   r'   r   r   r   update_post   s    







rW   z/post/<int:post_id>/deletec             C   sJ   t jj| }|jtkrtd tjj| tjj	  t
dd ttdS )Ni  z(Your post has been deleted successfully!r%   r   )r   r   rT   rP   r   r   r
   r-   deleter/   r   r   r   )rU   rR   r   r   r   delete_post   s    


rY   ))r=   r>   ZPILr   flaskr   r   r   r   r   r   	flaskblogr	   r
   r   Zflaskblog.formsr   r   r   r   Zflaskblog.modelsr   r   flask_loginr   r   r   r   router   r   r2   r&   r:   rH   rI   rN   rS   rR   rW   rY   r   r   r   r   <module>   s.    