B
    £0H[›:  ã               @   sd  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	 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mZ eƒ ZG dd„ dejƒZ G dd„ dej!ƒZ"G dd„ dej#ƒZ$G dd„ dej%ƒZ&G dd„ dej%ƒZ'G dd„ dej(ƒZ)G dd„ dej(ƒZ*G dd„ dej(ƒZ+G dd„ de+ƒZ,G d d!„ d!ej(ƒZ-dS )"é    N)Úforms)ÚauthenticateÚget_user_modelÚpassword_validation)ÚUNUSABLE_PASSWORD_PREFIXÚidentify_hasher)ÚUser)Údefault_token_generator)Úget_current_site)ÚEmailMultiAlternatives)Úloader)Úforce_bytes)Úurlsafe_base64_encode)Úcapfirst)ÚgettextÚgettext_lazyc                   s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚReadOnlyPasswordHashWidgetz)auth/widgets/read_only_password_hash.htmlc       	         s¦   t ƒ  |||¡}g }|r"| t¡r6| dtdƒi¡ ndyt|ƒ}W n& tk
rh   | dtdƒi¡ Y n2X x.| |¡ 	¡ D ]\}}| t|ƒ|dœ¡ qzW ||d< |S )NÚlabelzNo password set.z5Invalid password format or unknown hashing algorithm.)r   ÚvalueÚsummary)
ÚsuperÚget_contextÚ
startswithr   Úappendr   r   Ú
ValueErrorZsafe_summaryÚitems)	ÚselfÚnamer   ÚattrsÚcontextr   ZhasherÚkeyZvalue_)Ú	__class__© ú]C:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6bm3nxem\django\django\contrib\auth\forms.pyr      s    z&ReadOnlyPasswordHashWidget.get_context)Ú__name__Ú
__module__Ú__qualname__Ztemplate_namer   Ú__classcell__r"   r"   )r!   r#   r      s   r   c                   s0   e Zd ZeZ‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚReadOnlyPasswordHashFieldc                s   |  dd¡ tƒ j||Ž d S )NÚrequiredF)Ú
setdefaultr   Ú__init__)r   ÚargsÚkwargs)r!   r"   r#   r+   .   s    z"ReadOnlyPasswordHashField.__init__c             C   s   |S )Nr"   )r   ÚdataÚinitialr"   r"   r#   Ú
bound_data2   s    z$ReadOnlyPasswordHashField.bound_datac             C   s   dS )NFr"   )r   r/   r.   r"   r"   r#   Úhas_changed7   s    z%ReadOnlyPasswordHashField.has_changed)	r$   r%   r&   r   Úwidgetr+   r0   r1   r'   r"   r"   )r!   r#   r(   +   s   r(   c                   s   e Zd Z‡ fdd„Z‡  ZS )ÚUsernameFieldc                s   t  dtƒ  |¡¡S )NÚNFKC)ÚunicodedataÚ	normalizer   Ú	to_python)r   r   )r!   r"   r#   r7   <   s    zUsernameField.to_python)r$   r%   r&   r7   r'   r"   r"   )r!   r#   r3   ;   s   r3   c                   s”   e Zd ZdZdedƒiZejedƒdeje	 
¡ d�Zejedƒejdedƒd	�ZG d
d„ dƒZ‡ fdd„Zdd„ Z‡ fdd„Zd‡ fdd„	Z‡  ZS )ÚUserCreationFormzc
    A form that creates a user, with no privileges, from the given username and
    password.
    Úpassword_mismatchz%The two password fields didn't match.ÚPasswordF)r   Ústripr2   Ú	help_textzPassword confirmationz4Enter the same password as before, for verification.)r   r2   r;   r<   c               @   s   e Zd ZeZdZdeiZdS )zUserCreationForm.Meta)Úusernamer=   N)r$   r%   r&   r   ÚmodelÚfieldsr3   Úfield_classesr"   r"   r"   r#   ÚMetaU   s   rA   c                s@   t ƒ j||Ž | jjj| jkr<| j| jjj jj ddi¡ d S )NÚ	autofocusT)	r   r+   Ú_metar>   ÚUSERNAME_FIELDr?   r2   r   Úupdate)r   r,   r-   )r!   r"   r#   r+   Z   s    zUserCreationForm.__init__c             C   s@   | j  d¡}| j  d¡}|r<|r<||kr<tj| jd dd�‚|S )NÚ	password1Ú	password2r9   )Úcode)Úcleaned_dataÚgetr   ÚValidationErrorÚerror_messages)r   rF   rG   r"   r"   r#   Úclean_password2_   s    z UserCreationForm.clean_password2c          
      sb   t ƒ  ¡  | j d¡}|r^yt || j¡ W n0 tjk
r\ } z|  	d|¡ W d d }~X Y nX d S )NrG   )
r   Ú_post_cleanrI   rJ   r   Úvalidate_passwordÚinstancer   rK   Z	add_error)r   ÚpasswordÚerror)r!   r"   r#   rN   i   s    
zUserCreationForm._post_cleanTc                s.   t ƒ jdd�}| | jd ¡ |r*| ¡  |S )NF)ÚcommitrF   )r   ÚsaveÚset_passwordrI   )r   rS   Úuser)r!   r"   r#   rT   t   s
    zUserCreationForm.save)T)r$   r%   r&   Ú__doc__Ú_rL   r   Ú	CharFieldÚPasswordInputr   Ú"password_validators_help_text_htmlrF   rG   rA   r+   rM   rN   rT   r'   r"   r"   )r!   r#   r8   @   s"   
r8   c                   sF   e Zd Zeedƒedƒd�ZG dd„ dƒZ‡ fdd„Zdd	„ Z‡  Z	S )
ÚUserChangeFormr:   z�Raw passwords are not stored, so there is no way to see this user's password, but you can change the password using <a href="{}">this form</a>.)r   r<   c               @   s   e Zd ZeZdZdeiZdS )zUserChangeForm.MetaÚ__all__r=   N)r$   r%   r&   r   r>   r?   r3   r@   r"   r"   r"   r#   rA   †   s   rA   c                sN   t ƒ j||Ž | jd j d¡| jd _| j d¡}|d k	rJ|j d¡|_d S )NrQ   z../password/Zuser_permissionsÚcontent_type)r   r+   r?   r<   ÚformatrJ   ZquerysetZselect_related)r   r,   r-   Úf)r!   r"   r#   r+   ‹   s
    zUserChangeForm.__init__c             C   s
   | j d S )NrQ   )r/   )r   r"   r"   r#   Úclean_password’   s    zUserChangeForm.clean_password)
r$   r%   r&   r(   rX   rQ   rA   r+   ra   r'   r"   r"   )r!   r#   r\   |   s   
r\   c                   s‚   e Zd ZdZedejddid�d�Zeje	dƒdej
d	�Ze	d
ƒe	dƒdœZd‡ fdd„	Zdd„ Zdd„ Zdd„ Zdd„ Z‡  ZS )ÚAuthenticationFormzs
    Base class for authenticating users. Extend this to get a form that accepts
    username/password logins.
    éþ   rB   T)r   )Ú
max_lengthr2   r:   F)r   r;   r2   z^Please enter a correct %(username)s and password. Note that both fields may be case-sensitive.zThis account is inactive.)Úinvalid_loginÚinactiveNc                sR   || _ d| _tƒ j||Ž tj tj¡| _| j	d j
dkrNt| jjƒ| j	d _
dS )z‘
        The 'request' parameter is set for custom auth use by subclasses.
        The form data comes in via the standard 'data' kwarg.
        Nr=   )ÚrequestÚ
user_cacher   r+   Ú	UserModelrC   Ú	get_fieldrD   Úusername_fieldr?   r   r   Úverbose_name)r   rg   r,   r-   )r!   r"   r#   r+   °   s    zAuthenticationForm.__init__c             C   sr   | j  d¡}| j  d¡}|d k	rl|rlt| j||d�| _| jd kr`tj| jd dd| jj	id�‚n|  
| j¡ | j S )Nr=   rQ   )r=   rQ   re   )rH   Úparams)rI   rJ   r   rg   rh   r   rK   rL   rk   rl   Úconfirm_login_allowed)r   r=   rQ   r"   r"   r#   Úclean¾   s    
zAuthenticationForm.cleanc             C   s   |j stj| jd dd�‚dS )a›  
        Controls whether the given User may log in. This is a policy setting,
        independent of end-user authentication. This default behavior is to
        allow login by active users, and reject login by inactive users.

        If the given user cannot log in, this method should raise a
        ``forms.ValidationError``.

        If the given user may log in, this method should return None.
        rf   )rH   N)Ú	is_activer   rK   rL   )r   rV   r"   r"   r#   rn   Ï   s    z(AuthenticationForm.confirm_login_allowedc             C   s   | j r| j jS d S )N)rh   Úid)r   r"   r"   r#   Úget_user_idà   s    zAuthenticationForm.get_user_idc             C   s   | j S )N)rh   )r   r"   r"   r#   Úget_userå   s    zAuthenticationForm.get_user)N)r$   r%   r&   rW   r3   r   Z	TextInputr=   rY   rX   rZ   rQ   rL   r+   ro   rn   rr   rs   r'   r"   r"   )r!   r#   rb   ™   s    
rb   c            	   @   sL   e Zd Zejedƒdd�Zddd„Zdd„ Zdd	d
de	ddddf	dd„Z
dS )ÚPasswordResetFormZEmailrc   )r   rd   Nc             C   sb   t  ||¡}d | ¡ ¡}t  ||¡}t||||gƒ}	|dk	rVt  ||¡}
|	 |
d¡ |	 ¡  dS )zO
        Send a django.core.mail.EmailMultiAlternatives to `to_email`.
        Ú Nz	text/html)r   Zrender_to_stringÚjoinÚ
splitlinesr   Zattach_alternativeÚsend)r   Úsubject_template_nameÚemail_template_namer   Ú
from_emailZto_emailÚhtml_email_template_nameÚsubjectÚbodyZemail_messageZ
html_emailr"   r"   r#   Ú	send_mailì   s    zPasswordResetForm.send_mailc             C   s,   t jjf dt  ¡  |ddiŽ}dd„ |D ƒS )a  Given an email, return matching user(s) who should receive a reset.

        This allows subclasses to more easily customize the default policies
        that prevent inactive users and users with unusable passwords from
        resetting their password.
        z
%s__iexactrp   Tc             s   s   | ]}|  ¡ r|V  qd S )N)Zhas_usable_password)Ú.0Úur"   r"   r#   ú	<genexpr>  s    z.PasswordResetForm.get_users.<locals>.<genexpr>)ri   Z_default_managerÚfilterZget_email_field_name)r   ÚemailZactive_usersr"   r"   r#   Ú	get_usersý   s    
zPasswordResetForm.get_usersz'registration/password_reset_subject.txtz&registration/password_reset_email.htmlFc
          	   C   sœ   | j d }
xŒ|  |
¡D ]~}|s4t|ƒ}|j}|j}n| }}|
||tt|jƒƒ ¡ || 	|¡|rddnddœ}|	dk	r~| 
|	¡ | j|||||
|d� qW dS )zf
        Generate a one-use only link for resetting password and send it to the
        user.
        r„   ÚhttpsÚhttp)r„   ÚdomainÚ	site_nameÚuidrV   ÚtokenÚprotocolN)r|   )rI   r…   r
   r   rˆ   r   r   ÚpkÚdecodeZ
make_tokenrE   r   )r   Zdomain_overridery   rz   Z	use_httpsZtoken_generatorr{   rg   r|   Zextra_email_contextr„   rV   Zcurrent_siter‰   rˆ   r   r"   r"   r#   rT   
  s&    


zPasswordResetForm.save)N)r$   r%   r&   r   Z
EmailFieldrX   r„   r   r…   r	   rT   r"   r"   r"   r#   rt   é   s   
rt   c                   sp   e Zd ZdZdedƒiZejedƒejde	 
¡ d�Zejedƒdejd�Z‡ fd	d
„Zdd„ Zddd„Z‡  ZS )ÚSetPasswordFormza
    A form that lets a user change set their password without entering the old
    password
    r9   z%The two password fields didn't match.zNew passwordF)r   r2   r;   r<   zNew password confirmation)r   r;   r2   c                s   || _ tƒ j||Ž d S )N)rV   r   r+   )r   rV   r,   r-   )r!   r"   r#   r+   A  s    zSetPasswordForm.__init__c             C   sN   | j  d¡}| j  d¡}|r<|r<||kr<tj| jd dd�‚t || j¡ |S )NÚnew_password1Únew_password2r9   )rH   )rI   rJ   r   rK   rL   r   rO   rV   )r   rF   rG   r"   r"   r#   Úclean_new_password2E  s    z#SetPasswordForm.clean_new_password2Tc             C   s*   | j d }| j |¡ |r$| j ¡  | jS )Nr�   )rI   rV   rU   rT   )r   rS   rQ   r"   r"   r#   rT   Q  s
    

zSetPasswordForm.save)T)r$   r%   r&   rW   rX   rL   r   rY   rZ   r   r[   r�   r‘   r+   r’   rT   r'   r"   r"   )r!   r#   r�   -  s   
r�   c               @   sX   e Zd ZdZeejfdedƒiŽZej	edƒdej
ddid�d	�Zd
ddgZdd„ ZdS )ÚPasswordChangeFormz[
    A form that lets a user change their password by entering their old
    password.
    Úpassword_incorrectzAYour old password was entered incorrectly. Please enter it again.zOld passwordFrB   T)r   )r   r;   r2   Úold_passwordr�   r‘   c             C   s.   | j d }| j |¡s*tj| jd dd�‚|S )zB
        Validate that the old_password field is correct.
        r•   r”   )rH   )rI   rV   Zcheck_passwordr   rK   rL   )r   r•   r"   r"   r#   Úclean_old_passwordi  s    
z%PasswordChangeForm.clean_old_passwordN)r$   r%   r&   rW   Údictr�   rL   rX   r   rY   rZ   r•   Zfield_orderr–   r"   r"   r"   r#   r“   Y  s   
r“   c                   s”   e Zd ZdZdedƒiZdZejedƒej	ddid�d	e
 ¡ d
�Zejedƒej	d	edƒd
�Z‡ fdd„Zdd„ Zddd„Ze‡ fdd„ƒZ‡  ZS )ÚAdminPasswordChangeFormzN
    A form used to change the password of a user in the admin interface.
    r9   z%The two password fields didn't match.r)   r:   rB   T)r   F)r   r2   r;   r<   zPassword (again)z4Enter the same password as before, for verification.c                s   || _ tƒ j||Ž d S )N)rV   r   r+   )r   rV   r,   r-   )r!   r"   r#   r+   ‹  s    z AdminPasswordChangeForm.__init__c             C   sN   | j  d¡}| j  d¡}|r<|r<||kr<tj| jd dd�‚t || j¡ |S )NrF   rG   r9   )rH   )rI   rJ   r   rK   rL   r   rO   rV   )r   rF   rG   r"   r"   r#   rM   �  s    z'AdminPasswordChangeForm.clean_password2c             C   s*   | j d }| j |¡ |r$| j ¡  | jS )zSave the new password.rF   )rI   rV   rU   rT   )r   rS   rQ   r"   r"   r#   rT   ›  s
    

zAdminPasswordChangeForm.savec                s*   t ƒ j}x| jD ]}||krg S qW dgS )NrQ   )r   Úchanged_datar?   )r   r.   r   )r!   r"   r#   r™   £  s
    z$AdminPasswordChangeForm.changed_data)T)r$   r%   r&   rW   rX   rL   Zrequired_css_classr   rY   rZ   r   r[   rF   rG   r+   rM   rT   Úpropertyr™   r'   r"   r"   )r!   r#   r˜   v  s"   
r˜   ).r5   Zdjangor   Zdjango.contrib.authr   r   r   Zdjango.contrib.auth.hashersr   r   Zdjango.contrib.auth.modelsr   Zdjango.contrib.auth.tokensr	   Zdjango.contrib.sites.shortcutsr
   Zdjango.core.mailr   Zdjango.templater   Zdjango.utils.encodingr   Zdjango.utils.httpr   Zdjango.utils.textr   Zdjango.utils.translationr   r   rX   ri   ZWidgetr   ZFieldr(   rY   r3   Z	ModelFormr8   r\   ZFormrb   rt   r�   r“   r˜   r"   r"   r"   r#   Ú<module>   s.   <PD,