o
    Ah/                     @   s  d dl mZmZmZ d dlmZmZmZmZm	Z	 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 d dlZd dlZd dlZed	eZe Zed
dZejddgdedd Zejddgddd Zejddgdedd Zejddgdedd Zejddgdedd Zejddgdedd Zd d! Z ejd"dgded#d$ Z!ejd%dgded&d' Z"ejd(dgded)d* Z#d+d, Z$ejd-dgded.d/ Z%dS )0    )	Blueprintrequestjsonify)dbContentSchedule
SubscriberSentMessage)ContentSchema)upload_image_to_ftp)require_api_key)generate_unique_link)datetime	timedeltaNcontentT)manyz/contentPOST)methodsc               
   C   s   t  } td zAt| }t|d |d |d|d|dd}tj	
| tj	  d|j }||_tj	  td	 t|d
fW S  typ } ztdt|  tdt|idfW  Y d }~S d }~ww )Nz"Received request to create contentheadlinecontent_body	video_urlhead_bannersummary)r   r   r   r   r   z,https://bk.finsightngr.online:30976/content/zContent created successfully   zError creating content: error  )r   get_jsonlogginginfocontent_schemaloadr   getr   sessionaddcommitidlinkdump	Exceptionr   strr   )	json_dataZcontent_dataZnew_contentZclient_linke r,   E/home/www/bk.finsightngr.online/FinSight/app/routes/content_routes.pycreate_content   s.   




 r.   z/upload_imagec               
   C   s   zAdt jvrtddidfW S t jd } | jdkr"tddidfW S tjd| j}| | t|}t	| td|d	d
fW S  t
y] } ztdt|idfW  Y d }~S d }~ww )Nfiler   zNo file partr    zNo selected filez/tmpzFile uploaded successfully)messageurl     )r   filesr   filenameospathjoinsaver   remover(   r)   )r/   Z	file_pathZftp_urlr+   r,   r,   r-   upload_image>   s   




 r<   GETc                  C   s   t jdd } t jjddtd}t jjddtd}tj}| r+|tjd|  d}|j	||d}d	d
 |j
D }t||j|j|jdS )Nsearchpage   typeper_page
   %r?   rC   c              	   S   s*   g | ]}|j |j|j|j|j|jd qS )r%   r   
created_at
updated_atr&   r   rG   ).0r   r,   r,   r-   
<listcomp>o   s    
z get_contents.<locals>.<listcomp>)contentstotalr?   rC   )r   argsr!   intr   queryfilterr   ilikepaginateitemsr   rM   r?   rC   )r>   r?   rC   rP   rL   resultr,   r,   r-   get_contents`   s    	rV   z/content/<int:content_id>c                 C   s.   t j| }t|j|j|j|j|j|j	dS )N)r   r   r   r   r   r&   )
r   rP   
get_or_404r   r   r   r   r   r   r&   )
content_idr   r,   r,   r-   get_content   s   rY   PUTc              
   C   s   t j| }t }zt|}| D ]
\}}t||| qt	j
  t|W S  tyE } ztdt|idfW  Y d }~S d }~ww )Nr   r   )r   rP   rW   r   r   r   r    rT   setattrr   r"   r$   r'   r(   r   r)   )rX   r   r*   Zupdated_contentkeyvaluer+   r,   r,   r-   update_content   s   

 r^   DELETEc              
   C   s   zBt j| }tjj| d }|D ]}tj| qt	jj| d }|D ]}tj| q&tj| tj
  tddidfW S  tyk } ztdt|  tj  tddidfW  Y d }~S d }~ww )	NrX   r1   zDContent and related schedules and sent messages deleted successfully   zError deleting content: r   zFailed to delete contentr4   )r   rP   rW   r   	filter_byallr   r"   deleter	   r$   r   r(   r   r   r)   rollback)rX   r   	schedulesschedulesent_messagesZsent_messager+   r,   r,   r-   delete_content   s"   

ri   c              
   C   s   z=t | t |krtddidfW S t| |D ]\}}tj|}t||j|d}tj	
| qtj	  tddidfW S  tya } ztj	  tddt| id	fW  Y d }~S d }~ww )
Nr   z9Mismatch in the number of content IDs and scheduled datesr   )rX   r   dater1   zContent scheduled successfullyr3   zError scheduling content: r4   )lenr   zipr   rP   rW   r   r   r   r"   r#   r$   r(   re   r)   )content_idsscheduled_datesrX   Zscheduled_dater   rg   r+   r,   r,   r-   schedule_content   s   

&ro   z/schedule_contentc                  C   s>   t  } | d}| d}|r|stddidfS t||S )Nrm   rn   r   z,Content IDs and scheduled dates are requiredr   )r   r   r!   r   ro   )r*   rm   rn   r,   r,   r-   schedule_content_endpoint   s   


rp   z /check_schedule/<int:content_id>c              
   C   s   zt jj| d }|rtddidfW S tddidfW S  tyD } ztd|  dt|  tdd	id
fW  Y d }~S d }~ww )Nr`   statusZ	scheduledr3   znot scheduledz'Error checking schedule for content_id z: r   zFailed to check scheduler4   )	r   rP   rb   firstr   r(   r   r   r)   )rX   rg   r+   r,   r,   r-   check_schedule   s   rs   z
/schedulesc                  C   sX   t jjddtd} t jjddtd}tjj| |d}tdd |jD |j	|j
|jd	S )
Nr?   r@   rA   rC   rD   rF   c                 S   s&   g | ]}|j |j|j|j|jd qS )r%   rX   r   rj   scheduled_atrt   )rJ   rg   r,   r,   r-   rK     s    
z!get_schedules.<locals>.<listcomp>)rf   rM   r?   rC   )r   rN   r!   rO   r   rP   rS   r   rT   rM   r?   rC   )r?   rC   rf   r,   r,   r-   get_schedules
  s   rv   c               
   C   s  zt jdd} t }tj|}|t	d}| dkr&|t
dd }n|}td|d  tjj| d	 }|sUtd
|d d tddidfW S |D ])}tj|j}tjjdd }|D ]}t|j|j|j}	|	|_tj| qkqWtj  tdt| d tddidfW S  t y }
 ztj!  t"dt#|
  tddidfW  Y d }
~
S d }
~
ww )NdaytodayzAfrica/LagosZtomorrowr@   )dayszSelected date in WAT: z%Y-%m-%d)rj   zNo scheduled content found for .r1   z1No scheduled content found for the selected date.i  T)subscribeStatusz$Successfully updated news links for z active subscribers.z:Content links successfully updated for active subscribers.r3   z/Error in triggering daily content link update: r   z%Error processing content link update.r4   )$r   rN   r!   r   utcnowpytzutclocalize
astimezonetimezoner   r   r   strftimer   rP   rb   rj   rc   r   r   rW   rX   r   r   r%   	news_linkr   r"   r#   r$   rk   r(   re   r   r)   )Zselected_dayZutc_timeZ
local_timeZselected_dateZscheduled_contentZcontent_scheduler   Zactive_subscribersZ
subscriberZunique_linkr+   r,   r,   r-   !trigger_daily_content_link_update*  s<   
	
r   z/trigger_daily_contentc                   C   s   t  S )N)r   r,   r,   r,   r-   'trigger_daily_content_link_update_routea  s   r   )&flaskr   r   r   
app.modelsr   r   r   r   r	   Zapp.schemasr
   Z	app.utilsr   r   r   r   r   r}   r7   r   __name__
content_bpr   Zcontents_schemarouter.   r<   rV   rY   r^   ri   ro   rp   rs   rv   r   r   r,   r,   r,   r-   <module>   sT   

&
!! 7