o
    uvXjÑ:  ã                   @   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dZ‡ fdd„Z‡  ZS )ÚReadOnlyPasswordHashWidgetz)auth/widgets/read_only_password_hash.htmlTc           	         s    t ƒ  |||¡}g }|r| t¡r| dtdƒi¡ n/zt|ƒ}W n ty3   | dtdƒi¡ Y nw | |¡ 	¡ D ]\}}| t|ƒ|dœ¡ q;||d< |S )NÚlabelzNo password set.z5Invalid password format or unknown hashing algorithm.)r   ÚvalueÚsummary)
ÚsuperÚget_contextÚ
startswithr   Úappendr   r   Ú
ValueErrorÚsafe_summaryÚitems)	ÚselfÚnamer   ÚattrsÚcontextr   ÚhasherÚkeyÚvalue_©Ú	__class__© úV/var/www/html/myproject/venv/lib/python3.10/site-packages/django/contrib/auth/forms.pyr      s   ÿz&ReadOnlyPasswordHashWidget.get_context)Ú__name__Ú
__module__Ú__qualname__Útemplate_nameÚ	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"   |  dd¡ tƒ j|i |¤Ž d S )NÚrequiredF)Ú
setdefaultr   Ú__init__©r   ÚargsÚkwargsr$   r&   r'   r1   /   s   z"ReadOnlyPasswordHashField.__init__c                 C   s   |S ©Nr&   )r   ÚdataÚinitialr&   r&   r'   Ú
bound_data3   s   z$ReadOnlyPasswordHashField.bound_datac                 C   s   dS )NFr&   )r   r7   r6   r&   r&   r'   Úhas_changed8   s   z%ReadOnlyPasswordHashField.has_changed)	r(   r)   r*   r   Úwidgetr1   r8   r9   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'   r?   =   s   zUsernameField.to_python)r(   r)   r*   r?   r-   r&   r&   r$   r'   r;   <   s    r;   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_mismatchú%The two password fields didn't match.ÚPasswordF)r   Ústripr:   Ú	help_textzPassword confirmationú4Enter the same password as before, for verification.©r   r:   rD   rE   c                   @   ó   e Zd ZeZdZdeiZdS )zUserCreationForm.Meta)ÚusernamerI   N©r(   r)   r*   r   ÚmodelÚfieldsr;   Úfield_classesr&   r&   r&   r'   ÚMetaV   ó    rN   c                    sH   t ƒ j|i |¤Ž | jjj| jv r"| j| jjj jj ddi¡ d S d S )NÚ	autofocusT)	r   r1   Ú_metarK   ÚUSERNAME_FIELDrL   r:   r   Úupdater2   r$   r&   r'   r1   [   s   "ÿzUserCreationForm.__init__c                 C   s@   | j  d¡}| j  d¡}|r|r||krtj| jd dd�‚|S ©NÚ	password1Ú	password2rA   ©Úcode)Úcleaned_dataÚgetr   ÚValidationErrorÚerror_messages©r   rU   rV   r&   r&   r'   Úclean_password2`   s   þz UserCreationForm.clean_password2c              
      sh   t ƒ  ¡  | j d¡}|r2z
t || j¡ W d S  tjy1 } z|  	d|¡ W Y d }~d S d }~ww d S )NrV   )
r   Ú_post_cleanrY   rZ   r   Úvalidate_passwordÚinstancer   r[   Ú	add_error)r   ÚpasswordÚerrorr$   r&   r'   r_   j   s   
€ÿýzUserCreationForm._post_cleanTc                    s.   t ƒ jdd�}| | jd ¡ |r| ¡  |S )NF)ÚcommitrU   )r   ÚsaveÚset_passwordrY   )r   re   Úuserr$   r&   r'   rf   u   s
   zUserCreationForm.save©T)r(   r)   r*   Ú__doc__Ú_r\   r   Ú	CharFieldÚPasswordInputr   Ú"password_validators_help_text_htmlrU   rV   rN   r1   r^   r_   rf   r-   r&   r&   r$   r'   r@   A   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 )
ÚUserChangeFormrC   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   rE   c                   @   rH   )zUserChangeForm.MetaÚ__all__rI   NrJ   r&   r&   r&   r'   rN   ‡   rO   rN   c                    sV   t ƒ j|i |¤Ž | j d¡}|r|j d¡|_| j d¡}|r)|j d¡|_d S d S )Nrc   z../password/Úuser_permissionsÚcontent_type)r   r1   rL   rZ   rE   ÚformatÚquerysetÚselect_related)r   r3   r4   rc   rq   r$   r&   r'   r1   Œ   s   ÿzUserChangeForm.__init__c                 C   s   | j  d¡S ©Nrc   )r7   rZ   ©r   r&   r&   r'   Úclean_password•   s   zUserChangeForm.clean_password)
r(   r)   r*   r.   rk   rc   rN   r1   rx   r-   r&   r&   r$   r'   ro   }   s    ÿþ		ro   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�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.
    rP   T©r   )r:   rC   F©r   rD   r:   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                    sn   || _ d| _tƒ j|i |¤Ž tj tj¡| _| jj	pd| j
d _	| j
d jdu r5t| jjƒ| j
d _dS 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éþ   rI   )ÚrequestÚ
user_cacher   r1   Ú	UserModelrQ   Ú	get_fieldrR   Úusername_fieldÚ
max_lengthrL   r   r   Úverbose_name)r   r   r3   r4   r$   r&   r'   r1   °   s   ÿzAuthenticationForm.__init__c                 C   sZ   | j  d¡}| j  d¡}|d ur*|r*t| j||d�| _| jd u r$|  ¡ ‚|  | j¡ | j S )NrI   rc   )rI   rc   )rY   rZ   r   r   r€   Úget_invalid_login_errorÚconfirm_login_allowed)r   rI   rc   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.
        r}   rW   N)Ú	is_activer   r[   r\   )r   rh   r&   r&   r'   r‡   Ì   s   þÿz(AuthenticationForm.confirm_login_allowedc                 C   s   | j S r5   )r€   rw   r&   r&   r'   Úget_userÝ   s   zAuthenticationForm.get_userc                 C   s   t j| jd dd| jjid�S )Nr|   rI   )rX   Úparams)r   r[   r\   rƒ   r…   rw   r&   r&   r'   r†   à   s
   
ýz*AuthenticationForm.get_invalid_login_errorr5   )r(   r)   r*   rj   r;   r   Ú	TextInputrI   rl   rk   rm   rc   r\   r1   rˆ   r‡   rŠ   r†   r-   r&   r&   r$   r'   ry   œ   s$    ýÿûry   c                	   @   sN   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 )ÚPasswordResetFormÚEmailr~   )r   r„   Nc                 C   sb   t  ||¡}d | ¡ ¡}t  ||¡}t||||gƒ}	|dur+t  ||¡}
|	 |
d¡ |	 ¡  dS )zO
        Send a django.core.mail.EmailMultiAlternatives to `to_email`.
        Ú Nz	text/html)r   Úrender_to_stringÚjoinÚ
splitlinesr   Úattach_alternativeÚsend)r   Úsubject_template_nameÚemail_template_namer    Ú
from_emailÚto_emailÚhtml_email_template_nameÚsubjectÚbodyÚemail_messageÚ
html_emailr&   r&   r'   Ú	send_mailë   s   zPasswordResetForm.send_mailc                 C   s0   t jjdi 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__iexactr‰   Tc                 s   s   � | ]	}|  ¡ r|V  qd S r5   )Úhas_usable_password)Ú.0Úur&   r&   r'   Ú	<genexpr>  s   € z.PasswordResetForm.get_users.<locals>.<genexpr>Nr&   )r�   Ú_default_managerÚfilterÚget_email_field_name)r   ÚemailÚ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 }
|  |
¡D ]8}|st|ƒ}|j}|j}n| }}|
||tt|jƒƒ|| |¡|r/dnddœ|	p5i ¥}| j	|||||
|d� q
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Úuidrh   ÚtokenÚprotocol)r™   N)
rY   r¨   r
   r   r«   r   r   ÚpkÚ
make_tokenrž   )r   Údomain_overrider•   r–   Ú	use_httpsÚtoken_generatorr—   r   r™   Úextra_email_contextr¦   rh   Úcurrent_siter¬   r«   r    r&   r&   r'   rf   	  s,   


ùø
þïzPasswordResetForm.saver5   )r(   r)   r*   r   Ú
EmailFieldrk   r¦   rž   r¨   r	   rf   r&   r&   r&   r'   r�   è   s    
ÿûr�   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
    rA   rB   zNew passwordFrG   zNew password confirmationr{   c                    ó   || _ tƒ j|i |¤Ž d S r5   ©rh   r   r1   ©r   rh   r3   r4   r$   r&   r'   r1   ?  ó   zSetPasswordForm.__init__c                 C   óN   | j  d¡}| j  d¡}|r|r||krtj| jd dd�‚t || j¡ |S )NÚnew_password1Únew_password2rA   rW   ©rY   rZ   r   r[   r\   r   r`   rh   r]   r&   r&   r'   Úclean_new_password2C  ó   þz#SetPasswordForm.clean_new_password2Tc                 C   ó*   | j d }| j |¡ |r| j ¡  | jS )Nr¾   ©rY   rh   rg   rf   ©r   re   rc   r&   r&   r'   rf   O  s
   

zSetPasswordForm.saveri   )r(   r)   r*   rj   rk   r\   r   rl   rm   r   rn   r¾   r¿   r1   rÁ   rf   r-   r&   r&   r$   r'   r¸   +  s$    ÿüýr¸   c                   @   sV   e Zd ZdZi ej¥dedƒi¥Zejedƒdej	ddid�d	�Z
g d
¢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 passwordFrP   Trz   r{   )Ú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Ç   rW   )rY   rh   Úcheck_passwordr   r[   r\   )r   rÈ   r&   r&   r'   Úclean_old_passwordh  s   
þz%PasswordChangeForm.clean_old_passwordN)r(   r)   r*   rj   r¸   r\   rk   r   rl   rm   rÈ   Úfield_orderrÊ   r&   r&   r&   r'   rÆ   W  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.
    rA   rB   r/   rC   rP   Trz   FrG   zPassword (again)rF   c                    r¹   r5   rº   r»   r$   r&   r'   r1   Š  r¼   z AdminPasswordChangeForm.__init__c                 C   r½   rT   rÀ   r]   r&   r&   r'   r^   Ž  rÂ   z'AdminPasswordChangeForm.clean_password2c                 C   rÃ   )zSave the new password.rU   rÄ   rÅ   r&   r&   r'   rf   š  s
   

zAdminPasswordChangeForm.savec                    s*   t ƒ j}| jD ]
}||vrg   S qdgS rv   )r   Úchanged_datarL   )r   r6   r   r$   r&   r'   rÍ   ¢  s   
ÿz$AdminPasswordChangeForm.changed_datari   )r(   r)   r*   rj   rk   r\   Úrequired_css_classr   rl   rm   r   rn   rU   rV   r1   r^   rf   ÚpropertyrÍ   r-   r&   r&   r$   r'   rÌ   u  s,    ÿüü
rÌ   ).r=   Údjangor   Údjango.contrib.authr   r   r   Údjango.contrib.auth.hashersr   r   Údjango.contrib.auth.modelsr   Údjango.contrib.auth.tokensr	   Údjango.contrib.sites.shortcutsr
   Údjango.core.mailr   Údjango.templater   Údjango.utils.encodingr   Údjango.utils.httpr   Údjango.utils.textr   Údjango.utils.translationr   r   rk   r�   ÚWidgetr   ÚFieldr.   rl   r;   Ú	ModelFormr@   ro   ÚFormry   r�   r¸   rÆ   rÌ   r&   r&   r&   r'   Ú<module>   s0    <LC,