

_V*8                 @   s   d  d l  m 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 m Z d  d l m Z d  d l m Z d  d l  m! Z! d  d l" m# Z# m$ Z% Gd d   d e j&  Z' Gd d   d e j(  Z) Gd d   d e j*  Z+ Gd d   d e j*  Z, Gd d   d e j-  Z. Gd d   d e j-  Z/ Gd d   d e j-  Z0 Gd d    d  e0  Z1 Gd! d"   d" e j-  Z2 d# S)$    )unicode_literals)forms)authenticateget_user_modelpassword_validation)UNUSABLE_PASSWORD_PREFIXidentify_hasher)User)default_token_generator)get_current_site)EmailMultiAlternatives)flatatt)loader)force_bytes)format_htmlformat_html_join)urlsafe_base64_encode)	mark_safe)capfirst)ugettextugettext_lazyc               @   s   e  Z d  Z d d   Z d S)ReadOnlyPasswordHashWidgetc             C   s   | } |  j  |  } | s+ | j t  rD t d t d   } ni y t |  } Wn( t k
 r~ t d t d   } Yn/ Xt d d d d   | j |  j	   D  } t
 d t |  |  S)	Nz<strong>%s</strong>zNo password set.z5Invalid password format or unknown hashing algorithm. z<strong>{}</strong>: {} c             s   s'   |  ] \ } } t  |  | f Vq d  S)N)r   ).0keyvalue r   ;/tmp/pip-build-ghmbqnp_/Django/django/contrib/auth/forms.py	<genexpr>(   s   z4ReadOnlyPasswordHashWidget.render.<locals>.<genexpr>z<div{}>{}</div>)Zbuild_attrs
startswithr   r   r   r   
ValueErrorr   Zsafe_summaryitemsr   r   )selfnamer   attrsencodedZfinal_attrssummaryZhasherr   r   r   render   s    		z!ReadOnlyPasswordHashWidget.renderN)__name__
__module____qualname__r'   r   r   r   r   r      s   r   c                   s@   e  Z d  Z e Z   f d d   Z d d   Z d d   Z   S)ReadOnlyPasswordHashFieldc                s-   | j  d d  t t |   j | |   d  S)NrequiredF)
setdefaultsuperr+   __init__)r"   argskwargs)	__class__r   r   r/   2   s    z"ReadOnlyPasswordHashField.__init__c             C   s   | S)Nr   )r"   datainitialr   r   r   
bound_data6   s    z$ReadOnlyPasswordHashField.bound_datac             C   s   d S)NFr   )r"   r4   r3   r   r   r   has_changed;   s    z%ReadOnlyPasswordHashField.has_changed)r(   r)   r*   r   widgetr/   r5   r6   r   r   )r2   r   r+   /   s   r+   c                   s   e  Z d  Z d Z i e d  d 6Z e j d e d  d e j  Z	 e j d e d  d e j d e d	   Z
 Gd
 d   d  Z d d   Z d   f d d  Z   S)UserCreationFormzc
    A form that creates a user, with no privileges, from the given username and
    password.
    z%The two password fields didn't match.password_mismatchlabelPasswordr7   zPassword confirmation	help_textz4Enter the same password as before, for verification.c               @   s   e  Z d  Z e Z d Z d S)zUserCreationForm.MetausernameN)zusername)r(   r)   r*   r	   modelfieldsr   r   r   r   MetaM   s   r@   c             C   s   |  j  j d  } |  j  j d  } | r[ | r[ | | k r[ t j |  j d d d  n  |  j  j d  |  j _ t j |  j  j d  |  j  | S)N	password1	password2r9   coder=   )	cleaned_datagetr   ValidationErrorerror_messagesinstancer=   r   validate_password)r"   rA   rB   r   r   r   clean_password2Q   s    z UserCreationForm.clean_password2Tc                sF   t  t |   j d d  } | j |  j d  | rB | j   n  | S)NcommitFrA   )r.   r8   saveset_passwordrD   )r"   rK   user)r2   r   r   rL   ]   s
    zUserCreationForm.save)r(   r)   r*   __doc___rG   r   	CharFieldPasswordInputrA   rB   r@   rJ   rL   r   r   )r2   r   r8   ?   s   	r8   c                   sb   e  Z d  Z e d e d  d e d   Z Gd d   d  Z   f d d   Z d	 d
   Z   S)UserChangeFormr:   r;   r<   zRaw passwords are not stored, so there is no way to see this user's password, but you can change the password using <a href="../password/">this form</a>.c               @   s   e  Z d  Z e Z d Z d S)zUserChangeForm.Meta__all__N)r(   r)   r*   r	   r>   r?   r   r   r   r   r@   k   s   r@   c                sS   t  t |   j | |   |  j j d  } | d  k	 rO | j j d  | _ n  d  S)NZuser_permissionscontent_type)r.   rS   r/   r?   rE   ZquerysetZselect_related)r"   r0   r1   f)r2   r   r   r/   o   s    zUserChangeForm.__init__c             C   s   |  j  d S)Npassword)r4   )r"   r   r   r   clean_passwordu   s    zUserChangeForm.clean_password)	r(   r)   r*   r+   rP   rW   r@   r/   rX   r   r   )r2   r   rS   e   s
   rS   c                   s   e  Z d  Z d Z e j d d  Z e j d e d  d e j  Z	 i e d  d 6e d	  d
 6Z
 d   f d d  Z d d   Z d d   Z d d   Z d d   Z   S)AuthenticationFormzs
    Base class for authenticating users. Extend this to get a form that accepts
    username/password logins.
    
max_length   r:   r;   r7   z^Please enter a correct %(username)s and password. Note that both fields may be case-sensitive.invalid_loginzThis account is inactive.inactiveNc                s   | |  _  d |  _ t t |   j | |   t   } | j j | j  |  _	 |  j
 d j d k r t |  j	 j  |  j
 d _ n  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.   rY   r/   r   Z_meta	get_fieldZUSERNAME_FIELDusername_fieldr?   r:   r   verbose_name)r"   r^   r0   r1   Z	UserModel)r2   r   r   r/      s    			zAuthenticationForm.__init__c             C   s   |  j  j d  } |  j  j d  } | r | r t d | d |  |  _ |  j d  k r t j |  j d d d d i |  j j d 6 q |  j	 |  j  n  |  j  S)Nr=   rW   r\   rC   params)
rD   rE   r   r_   r   rF   rG   ra   rb   confirm_login_allowed)r"   r=   rW   r   r   r   clean   s    zAuthenticationForm.cleanc             C   s,   | j  s( t j |  j d d d  n  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.
        r]   rC   N)	is_activer   rF   rG   )r"   rN   r   r   r   rd      s    	z(AuthenticationForm.confirm_login_allowedc             C   s   |  j  r |  j  j Sd  S)N)r_   id)r"   r   r   r   get_user_id   s    	
zAuthenticationForm.get_user_idc             C   s   |  j  S)N)r_   )r"   r   r   r   get_user   s    zAuthenticationForm.get_user)r(   r)   r*   rO   r   rQ   r=   rP   rR   rW   rG   r/   re   rd   rh   ri   r   r   )r2   r   rY   |   s   !rY   c               @   sp   e  Z d  Z e j d e d  d d  Z d d d  Z d d	   Z d d
 d d e	 d d d d d d 	 Z
 d S)PasswordResetFormr:   ZEmailrZ   r[   Nc             C   s   t  j | |  } d j | j    } t  j | |  } t | | | | g  }	 | d k	 r t  j | |  }
 |	 j |
 d  n  |	 j   d S)zP
        Sends a django.core.mail.EmailMultiAlternatives to `to_email`.
        r   Nz	text/html)r   Zrender_to_stringjoin
splitlinesr   Zattach_alternativesend)r"   subject_template_nameemail_template_namecontext
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 d | d d  } 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email__iexactrf   Tc             s   s!   |  ] } | j    r | Vq d  S)N)Zhas_usable_password)r   ur   r   r   r      s    z.PasswordResetForm.get_users.<locals>.<genexpr>)r   Z_default_managerfilter)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 |  j |
  D] } | sJ t |  } | j } | j } n
 | } } i | j d 6| d 6| d 6t t | j   d 6| d 6| j	 |  d 6| r d n d d	 6} |	 d
 k	 r | j
 |	  n  |  j | | | | | j d | q Wd
 S)ze
        Generates a one-use only link for resetting password and sends to the
        user.
        rx   domain	site_nameuidrN   tokenhttpshttpprotocolNrr   )rD   ry   r   r#   rz   rx   r   r   pkZ
make_tokenupdateru   )r"   Zdomain_overridern   ro   Z	use_httpsZtoken_generatorrq   r^   rr   Zextra_email_contextrx   rN   Zcurrent_siter{   rz   rp   r   r   r   rL      s(    
	

zPasswordResetForm.save)r(   r)   r*   r   Z
EmailFieldrP   rx   ru   ry   r
   rL   r   r   r   r   rj      s   	rj   c                   s   e  Z d  Z d Z i e d  d 6Z e j d e d  d e j d e	 j
    Z e j d e d  d e j  Z   f d	 d
   Z d d   Z d d d  Z   S)SetPasswordFormza
    A form that lets a user change set their password without entering the old
    password
    z%The two password fields didn't match.r9   r:   zNew passwordr7   r<   zNew password confirmationc                s&   | |  _  t t |   j | |   d  S)N)rN   r.   r   r/   )r"   rN   r0   r1   )r2   r   r   r/     s    	zSetPasswordForm.__init__c             C   su   |  j  j d  } |  j  j d  } | r^ | r^ | | k r^ t j |  j d d d  q^ n  t j | |  j  | S)Nnew_password1new_password2r9   rC   )rD   rE   r   rF   rG   r   rI   rN   )r"   rA   rB   r   r   r   clean_new_password2  s    z#SetPasswordForm.clean_new_password2Tc             C   s:   |  j  d } |  j j |  | r3 |  j j   n  |  j S)Nr   )rD   rN   rM   rL   )r"   rK   rW   r   r   r   rL   $  s
    zSetPasswordForm.save)r(   r)   r*   rO   rP   rG   r   rQ   rR   r   "password_validators_help_text_htmlr   r   r/   r   rL   r   r   )r2   r   r     s   	r   c               @   sq   e  Z d  Z d Z e e j i e d  d 6 Z e j	 d e d  d e j
  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.
    zAYour old password was entered incorrectly. Please enter it again.password_incorrectr:   zOld passwordr7   old_passwordr   r   c             C   sB   |  j  d } |  j j |  s> t j |  j d d d  n  | S)zC
        Validates that the old_password field is correct.
        r   r   rC   )rD   rN   Zcheck_passwordr   rF   rG   )r"   r   r   r   r   clean_old_password:  s    z%PasswordChangeForm.clean_old_passwordN)r(   r)   r*   rO   dictr   rG   rP   r   rQ   rR   r   Zfield_orderr   r   r   r   r   r   ,  s   r   c                   s   e  Z d  Z d Z i e d  d 6Z d Z e j d e d  d e j	 d e
 j    Z e j d e d	  d e j	 d e d
   Z   f d d   Z d d   Z d d d  Z   f d d   Z e e  Z   S)AdminPasswordChangeFormzN
    A form used to change the password of a user in the admin interface.
    z%The two password fields didn't match.r9   r,   r:   r;   r7   r<   zPassword (again)z4Enter the same password as before, for verification.c                s&   | |  _  t t |   j | |   d  S)N)rN   r.   r   r/   )r"   rN   r0   r1   )r2   r   r   r/   Z  s    	z AdminPasswordChangeForm.__init__c             C   su   |  j  j d  } |  j  j d  } | r^ | r^ | | k r^ t j |  j d d d  q^ n  t j | |  j  | S)NrA   rB   r9   rC   )rD   rE   r   rF   rG   r   rI   rN   )r"   rA   rB   r   r   r   rJ   ^  s    z'AdminPasswordChangeForm.clean_password2Tc             C   s:   |  j  d } |  j j |  | r3 |  j j   n  |  j S)z)
        Saves the new password.
        rA   )rD   rN   rM   rL   )r"   rK   rW   r   r   r   rL   j  s
    zAdminPasswordChangeForm.savec                sC   t  t |   j } x' |  j j   D] } | | k r" g  Sq" Wd g S)NrW   )r.   r   changed_datar?   keys)r"   r3   r#   )r2   r   r   _get_changed_datat  s
    z)AdminPasswordChangeForm._get_changed_data)r(   r)   r*   rO   rP   rG   Zrequired_css_classr   rQ   rR   r   r   rA   rB   r/   rJ   rL   r   propertyr   r   r   )r2   r   r   G  s"   				
r   N)3
__future__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.forms.utilsr   Zdjango.templater   Zdjango.utils.encodingr   Zdjango.utils.htmlr   r   Zdjango.utils.httpr   Zdjango.utils.safestringr   Zdjango.utils.textr   Zdjango.utils.translationr   r   rP   ZWidgetr   ZFieldr+   Z	ModelFormr8   rS   ZFormrY   rj   r   r   r   r   r   r   r   <module>   s0   &IA&