
^Q\n:                 @   s  d  d l  m Z 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  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*  Z, Gd d   d e j*  Z- Gd d    d  e-  Z. Gd! d"   d" e j*  Z/ d S)#    )unicode_literalsN)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)ugettextugettext_lazyc                   s(   e  Z d  Z d Z   f d d   Z   S)ReadOnlyPasswordHashWidgetz)auth/widgets/read_only_password_hash.htmlc       	         s   t  t |   j | | |  } g  } | s: | j t  rW | j i t d  d 6 n y t |  } Wn, t k
 r | j i t d  d 6 YnH XxD | j	 |  j
   D]- \ } } | j i t |  d 6| d 6 q W| | d <| S)NzNo password set.labelz5Invalid password format or unknown hashing algorithm.valuesummary)superr   get_context
startswithr   appendr   r   
ValueErrorZsafe_summaryitems)	selfnamer   attrscontextr   ZhasherkeyZvalue_)	__class__ ?/var/www/dbchiro/venv/build/Django/django/contrib/auth/forms.pyr      s    "%
z&ReadOnlyPasswordHashWidget.get_context)__name__
__module____qualname__Ztemplate_namer   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)
setdefaultr   r(   __init__)r   argskwargs)r"   r#   r$   r+   0   s    z"ReadOnlyPasswordHashField.__init__c             C   s   | S)Nr#   )r   datainitialr#   r#   r$   
bound_data4   s    z$ReadOnlyPasswordHashField.bound_datac             C   s   d S)NFr#   )r   r/   r.   r#   r#   r$   has_changed9   s    z%ReadOnlyPasswordHashField.has_changed)r%   r&   r'   r   widgetr+   r0   r1   r#   r#   )r"   r$   r(   -   s   r(   c                   s"   e  Z d  Z   f d d   Z   S)UsernameFieldc                s"   t  j d t t |   j |   S)NNFKC)unicodedata	normalizer   r3   	to_python)r   r   )r"   r#   r$   r7   >   s    zUsernameField.to_python)r%   r&   r'   r7   r#   r#   )r"   r$   r3   =   s   r3   c            
       s   e  Z d  Z d Z i e d  d 6Z e j d e d  d d d e j d	 e	 j
    Z e j d e d
  d e j d d d	 e d   Z Gd d   d  Z   f 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_mismatchr   PasswordstripFr2   	help_textzPassword confirmationz4Enter the same password as before, for verification.c               @   s)   e  Z d  Z e Z d Z i e d 6Z d S)zUserCreationForm.MetausernameN)zusername)r%   r&   r'   r	   modelfieldsr3   field_classesr#   r#   r#   r$   MetaW   s   rA   c                sb   t  t |   j | |   |  j j j |  j k r^ |  j |  j j j j j j	 i d d 6 n  d  S)NT	autofocus)
r   r8   r+   _metar>   USERNAME_FIELDr?   r2   r   update)r   r,   r-   )r"   r#   r$   r+   \   s    zUserCreationForm.__init__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   rF   rG   r#   r#   r$   clean_password2a   s    z UserCreationForm.clean_password2Tc                sF   t  t |   j d d  } | j |  j d  | rB | j   n  | S)NcommitFrF   )r   r8   saveset_passwordrI   )r   rP   user)r"   r#   r$   rQ   m   s
    zUserCreationForm.save)r%   r&   r'   __doc___rL   r   	CharFieldPasswordInputr   "password_validators_help_text_htmlrF   rG   rA   r+   rO   rQ   r#   r#   )r"   r$   r8   B   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 i e d 6Z d S)zUserChangeForm.Meta__all__r=   N)r%   r&   r'   r	   r>   r?   r3   r@   r#   r#   r#   r$   rA      s   rA   c                sS   t  t |   j | |   |  j j d  } | d  k	 rO | j j d  | _ n  d  S)NZuser_permissionscontent_type)r   rY   r+   r?   rJ   ZquerysetZselect_related)r   r,   r-   f)r"   r#   r$   r+      s    zUserChangeForm.__init__c             C   s   |  j  d S)Npassword)r/   )r   r#   r#   r$   clean_password   s    zUserChangeForm.clean_password)	r%   r&   r'   r(   rU   r]   rA   r+   r^   r#   r#   )r"   r$   rY   u   s   rY   c            	       s   e  Z d  Z d Z e d d d e j d i d d 6  Z e j d e	 d	  d
 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   r2   r   TrB   r   r:   r;   Fz^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 t j  |  _	 |  j
 d j d k rx 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   r_   r+   	UserModelrC   	get_fieldrD   username_fieldr?   r   r   verbose_name)r   rd   r,   r-   )r"   r#   r$   r+      s    		zAuthenticationForm.__init__c             C   s   |  j  j d  } |  j  j d  } | d  k	 r | r t |  j 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=   r]   rb   rH   params)rI   rJ   r   rd   re   r   rK   rL   rh   ri   confirm_login_allowed)r   r=   r]   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.
        rc   rH   N)	is_activer   rK   rL   )r   rS   r#   r#   r$   rk      s    	z(AuthenticationForm.confirm_login_allowedc             C   s   |  j  r |  j  j Sd  S)N)re   id)r   r#   r#   r$   get_user_id   s    	
zAuthenticationForm.get_user_idc             C   s   |  j  S)N)re   )r   r#   r#   r$   get_user   s    zAuthenticationForm.get_user)r%   r&   r'   rT   r3   r   Z	TextInputr=   rV   rU   rW   r]   rL   r+   rl   rk   ro   rp   r#   r#   )r"   r$   r_      s"   	
r_   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Emailr`   ra   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`.
         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 i | d t  j   6d d 6  } 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__iexactTrm   c             s   s!   |  ] } | j    r | Vq d  S)N)Zhas_usable_password).0ur#   r#   r$   	<genexpr>   s    z.PasswordResetForm.get_users.<locals>.<genexpr>)rf   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 |  j |
  D] } | sJ t |  } | j } | j } n
 | } } i |
 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
 | | | | |
 d | q Wd
 S)ze
        Generates a one-use only link for resetting password and sends to the
        user.
        r   domain	site_nameuidrS   tokenhttpshttpprotocolNry   )rI   r   r   r   r   r   r   pkZ
make_tokenrE   r|   )r   Zdomain_overriderv   rw   Z	use_httpsZtoken_generatorrx   rd   ry   Zextra_email_contextr   rS   Zcurrent_siter   r   r    r#   r#   r$   rQ     s(    
	
zPasswordResetForm.save)r%   r&   r'   r   Z
EmailFieldrU   r   r|   r   r
   rQ   r#   r#   r#   r$   rq      s   	rq   c            	       s   e  Z d  Z d Z i e d  d 6Z e j d e d  d e j d d d	 e	 j
    Z e j d e d
  d 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 passwordr2   r;   Fr<   zNew password confirmationc                s&   | |  _  t t |   j | |   d  S)N)rS   r   r   r+   )r   rS   r,   r-   )r"   r#   r$   r+   9  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   rH   )rI   rJ   r   rK   rL   r   rN   rS   )r   rF   rG   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   )rI   rS   rR   rQ   )r   rP   r]   r#   r#   r$   rQ   I  s
    zSetPasswordForm.save)r%   r&   r'   rT   rU   rL   r   rV   rW   r   rX   r   r   r+   r   rQ   r#   r#   )r"   r$   r   %  s   			r   c               @   s   e  Z d  Z d Z e e j i e d  d 6 Z e j	 d e d  d d d e j
 d	 i d
 d 6  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 passwordr;   Fr2   r   TrB   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   rH   )rI   rS   Zcheck_passwordr   rK   rL   )r   r   r#   r#   r$   clean_old_passworda  s    z%PasswordChangeForm.clean_old_passwordN)r%   r&   r'   rT   dictr   rL   rU   r   rV   rW   r   Zfield_orderr   r#   r#   r#   r$   r   Q  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 i d	 d
 6 d d d e
 j    Z e j d e d  d e j	 d d d e d   Z   f d d   Z d d   Z d	 d d  Z e   f d d    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:   r2   r   TrB   r;   Fr<   zPassword (again)z4Enter the same password as before, for verification.c                s&   | |  _  t t |   j | |   d  S)N)rS   r   r   r+   )r   rS   r,   r-   )r"   r#   r$   r+     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)NrF   rG   r9   rH   )rI   rJ   r   rK   rL   r   rN   rS   )r   rF   rG   r#   r#   r$   rO     s    z'AdminPasswordChangeForm.clean_password2c             C   s:   |  j  d } |  j j |  | r3 |  j j   n  |  j S)z)
        Saves the new password.
        rF   )rI   rS   rR   rQ   )r   rP   r]   r#   r#   r$   rQ     s
    zAdminPasswordChangeForm.savec                sC   t  t |   j } x' |  j j   D] } | | k r" g  Sq" Wd g S)Nr]   )r   r   changed_datar?   keys)r   r.   r   )r"   r#   r$   r     s
    z$AdminPasswordChangeForm.changed_data)r%   r&   r'   rT   rU   rL   Zrequired_css_classr   rV   rW   r   rX   rF   rG   r+   rO   rQ   propertyr   r#   r#   )r"   r$   r   n  s$   			
r   )0
__future__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   rU   rf   ZWidgetr   ZFieldr(   rV   r3   Z	ModelFormr8   rY   ZFormr_   rq   r   r   r   r#   r#   r#   r$   <module>   s0   	3PD,