a
    Lex                     @   s  d dl Z d dlZd dlZd dlmZ d dlmZmZ d dlm	Z	m
Z
mZ d dlmZmZ d dlmZ d dlmZ e	dd	d
ZeddejddiZdZedG dd de
ZedG dd de
ZedG dd de
ZedG dd de
ZedG dd de
ZdS )    N)Image)	send_fileurl_for)	NamespaceResourcefields)jwt_requiredget_jwt_identity)Profile)UserServiceZaccountzAccount operations)descriptionEmailemailT)requiredZuploadsz/mec                   @   s4   e Zd Ze ejdddd Ze dd ZdS )	AccountMeBearer_Token)securityc                 C   sL   t  }t|}|  |  |jr0|jd u r@t |_|  | dfS )N   )r	   r   get_user_by_emailZensure_client_rolesaveprofiler
   to_dict)selfcurrent_user_id	user_info r   /app/app/resources/v1/user.pyget   s    
zAccountMe.getc              	   C   s4  t jtdd t }tj}t|}dD ]}||v r(t|||	| q(dD ]}|	|d  qLd|v r|	d}d|v rt j
t|d }n dd l}t j
t|  d}d	|v rttt|d	 d
d }|| d|d  |d< | D ]"\}	}
t|j|	rt|j|	|
 q|  | dfS )NT)exist_ok)Z
first_name	last_nameusername)Z
updated_attagsZrolesZavatarnamer   z.pngZ
base64file,   z)http://173.249.58.66:8082/account/avatar/r   )osmakedirsUPLOAD_FOLDERr	   account_namespacepayloadr   r   setattrpoppathjoinuuiduuid4r   openioBytesIObase64	b64decodesplitr   itemshasattrr   r   )r   r   Zuser_update_jsonr   fieldZavatar_fileZ	file_pathr.   imagekeyvaluer   r   r   patch'   s0    

$
zAccountMe.patchN)__name__
__module____qualname__r   r(   docr   r<   r   r   r   r   r      s
   
r   z/existc                   @   s   e Zd Zeedd ZdS )AccountExistc                 C   s<   t j}t|d }|r,|d ur,ddidfS ddidfS d S )Nr   existsTr   F)r(   r)   r   r   )r   datar   r   r   r   postU   s
    zAccountExist.postN)r=   r>   r?   r(   expectemail_modelrD   r   r   r   r   rA   S   s   rA   z/deactivatec                   @   s   e Zd Ze dd ZdS )AccountDeactivateResourcec              
   C   s   z>t  }t|}|jr |  |js.|  dd dddfW S  ty } z\t|dr|j	d urt
|d |j	d|j	fW  Y d }~S dt
| d dddfW  Y d }~S d }~0 0 d S )NzYour account has been deactivated. Please contact our administrator within 72 hours to discuss account reactivation. Failure to do so may result in permanent account deletion.r   )msgrC   statuscodez1An error occurred while processing your request: i  )r	   r   r   Z	is_activeZtoggle_active_statusZdeletedarchive	Exceptionr7   rJ   str)r   r   r   er   r   r   deleted   s6    

z AccountDeactivateResource.deleteN)r=   r>   r?   r   rO   r   r   r   r   rG   b   s   rG   z/statsc                   @   s   e Zd Zdd ZdS )AccountStatsResourcec              
   C   sL   ddddddddd	d
ddddddddddddddddddg}d|iS )Nz21,459ZSessionz+29ztabler:userzTotal Users)statstitle	trendDifficonsubtitlez4,567z+18z
Paid Userserrorztabler:user-pluszLast week analytics)rQ   rS   rR   avatarColorrT   rU   z19,860z-14negativezActive Userssuccessztabler:user-check)rQ   rS   ZtrendrR   rW   rT   rU   Z237z+42zPending Userswarningztabler:user-exclamationZstatsHorizontalWithDetailsr   )r   rQ   r   r   r   r      s<    	
"zAccountStatsResource.getNr=   r>   r?   r   r   r   r   r   rP      s   rP   z/avatar/<file_name>c                   @   s   e Zd Zdd ZdS )ImageResourcec                 C   s   d}t tj||ddS )Nz/app/uploadsz	image/png)mimetype)r   r%   r,   r-   )r   	file_nameZimage_directoryr   r   r   r      s    zImageResource.getNr[   r   r   r   r   r\      s   r\   )r%   r1   r3   PILr   flaskr   r   flask_restxr   r   r   flask_jwt_extendedr   r	   Z
app.modelsr
   Zapp.services.userr   r(   modelStringrF   r'   router   rA   rG   rP   r\   r   r   r   r   <module>   s*   ?''