B
    -
d=                 @   sx  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 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+Z0dS )%    N)forms)authenticateget_user_modelpassword_validation)UNUSABLE_PASSWORD_PREFIXidentify_hasher)User)default_token_generator)get_current_site)ValidationError)EmailMultiAlternatives)loader)force_bytes)urlsafe_base64_encode)capfirst)gettextgettext_lazyc             C   s    t d|  t d| 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-install-nbqhn9mb/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  |||}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__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                   s    e Zd ZeZ fddZ  ZS )ReadOnlyPasswordHashFieldc                s*   | dd | dd t j|| d S )NrequiredFdisabledT)
setdefaultr    __init__)r&   argskwargs)r+   r   r   r4   9   s    z"ReadOnlyPasswordHashField.__init__)r,   r-   r.   r   widgetr4   r/   r   r   )r+   r   r0   6   s   r0   c                   s(   e Zd Z fddZ fddZ  ZS )UsernameFieldc                s   t dt |S )Nr   )r   r   r    	to_python)r&   r   )r+   r   r   r9   @   s    zUsernameField.to_pythonc                s   t  |dddS )Nnoneusername)Zautocapitalizeautocomplete)r    widget_attrs)r&   r7   )r+   r   r   r=   C   s    
zUsernameField.widget_attrs)r,   r-   r.   r9   r=   r/   r   r   )r+   r   r8   ?   s   r8   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	
 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   stripr7   	help_textzPassword confirmationz4Enter the same password as before, for verification.)r   r7   rA   rB   c               @   s   e Zd ZeZdZdeiZdS )zUserCreationForm.Meta)r;   r;   N)r,   r-   r.   r   modelfieldsr8   field_classesr   r   r   r   Meta`   s   rF   c                s:   t  j|| | jjj| jkr6d| j| jjj jjd< d S )NT	autofocus)r    r4   _metarC   USERNAME_FIELDrD   r7   r(   )r&   r5   r6   )r+   r   r   r4   e   s    zUserCreationForm.__init__c             C   s>   | j d}| j d}|r:|r:||kr:t| jd dd|S )N	password1	password2r?   )code)cleaned_datagetr   error_messages)r&   rJ   rK   r   r   r   clean_password2j   s    z UserCreationForm.clean_password2c          
      s`   t    | jd}|r\yt|| j W n. tk
rZ } z| d| W d d }~X Y nX d S )NrK   )	r    _post_cleanrM   rN   r   validate_passwordinstancer   Z	add_error)r&   passworderror)r+   r   r   rQ   t   s    
zUserCreationForm._post_cleanTc                s.   t  jdd}|| jd  |r*|  |S )NF)commitrJ   )r    saveset_passwordrM   )r&   rV   user)r+   r   r   rW      s
    zUserCreationForm.save)T)r,   r-   r.   __doc___rO   r   	CharFieldPasswordInputr   "password_validators_help_text_htmlrJ   rK   rF   r4   rP   rQ   rW   r/   r   r   )r+   r   r>   K   s"   
r>   c                   s>   e Zd ZeededdZG dd dZ fddZ  ZS )UserChangeFormr@   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   rB   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   rC   rD   r8   rE   r   r   r   r   rF      s   rF   c                sN   t  j|| | jd}|r,|jd|_| jd}|rJ|jd|_d S )NrT   z../password/user_permissionscontent_type)r    r4   rD   rN   rB   formatZquerysetZselect_related)r&   r5   r6   rT   ra   )r+   r   r   r4      s    zUserChangeForm.__init__)	r,   r-   r.   r0   r[   rT   rF   r4   r/   r   r   )r+   r   r_      s   
r_   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.
    rG   T)r(   )r7   r@   Fr<   zcurrent-password)r   rA   r7   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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    r4   	UserModelrH   	get_fieldrI   username_field
max_lengthrD   r7   r(   r   r   verbose_name)r&   rh   r5   r6   Zusername_max_length)r+   r   r   r4      s    zAuthenticationForm.__init__c             C   s\   | j d}| j d}|d k	rV|rVt| j||d| _| jd krJ|  n| | j | j S )Nr;   rT   )r;   rT   )rM   rN   r   rh   ri   get_invalid_login_errorconfirm_login_allowed)r&   r;   rT   r   r   r   clean   s    

zAuthenticationForm.cleanc             C   s   |j st| 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
        ``ValidationError``.

        If the given user may log in, this method should return None.
        rf   )rL   N)	is_activer   rO   )r&   rY   r   r   r   rp      s    z(AuthenticationForm.confirm_login_allowedc             C   s   | j S )N)ri   )r&   r   r   r   get_user   s    zAuthenticationForm.get_userc             C   s   t | jd dd| jjidS )Nre   r;   )rL   params)r   rO   rl   rn   )r&   r   r   r   ro      s    z*AuthenticationForm.get_invalid_login_error)N)r,   r-   r.   rZ   r8   r   Z	TextInputr;   r\   r[   r]   rT   rO   r4   rq   rp   rs   ro   r/   r   r   )r+   r   rd      s   rd   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Emailrg   r<   email)r(   )r   rm   r7   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                s6   t  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__iexactrr   Tc             3   s*   | ]"}|  rt t|r|V  qd S )N)Zhas_usable_passwordr   getattr).0u)rv   email_field_namer   r   	<genexpr>  s    z.PasswordResetForm.get_users.<locals>.<genexpr>)rj   get_email_field_nameZ_default_managerfilter)r&   rv   Zactive_usersr   )rv   r   r   	get_users  s
    
zPasswordResetForm.get_usersz'registration/password_reset_subject.txtz&registration/password_reset_email.htmlFc
          	   C   s   | j d }
|s$t|}|j}|j}n| }}t }xf| |
D ]X}t||}|||tt	|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.
        rv   httpshttp)rv   domain	site_nameuidrY   tokenprotocol)r~   N)rM   r
   r'   r   rj   r   r   r   r   r   pkZ
make_tokenr   )r&   Zdomain_overrider{   r|   Z	use_httpsZtoken_generatorr}   rh   r~   Zextra_email_contextrv   Zcurrent_siter   r   r   rY   Z
user_emailr)   r   r   r   rW     s(    



zPasswordResetForm.save)N)r,   r-   r.   r   Z
EmailFieldr[   Z
EmailInputrv   r   r   r	   rW   r   r   r   r   ru      s   
ru   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	
 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
    r?   u'   The two password fields didn’t match.zNew passwordr<   znew-password)r(   F)r   r7   rA   rB   zNew password confirmation)r   rA   r7   c                s   || _ t j|| d S )N)rY   r    r4   )r&   rY   r5   r6   )r+   r   r   r4   P  s    zSetPasswordForm.__init__c             C   sL   | j d}| j d}|r:|r:||kr:t| jd ddt|| j |S )Nnew_password1new_password2r?   )rL   )rM   rN   r   rO   r   rR   rY   )r&   rJ   rK   r   r   r   clean_new_password2T  s    z#SetPasswordForm.clean_new_password2Tc             C   s*   | j d }| j| |r$| j  | jS )Nr   )rM   rY   rX   rW   )r&   rV   rT   r   r   r   rW   `  s
    

zSetPasswordForm.save)T)r,   r-   r.   rZ   r[   rO   r   r\   r]   r   r^   r   r   r4   r   rW   r/   r   r   )r+   r   r   <  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<   rG   )r(   )r   rA   r7   old_passwordr   r   c             C   s,   | j d }| j|s(t| jd dd|S )zB
        Validate that the old_password field is correct.
        r   r   )rL   )rM   rY   Zcheck_passwordr   rO   )r&   r   r   r   r   clean_old_passwordy  s    
z%PasswordChangeForm.clean_old_passwordN)r,   r-   r.   rZ   r   rO   r[   r   r\   r]   r   Zfield_orderr   r   r   r   r   r   h  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
 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.
    r?   u'   The two password fields didn’t match.r1   r@   znew-passwordT)r<   rG   )r(   F)r   r7   rA   rB   zPassword (again)r<   z4Enter the same password as before, for verification.c                s   || _ t j|| d S )N)rY   r    r4   )r&   rY   r5   r6   )r+   r   r   r4     s    z AdminPasswordChangeForm.__init__c             C   sL   | j d}| j d}|r:|r:||kr:t| jd ddt|| j |S )NrJ   rK   r?   )rL   )rM   rN   r   rO   r   rR   rY   )r&   rJ   rK   r   r   r   rP     s    z'AdminPasswordChangeForm.clean_password2c             C   s*   | j d }| j| |r$| j  | jS )zSave the new password.rJ   )rM   rY   rX   rW   )r&   rV   rT   r   r   r   rW     s
    

zAdminPasswordChangeForm.savec                s*   t  j}x| jD ]}||krg S qW dgS )NrT   )r    changed_datarD   )r&   datar'   )r+   r   r   r     s
    z$AdminPasswordChangeForm.changed_data)T)r,   r-   r.   rZ   r[   rO   Zrequired_css_classr   r\   r]   r   r^   rJ   rK   r4   rP   rW   propertyr   r/   r   r   )r+   r   r     s"   
r   )1r   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.exceptionsr   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[   rj   r   ZWidgetr   ZFieldr0   r\   r8   Z	ModelFormr>   r_   ZFormrd   ru   r   r   r   r   r   r   r   <module>   s2   		<NN,