3
W­E^è?  ã               @   sl  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dd„ 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             C   s    t jd| ƒjƒ t jd|ƒjƒ kS )z¡
    Perform case-insensitive comparison of two identifiers, using the
    recommended algorithm from Unicode Technical Report 36, section
    2.11.2(B)(2).
    ÚNFKC)ÚunicodedataÚ	normalizeÚcasefold)Ús1Ús2© r   ú;/tmp/pip-build-78s3knkw/Django/django/contrib/auth/forms.pyÚ_unicode_ci_compare   s    r   c                   s$   e Zd ZdZdZ‡ fdd„Z‡  ZS )ÚReadOnlyPasswordHashWidgetz)auth/widgets/read_only_password_hash.htmlTc       	         s¨   t ƒ j|||ƒ}g }| s$|jtƒr8|jdtdƒiƒ ndyt|ƒ}W n& tk
rj   |jdtdƒiƒ Y n2X x.|j|ƒj	ƒ D ]\}}|jt|ƒ|dœƒ q|W ||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__r   r   r    $   s    z&ReadOnlyPasswordHashWidget.get_context)Ú__name__Ú
__module__Ú__qualname__Ztemplate_nameZ	read_onlyr    Ú__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   |j ddƒ tƒ j||Ž d S )NÚrequiredF)Ú
setdefaultr   Ú__init__)r%   ÚargsÚkwargs)r*   r   r   r2   8   s    z"ReadOnlyPasswordHashField.__init__c             C   s   |S )Nr   )r%   ÚdataÚinitialr   r   r   Ú
bound_data<   s    z$ReadOnlyPasswordHashField.bound_datac             C   s   dS )NFr   )r%   r6   r5   r   r   r   Úhas_changedA   s    z%ReadOnlyPasswordHashField.has_changed)	r+   r,   r-   r   Úwidgetr2   r7   r8   r.   r   r   )r*   r   r/   5   s   r/   c                   s(   e Zd Z‡ fdd„Z‡ fdd„Z‡  ZS )ÚUsernameFieldc                s   t jdtƒ j|ƒƒS )Nr   )r   r   r   Ú	to_python)r%   r   )r*   r   r   r;   F   s    zUsernameField.to_pythonc                s   t ƒ j|ƒdddœ–S )NÚnoneÚusername)ZautocapitalizeÚautocomplete)r   Úwidget_attrs)r%   r9   )r*   r   r   r?   I   s    
zUsernameField.widget_attrs)r+   r,   r-   r;   r?   r.   r   r   )r*   r   r:   E   s   r:   c                   s¨   e Zd ZdZdedƒiZejedƒdejddid�e	j
ƒ d	�Zejed
ƒejddid�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_mismatchu'   The two password fields didnâ€™t match.ÚPasswordFr>   znew-password)r'   )r   Ústripr9   Ú	help_textzPassword confirmationz4Enter the same password as before, for verification.)r   r9   rC   rD   c               @   s   e Zd ZeZdZdeiZdS )zUserCreationForm.Metar=   N)r=   )r+   r,   r-   r   ÚmodelÚfieldsr:   Úfield_classesr   r   r   r   ÚMetaf   s   rH   c                s:   t ƒ j||Ž | jjj| jkr6d| j| jjj jjd< d S )NTÚ	autofocus)r   r2   Ú_metarE   ÚUSERNAME_FIELDrF   r9   r'   )r%   r3   r4   )r*   r   r   r2   k   s    zUserCreationForm.__init__c             C   s@   | j jdƒ}| j jdƒ}|r<|r<||kr<tj| jd dd�‚|S )NÚ	password1Ú	password2rA   )Úcode)Úcleaned_dataÚgetr   ÚValidationErrorÚerror_messages)r%   rL   rM   r   r   r   Úclean_password2p   s    z UserCreationForm.clean_password2c                sb   t ƒ jƒ  | jjdƒ}|r^ytj|| jƒ W n0 tjk
r\ } z| j	d|ƒ W Y d d }~X nX d S )NrM   )
r   Ú_post_cleanrO   rP   r   Úvalidate_passwordÚinstancer   rQ   Z	add_error)r%   ÚpasswordÚerror)r*   r   r   rT   z   s    
zUserCreationForm._post_cleanTc                s.   t ƒ jdd�}|j| jd ƒ |r*|jƒ  |S )NF)ÚcommitrL   )r   ÚsaveÚset_passwordrO   )r%   rY   Úuser)r*   r   r   rZ   …   s
    zUserCreationForm.save)T)r+   r,   r-   Ú__doc__Ú_rR   r   Ú	CharFieldÚPasswordInputr   Ú"password_validators_help_text_htmlrL   rM   rH   r2   rS   rT   rZ   r.   r   r   )r*   r   r@   Q   s"   
r@   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 )
ÚUserChangeFormrB   u‘   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   rD   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   rE   rF   r:   rG   r   r   r   r   rH   —   s   rH   c                sN   t ƒ j||Ž | jjdƒ}|r,|jjdƒ|_| jjdƒ}|rJ|jjdƒ|_d S )NrW   z../password/Úuser_permissionsÚcontent_type)r   r2   rF   rP   rD   ÚformatZquerysetZselect_related)r%   r3   r4   rW   rd   )r*   r   r   r2   œ   s    zUserChangeForm.__init__c             C   s   | j jdƒS )NrW   )r6   rP   )r%   r   r   r   Úclean_password¥   s    zUserChangeForm.clean_password)
r+   r,   r-   r/   r^   rW   rH   r2   rg   r.   r   r   )r*   r   rb   �   s   
	rb   c                   sŠ   e Zd ZdZeejddid�d�Zeje	dƒdej
dd	id�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.
    rI   T)r'   )r9   rB   Fr>   zcurrent-password)r   rC   r9   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                s|   || _ d| _tƒ j||Ž tjjtjƒ| _| jj	p4d}|| j
d _	|| j
d jjd< | j
d jdkrxt| 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.
        Néþ   r=   Z	maxlength)ÚrequestÚ
user_cacher   r2   Ú	UserModelrJ   Ú	get_fieldrK   Úusername_fieldÚ
max_lengthrF   r9   r'   r   r   Úverbose_name)r%   rl   r3   r4   Zusername_max_length)r*   r   r   r2   À   s    zAuthenticationForm.__init__c             C   s\   | j jdƒ}| j jdƒ}|d k	rV|rVt| j||d�| _| jd krJ| jƒ ‚n| j| jƒ | j S )Nr=   rW   )r=   rW   )rO   rP   r   rl   rm   Úget_invalid_login_errorÚconfirm_login_allowed)r%   r=   rW   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.
        rj   )rN   N)Ú	is_activer   rQ   rR   )r%   r\   r   r   r   rt   Þ   s    z(AuthenticationForm.confirm_login_allowedc             C   s   | j S )N)rm   )r%   r   r   r   Úget_userï   s    zAuthenticationForm.get_userc             C   s   t j| jd dd| jjid�S )Nri   r=   )rN   Úparams)r   rQ   rR   rp   rr   )r%   r   r   r   rs   ò   s    z*AuthenticationForm.get_invalid_login_error)N)r+   r,   r-   r]   r:   r   Z	TextInputr=   r_   r^   r`   rW   rR   r2   ru   rt   rw   rs   r.   r   r   )r*   r   rh   ¬   s   rh   c            	   @   sZ   e Zd Zejedƒdejddi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Emailrk   r>   Úemail)r'   )r   rq   r9   Nc             C   sb   t j||ƒ}dj|jƒ ƒ}t j||ƒ}t||||gƒ}	|dk	rVt j||ƒ}
|	j|
dƒ |	jƒ  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                s6   t jƒ ‰t jjf dˆ ˆ ddiŽ}‡ ‡f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__iexactrv   Tc             3   s*   | ]"}|j ƒ rtˆ t|ˆƒƒr|V  qd S )N)Zhas_usable_passwordr   Úgetattr)Ú.0Úu)rz   Úemail_field_namer   r   ú	<genexpr>  s    z.PasswordResetForm.get_users.<locals>.<genexpr>)rn   Úget_email_field_nameZ_default_managerÚfilter)r%   rz   Zactive_usersr   )rz   r‰   r   Ú	get_users  s
    
zPasswordResetForm.get_usersz'registration/password_reset_subject.txtz&registration/password_reset_email.htmlFc
          	   C   s    | j d }
tjƒ }xˆ| j|
ƒD ]z}|s<t|ƒ}|j}|j}n| }}t||ƒ}|||tt	|j
ƒƒ||j|ƒ|rrdnddœ|	p~i –}| j||||||d� qW dS )zf
        Generate a one-use only link for resetting password and send it to the
        user.
        rz   ÚhttpsÚhttp)rz   ÚdomainÚ	site_nameÚuidr\   ÚtokenÚprotocol)r‚   N)rO   rn   r‹   r�   r
   r&   r�   r†   r   r   ÚpkZ
make_tokenr…   )r%   Zdomain_overrider   r€   Z	use_httpsZtoken_generatorr�   rl   r‚   Zextra_email_contextrz   r‰   r\   Zcurrent_siter‘   r�   Z
user_emailr(   r   r   r   rZ   $  s(    



zPasswordResetForm.save)N)r+   r,   r-   r   Z
EmailFieldr^   Z
EmailInputrz   r…   r�   r	   rZ   r   r   r   r   ry   ú   s   
ry   c                   s„   e Zd ZdZdedƒiZejedƒejddid�de	j
ƒ d	�Zejed
ƒdejddid�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
    rA   u'   The two password fields didnâ€™t match.zNew passwordr>   znew-password)r'   F)r   r9   rC   rD   zNew password confirmation)r   rC   r9   c                s   || _ tƒ j||Ž d S )N)r\   r   r2   )r%   r\   r3   r4   )r*   r   r   r2   \  s    zSetPasswordForm.__init__c             C   sN   | j jdƒ}| j jdƒ}|r<|r<||kr<tj| jd dd�‚tj|| jƒ |S )NÚnew_password1Únew_password2rA   )rN   )rO   rP   r   rQ   rR   r   rU   r\   )r%   rL   rM   r   r   r   Úclean_new_password2`  s    z#SetPasswordForm.clean_new_password2Tc             C   s*   | j d }| jj|ƒ |r$| jjƒ  | jS )Nr—   )rO   r\   r[   rZ   )r%   rY   rW   r   r   r   rZ   l  s
    

zSetPasswordForm.save)T)r+   r,   r-   r]   r^   rR   r   r_   r`   r   ra   r—   r˜   r2   r™   rZ   r.   r   r   )r*   r   r–   H  s   r–   c               @   sV   e Zd ZdZejdedƒi–Zejedƒdej	dddœ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 passwordFzcurrent-passwordT)r>   rI   )r'   )r   rC   r9   Úold_passwordr—   r˜   c             C   s.   | j d }| jj|ƒs*tj| jd dd�‚|S )zB
        Validate that the old_password field is correct.
        rœ   r›   )rN   )rO   r\   Zcheck_passwordr   rQ   rR   )r%   rœ   r   r   r   Úclean_old_password…  s    
z%PasswordChangeForm.clean_old_passwordN)r+   r,   r-   r]   r–   rR   r^   r   r_   r`   rœ   Zfield_orderr�   r   r   r   r   rš   t  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dœd	�d
e
jƒ d�Zejedƒej	ddid	�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.
    rA   u'   The two password fields didnâ€™t match.r0   rB   znew-passwordT)r>   rI   )r'   F)r   r9   rC   rD   zPassword (again)r>   z4Enter the same password as before, for verification.c                s   || _ tƒ j||Ž d S )N)r\   r   r2   )r%   r\   r3   r4   )r*   r   r   r2   §  s    z AdminPasswordChangeForm.__init__c             C   sN   | j jdƒ}| j jdƒ}|r<|r<||kr<tj| jd dd�‚tj|| jƒ |S )NrL   rM   rA   )rN   )rO   rP   r   rQ   rR   r   rU   r\   )r%   rL   rM   r   r   r   rS   «  s    z'AdminPasswordChangeForm.clean_password2c             C   s*   | j d }| jj|ƒ |r$| jjƒ  | jS )zSave the new password.rL   )rO   r\   r[   rZ   )r%   rY   rW   r   r   r   rZ   ·  s
    

zAdminPasswordChangeForm.savec                s*   t ƒ j}x| jD ]}||krg S qW dgS )NrW   )r   Úchanged_datarF   )r%   r5   r&   )r*   r   r   rŸ   ¿  s
    z$AdminPasswordChangeForm.changed_data)T)r+   r,   r-   r]   r^   rR   Zrequired_css_classr   r_   r`   r   ra   rL   rM   r2   rS   rZ   ÚpropertyrŸ   r.   r   r   )r*   r   rž   ’  s"   
rž   )/r   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   r^   rn   r   ZWidgetr   ZFieldr/   r_   r:   Z	ModelFormr@   rb   ZFormrh   ry   r–   rš   rž   r   r   r   r   Ú<module>   s0   	<NN,