U
    >i<                     @   s$  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	 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 d d
lmZ d dlmZ d dlmZ d dlmZ eeddG dd deZe Zeeddee	ddG dd deZe Z eeddG dd deZ!e! Z"dS )    )login_required)Http404)reverse_lazy)method_decorator)never_cache)TemplateView)FormView)app_settings)reauthentication_required)Authenticator)GenerateRecoveryCodesForm)flows)get_form_classdispatch)namec                       sR   e Zd ZeZdej ZedZ	 fddZ
 fddZ fddZd	d
 Z  ZS )GenerateRecoveryCodesViewzmfa/recovery_codes/generate.Zmfa_view_recovery_codesc                    s   t | j t |S N)r   generate_recovery_codesrequestsuper
form_valid)selfform	__class__ D/tmp/pip-unpacked-wheel-upujnpc2/allauth/mfa/recovery_codes/views.pyr      s    z$GenerateRecoveryCodesView.form_validc                    sN   t  jf |}g }tjj| jjtjjd	 }|r>|
  }t||d< |S )N)usertypeZunused_code_count)r   get_context_datar   Zobjectsfilterr   r   TypeZRECOVERY_CODESfirstwrapget_unused_codeslen)r   kwargsretunused_codesauthenticatorr   r   r   r      s     
z*GenerateRecoveryCodesView.get_context_datac                    s   t   }| jj|d< |S )Nr   )r   get_form_kwargsr   r   )r   r'   r   r   r   r*   '   s    
z)GenerateRecoveryCodesView.get_form_kwargsc                 C   s   t tjd| jS )Nr   )r   r	   ZFORMS
form_class)r   r   r   r   r   ,   s
      z(GenerateRecoveryCodesView.get_form_class)__name__
__module____qualname__r   r+   account_settingsTEMPLATE_EXTENSIONtemplate_namer   Zsuccess_urlr   r   r*   r   __classcell__r   r   r   r   r      s   
r   c                       s<   e Zd ZdZdZ fddZ fddZ fddZ  ZS )	DownloadRecoveryCodesViewzmfa/recovery_codes/download.txtz
text/plainc                    sJ   t | j| _| jst | j  | _| js6t S t j	|f||S r   )
r   view_recovery_codesr   r)   r   r#   r$   r(   r   r   )r   r   argsr&   r   r   r   r   ;   s    z"DownloadRecoveryCodesView.dispatchc                    s   t  jf |}| j|d< |S )Nr(   )r   r   r(   )r   r&   r'   r   r   r   r   D   s    
z*DownloadRecoveryCodesView.get_context_datac                    s   t  j|f|}d|d< |S )Nz)attachment; filename="recovery-codes.txt"zContent-Disposition)r   render_to_response)r   contextZresponse_kwargsresponser   r   r   r6   I   s    z,DownloadRecoveryCodesView.render_to_response)	r,   r-   r.   r1   content_typer   r   r6   r2   r   r   r   r   r3   5   s
   	r3   c                       s&   e Zd Zdej Z fddZ  ZS )ViewRecoveryCodesViewzmfa/recovery_codes/index.c                    sB   t  jf |}t| j}|s$t ||  t	j
d |S )N)r(   Ztotal_count)r   r   r   r4   r   r   updater#   r$   r	   ZRECOVERY_CODE_COUNT)r   r&   r'   r)   r   r   r   r   V   s    
z&ViewRecoveryCodesView.get_context_data)r,   r-   r.   r/   r0   r1   r   r2   r   r   r   r   r:   R   s   
r:   N)#Zdjango.contrib.auth.decoratorsr   Zdjango.httpr   Zdjango.urlsr   Zdjango.utils.decoratorsr   Zdjango.views.decorators.cacher   Zdjango.views.genericr   Zdjango.views.generic.editr   Zallauth.accountr	   r/   Zallauth.account.decoratorsr
   Zallauth.mfaZallauth.mfa.modelsr   Z allauth.mfa.recovery_codes.formsr   Z#allauth.mfa.recovery_codes.internalr   Zallauth.utilsr   r   Zas_viewr   r3   Zdownload_recovery_codesr:   r4   r   r   r   r   <module>   s.   



