o
    7f                     @   s   d dl m Z  ddlmZmZmZmZmZ d dlZd dlZd dl	m
Z
mZmZ ddlmZ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d	d
 ZdS )    )datetime   )dbmigrateloginbcryptoauthN)Flaskredirecturl_for)r   r   r   r   r   mailcache)make_celery)Configc                  C   s  t t} | jt tjtjt	}tj
|d}t| t|}t|}W d    n1 s3w   Y  t|  tjd|d d |d d dddid	}t|  tj| td
d t|  t|  t|  t|  t|  |   ddlm} | | t| }|| _W d    n1 sw   Y  tdd}tj|stj |d
d t!j"t!j#tj
|dd d"dd}	dd }
dd }|| j$j%d< |	| j$j%d< |
| j$j%d< tj&dd }tj'd d! }| S )#NzoctCreds.jsongoogleweb	client_idclient_secretz<https://accounts.google.com/.well-known/openid-configurationscopezopenid profile email)namer   r   server_metadata_urlclient_kwargsT)render_as_batchr   )mainLOG_DIRlogs)exist_okzapp.log)levelfilename%H:%Mr   c                 S   sx   | d u rdS t | ttfrtt| t| }nt | tr(|dkr'| dS nt| d}|dkr7|dS ||S )N datez%m/%d/%Yz%Y-%m-%d %H:%M:%Sdayz%A)
isinstanceintfloatr   fromtimestampstrftimestrptime)valueformattimezone_offsetadjusted_time r-   #/home/ubuntu/webapp/app/__init__.pydatetimeformat=   s   



z"create_app.<locals>.datetimeformatc                    s   t  fdd|D S )Nc                 3   s    | ]}  |V  qd S )N)has_role).0roleuserr-   r.   	<genexpr>P   s    z3create_app.<locals>.has_any_role.<locals>.<genexpr>)any)r4   rolesr-   r3   r.   has_any_roleO   s   z create_app.<locals>.has_any_rolec                 S   s
   d | S )z
        Format a number as currency.

        :param value: The number to format.
        :return: The formatted currency string.
        z${:,.2f})r*   )r)   r-   r-   r.   format_currencyR   s   
z#create_app.<locals>.format_currencycurrencyr/   r8   c                 S   s*   ddl m} td|j|  |j| S )Nr   )UserzID:)
app.modelsr;   printqueryget)idr;   r-   r-   r.   	load_userb   s   zcreate_app.<locals>.load_userc                   S   s   t tdS )Nz
main.login)r
   r   r-   r-   r-   r.   unauthorized_callbackh   s   z)create_app.<locals>.unauthorized_callback)r   r   )(r	   __name__configfrom_objectr   ospathdirnameabspath__file__joinr=   openjsonloadr   init_appregisterr   r   r   r   r   r   app_contextroutesr   register_blueprintr   celerygetenvexistsmakedirsloggingbasicConfigDEBUG	jinja_envfiltersuser_loaderunauthorized_handler)appbase_dir
creds_pathfsecretsr   main_bprT   r   r/   r8   r9   rA   rB   r-   r-   r.   
create_app   sZ   












	
	

re   )r   
extensionsr   r   r   r   r   rX   rF   flaskr	   r
   r   r   r   rT   r   rD   r   rM   re   r-   r-   r-   r.   <module>   s    $