a
    gh14                     @   sP  d dl Z d dlZd dlZd dlmZ d dlmZmZmZ d dl	m
Z
mZmZmZ d dl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Zd d
lmZ d dlmZ d dlmZ d dlmZ dZ e
dddZ!e!"ddej#ddiZ$e!"ddej#ddiZ%e& Z'e'j(ddedd e!"dej#dddej#ddd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/e!*d,G d-d+ d+eZ/e!*d.G d/d0 d0eZ0e!*d1G d2d3 d3eZ1e!*d4G d5d6 d6eZ2e!*d7G d8d9 d9eZ3dS ):    N)Image)	send_fileurl_forrender_template)	NamespaceResourcefieldsreqparse)jwt_requiredget_jwt_identity)jsonify)Profile)UserService)admin_required)bcrypt)create_access_token)MailService)FileStorageZuploadsZaccountzAccount operations)descriptionEmailemailT)requiredimagefiles)locationtyper   ZUsername)r   r   ZPassword)r   passwordz/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   sZ   t  }t|}|  |  |jr0|jdu r@t |_|  t|  t	| dS )z Endpoint that retreive single user based on User Token

        Args:
            Resource (strin): User Token

        Returns:
            json: json Data of user information
        N   )
r   r   get_user_by_emailZensure_client_rolesaveprofiler   printto_dictr   )selfcurrent_user_id	user_info r)    /app/app/resources/v1/account.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roles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_file	file_pathr=   r   keyvaluer)   r)   r*   patchG   s0    

$
zAccountMe.patchN)__name__
__module____qualname__r
   r7   docr+   rK   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 )z Endpoint that check user if exists or not based on user email

        Returns:
            json: json format of user boolean if exist or not
        r   NexistsTr    F)r7   r8   r   r!   )r&   datar(   r)   r)   r*   postu   s
    zAccountExist.postN)rL   rM   rN   r7   expectemail_modelrS   r)   r)   r)   r*   rP   s   s   rP   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    )msgrR   statuscodez1An error occurred while processing your request: i  )r   r   r!   	is_activeZtoggle_active_statusZdeletedarchive	ExceptionrF   rY   str)r&   r'   r(   er)   r)   r*   delete   s6    

z AccountDeactivateResource.deleteN)rL   rM   rN   r
   r_   r)   r)   r)   r*   rV      s   rV   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)ra   rc   rb   avatarColorrd   re   z19,860z-14negativezActive Userssuccessztabler:user-check)ra   rc   Ztrendrb   rg   rd   re   Z237z+42zPending Userswarningztabler:user-exclamationZstatsHorizontalWithDetailsr)   )r&   ra   r)   r)   r*   r+      s<    	
"zAccountStatsResource.getNrL   rM   rN   r+   r)   r)   r)   r*   r`      s   r`   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   r4   r;   r<   )r&   	file_nameZimage_directoryr)   r)   r*   r+      s    zImageResource.getNrk   r)   r)   r)   r*   rl      s   rl   z<string:username>/upload_imagec                   @   s   e Zd Zeedd ZdS )rl   c                 C   s   t  }|d }t|}|rt d|d  d|j }|| t|}|d rv||d _|d   ddidfS t|d	|d< |  ndd
idfS d S )Nr   /r.   _r#   messagezImage uploaded successfully   )r0   zUser not found  )	upload_parser
parse_argsr   Zget_user_by_email_or_usernamer6   filenamer"   r0   r   )r&   r.   argsZuploaded_fileuserrH   r)   r)   r*   rS      s    




zImageResource.postN)rL   rM   rN   r7   rT   rt   rS   r)   r)   r)   r*   rl      s   z/usersc                   @   s.   e Zd Ze e ejddddd ZdS )	UsersListr   Return all usersr   r   c                 C   s(   t  }g }|D ]}|| qt|S N)r   allappendr   )r&   ZusersZformated_datarx   r)   r)   r*   r+      s
    zUsersList.getN)rL   rM   rN   r
   r   r7   rO   r+   r)   r)   r)   r*   ry      s   ry   z/users/<public_id>c                   @   sH   e Zd Ze e ejddddd Zee	dd Z
dd	 Zd
S )UserInfor   rz   r{   c                 C   s"   t |}|rt|S ddidfS )Nrq   zUser Not Found!rs   )r   get_user_by_idr   r&   	public_idrx   r)   r)   r*   r+     s    
zUserInfo.getc                 C   s   t tj d S r|   )r$   r7   r8   )r&   r   r)   r)   r*   put  s    zUserInfo.putc                 C   s   t |}|  ddidfS )Nrq   zUser Are Deleted Success!r    )r   r   r_   r   r)   r)   r*   r_     s    
zUserInfo.deleteN)rL   rM   rN   r
   r   r7   rO   r+   rT   account_modelr   r_   r)   r)   r)   r*   r   
  s   
r   z/change-password/<token>c                   @   s$   e Zd Ze eedd ZdS )AccountChangePasswordResourcec                 C   sV   t j}|d}|d}t|}|rFtj|d |d}ddidfS ddid	fS d S )
Nr   r   r   )Zuser_idnew_passwordrq   zPassword updated successfullyr    zInvalid username or password  )r7   r8   r+   r   r!   Zchange_password)r&   tokenrR   r   r   current_userZnew_passr)   r)   r*   rS   #  s    


z"AccountChangePasswordResource.postN)rL   rM   rN   r
   r7   rT   
user_modelrS   r)   r)   r)   r*   r   !  s   r   z/forgot-passwordc                   @   s   e Zd Zeedd ZdS )ForgotPasswordResourcec                 C   s   t j}|d}|d}t|}td| ||jkr|j|j|j	|j
d}t|j|d}d| }tjd|jtd|d	d
d d|ddfS ddidfS d S )Nr.   r   zcurrent user: )is_superuserrZ   is_staffis_admin)identityadditional_claimsz0http://0.0.0.0:8002/api/account/change_password/z&Account Activation: Email Verificationzreset_password.html)Zpassword_reset_linkT)subjectZ	recipientrq   htmlz&Password reset link sent to your email)rq   
reset_linkr    rq   zInvalid username or emailr   )r7   r8   r+   r   r!   r$   r   r   rZ   r   r   r   r   Z
send_emailr   )r&   rR   r.   r   r   r   r   r   r)   r)   r*   rS   8  s*    






zForgotPasswordResource.postN)rL   rM   rN   r7   rT   r   rS   r)   r)   r)   r*   r   6  s   r   )4r4   r@   rB   PILr   flaskr   r   r   flask_restxr   r   r   r	   flask_jwt_extendedr
   r   r   Z
app.modelsr   Zapp.services.userr   Z	app.utilsr   jsonZapp.config.extensionsr   r   Zapp.services.mailr   Zwerkzeug.datastructuresr   r6   r7   modelStringrU   r   RequestParserrt   add_argumentr   router   rP   rV   r`   rl   ry   r   r   r   r)   r)   r)   r*   <module>   sZ   I'&