a
    "
h                     @   s   d dl mZmZmZ d dlmZ d dlmZ d dlm	Z	 d dl
Z
d dlmZ d dlZd dlmZ d dlmZmZmZ e ZeeZeeZe Zed	d
Zdd Zd dlmZ d dl
Z
edeefedddZedeefedddZdS )    )	APIRouterRequestDepends)RedirectResponse)Jinja2Templates)HTTP_302_FOUNDN)datetime)get_current_user)get_pingconf_vars
open_mysqlget_mysql_creds	templates)	directoryc                  C   sf   ddl m} m}m} | t6}|jdd}|d | }|  W d    n1 sX0    Y  |S )Nr   )r   r   r
   T)
dictionaryz7SELECT CatNum, CatName FROM Categories ORDER BY CatName)	utils.pingdatar   r   r
   mysql_credscursorexecutefetchallclose)r   r   r
   connr   results r   ./wd/v2025.freedom4um.com/routes/postarticle.pyget_categories_from_db   s    

&r   z/postarticle)requestc                    sn   |st dtdS t }dd tddD }ttdd}td| t |||j	|j
|jd	d	d	d	d	d	i |d
S )N/signinurlstatus_codec                 S   s   g | ]}|t j| d qS )numnamecalendar
month_abbr.0ir   r   r   
<listcomp>)       z$postarticle_form.<locals>.<listcomp>          postarticle.html r   
categoriesmonthsdayspublish_yearpublish_monthpublish_daytitlecategorysource
url_sourceauthorarticleerrorsuser)r   r   r   nowrangelistr   TemplateResponser   yearmonthday)r   r>   current_dater2   r3   r   r   r   postarticle_form"   s0    rG   c                    s  |st dtdS |  I d H }|dd }|d}|dd }|dd }|dd }|d	}|d
}	|d}
|dd }| dt|	ddt|
d}i }|sd|d< |sd|d< |sd|d< |rBdd tddD }ttdd}t	 }t
d| t |||j|j|j||||||||dS t	 d}zrttF}| }d}|||d ||||||||f	 |  W d    n1 s0    Y  W |r|  n|r|  0 t dtdS )Nr   r   r7   r/   r8   r9   r:   r;   r4   r5   r6   r<   -02dzTitle is required.zAuthor is required.zArticle content is required.contentc                 S   s   g | ]}|t j| d qS r    r#   r&   r   r   r   r)   ^   r*   z&postarticle_submit.<locals>.<listcomp>r+   r,   r-   r.   r0   z%Y-%m-%d %H:%M:%Sz
            INSERT INTO Articles
            (HandleID, CatNum, PostDate, PubDate, URL, Source, Author, Title, Article)
            VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)
            idz/readarticle)r   r   formgetstripintr@   rA   r   r?   r   rB   r   rC   rD   rE   strftimer   r   r   r   commitr   )r   r>   rL   r7   r8   r9   r:   r;   Zpub_yearZ	pub_monthZpub_dayZarticle_htmlZpub_dater=   r2   r3   rF   Z	post_dater   r   sqlr   r   r   postarticle_submitC   s    



 
*

rS   )fastapir   r   r   fastapi.responsesr   fastapi.templatingr   Zstarlette.statusr   r$   r   loggingZ
utils.authr	   r   r
   r   r   PingConfr   	getLogger__name__loggerrouterr   r   rM   rG   postrS   r   r   r   r   <module>   s(   

 