o
    Ah
                     @   s   d dl Z d dlZd dlmZ d dlZd dlZd dlmZ d dlmZ d dl	m
Z
mZmZmZ dd Zd dlZdZd	d
 Zdd Zdd Zdd Zdd ZdS )    N)datetime)	AdminUserwraps)requestjsonifycurrent_appsessionc                       t   fdd}|S )Nc                     s8   t jd}|tjd krtddidfS  | i |S )N	x-api-keyAPI_KEYerrorUnauthorized access  )r   headersgetr   configr   argskwargsZapi_keyf 5/home/www/bk.finsightngr.online/FinSight/app/utils.pydecorated_function   s   z+require_api_key.<locals>.decorated_functionr   r   r   r   r   r   require_api_key   s   r   zl1NiIsInR5cCI6IkpXVCJ9.eyJmcmVzaCI6ZmFsc2UsImlhdCI6MTcyOTQzNDM2MCwianRpIjoiNGU1NDdjZTQtNmY2MC00MjgyLWI5YWItNzc                    r
   )Nc                     sV   t jdkrdS t jdd}|rt|ts$tj	d t
ddidfS  | i |S )	NOPTIONS)    r   r   z3Unauthorized access (missing or invalid x-api-key).r   r   r   )r   methodr   r   hmaccompare_digestr   r   loggerwarningr   r   r   r   r   wrapper   s   
z!require_api_key2.<locals>.wrapperr   )r   r%   r   r   r   require_api_key2   s   r&   c                 C   sD  d}d}d}d}z}t |c}||| z|| W n+ t jyG } zdt|v r=td| d || || W Y d }~nd }~ww t| d}|	d	t
j|  | W d    n1 sew   Y  W d    n1 stw   Y  d
| dt
j|  W S  ty } ztdt|  W Y d }~d S d }~ww )Nzftps-s1.uk.cloudlogin.cozfindev_finsightngr.onlinez769ramTFJ-rz/uploads550z
Directory z does not exist, creating it.rbzSTOR z"https://finsightngr.online/uploads/zError during FTP upload: )ftplibFTPlogincwd
error_permstrprintZmkdopenZ
storbinaryospathbasename	Exception)	file_pathZftp_hostZftp_usernameZftp_passwordZftp_directoryftpefiler   r   r   upload_image_to_ftp3   s6   

r:   c                 C   sF   |  d| d| dt   }t|  }d|  d| d| S )N_z,https://bk.finsightngr.online:30976/content/r)   )r   utcnowhashlibmd5encode	hexdigest)
content_idscheduled_datesubscriber_idZunique_stringZunique_hashr   r   r   generate_unique_linkY   s   rD   c                    r
   )Nc                     s&   dt vrtddidfS  | i |S )Nadmin_user_idr   z(Unauthorized. Please log in as an admin.  )r	   r   )r   r   r   r   r   r   j   s   z*login_required.<locals>.decorated_functionr   r   r   r   r   login_requiredi   s   rG   c                     s    fdd}|S )Nc                    s   t   fdd}|S )Nc                     sT   dt vrtddidfS tjt d }|r|jvr#tddidfS  | i |S )NrE   r   zUnauthorized. Please log in.rF   z(Access denied. Insufficient permissions.r   )r	   r   r   queryr   role)r   r   Z
admin_user)r   required_rolesr   r   wrapped_functionu   s   z:role_required.<locals>.decorator.<locals>.wrapped_functionr   )r   rK   rJ   r   r   	decoratort   s   	z role_required.<locals>.decoratorr   )rJ   rM   r   rL   r   role_requireds   s   rN   )r*   r2   r   r=   pytz
app.modelsr   	functoolsr   flaskr   r   r   r	   r   r!   r   r&   r:   rD   rG   rN   r   r   r   r   <module>   s     
&
