o
    ÔèFhÔD  ã                   @   s�  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 d dl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)ƒ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/ƒZ0G d&d'„ d'ej,ƒZ1d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   úR/var/www/html/loop/nvenv/lib/python3.10/site-packages/django/contrib/auth/forms.pyÚ_unicode_ci_compare   s   ÿÿr   c                       s,   e Zd ZdZdZ‡ fdd„Z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__r   r   r!   %   s$   ÿÿÿÿ	z&ReadOnlyPasswordHashWidget.get_contextc                 C   s   d S ©Nr   )r'   Úid_r   r   r   Úid_for_label;   s   z'ReadOnlyPasswordHashWidget.id_for_label)Ú__name__Ú
__module__Ú__qualname__Útemplate_nameÚ	read_onlyr!   r2   Ú__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|i |¤Ž d S )NÚrequiredFÚdisabledT)Ú
setdefaultr    Ú__init__©r'   ÚargsÚkwargsr.   r   r   r=   B   s   z"ReadOnlyPasswordHashField.__init__)r3   r4   r5   r   Úwidgetr=   r8   r   r   r.   r   r9   ?   s    r9   c                       s(   e Zd Z‡ fdd„Z‡ fdd„Z‡  ZS )ÚUsernameFieldc                    s4   t ƒ  |¡}| jd urt|ƒ| jkr|S t d|¡S )Nr   )r    Ú	to_pythonÚ
max_lengthÚlenr   r   )r'   r   r.   r   r   rC   I   s   zUsernameField.to_pythonc                    s   i t ƒ  |¡¥dddœ¥S )NÚnoneÚusername)ÚautocapitalizeÚautocomplete)r    Úwidget_attrs)r'   rA   r.   r   r   rJ   T   s   
ÿýzUsernameField.widget_attrs)r3   r4   r5   rC   rJ   r8   r   r   r.   r   rB   H   s    rB   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 )ÚBaseUserCreationFormzc
    A form that creates a user, with no privileges, from the given username and
    password.
    Úpassword_mismatchõ'   The two password fields didnâ€™t match.ÚPasswordFrI   únew-password©r)   )r   ÚstriprA   Ú	help_textzPassword confirmationú4Enter the same password as before, for verification.©r   rA   rQ   rR   c                   @   ó   e Zd ZeZdZdeiZdS )zBaseUserCreationForm.Meta)rG   rG   N©r3   r4   r5   r   ÚmodelÚfieldsrB   Úfield_classesr   r   r   r   ÚMetar   ó    rZ   c                    sB   t ƒ j|i |¤Ž | jjj| jv rd| j| jjj jjd< d S d S )NTÚ	autofocus)r    r=   Ú_metarW   ÚUSERNAME_FIELDrX   rA   r)   r>   r.   r   r   r=   w   s   þÿÿzBaseUserCreationForm.__init__c                 C   s>   | j  d¡}| j  d¡}|r|r||krt| jd dd�‚|S ©NÚ	password1Ú	password2rL   ©Úcode)Úcleaned_dataÚgetr   Úerror_messages©r'   r`   ra   r   r   r   Úclean_password2~   s   þz$BaseUserCreationForm.clean_password2c              
      sf   t ƒ  ¡  | j d¡}|r1z
t || j¡ W d S  ty0 } z|  d|¡ W Y d }~d S d }~ww d S )Nra   )	r    Ú_post_cleanrd   re   r   Úvalidate_passwordÚinstancer   Ú	add_error)r'   ÚpasswordÚerrorr.   r   r   ri   ˆ   s   
€ÿýz BaseUserCreationForm._post_cleanTc                    s@   t ƒ jdd�}| | jd ¡ |r| ¡  t| dƒr|  ¡  |S )NF)Úcommitr`   Úsave_m2m)r    ÚsaveÚset_passwordrd   Úhasattrrp   )r'   ro   Úuserr.   r   r   rq   “   s   
zBaseUserCreationForm.save©T)r3   r4   r5   Ú__doc__Ú_rf   r   Ú	CharFieldÚPasswordInputr   Ú"password_validators_help_text_htmlr`   ra   rZ   r=   rh   ri   rq   r8   r   r   r.   r   rK   \   s*    ÿüü
rK   c                   @   s   e Zd Zdd„ ZdS )ÚUserCreationFormc                 C   sP   | j  d¡}|r&| jjjj|d� ¡ r&|  td| j	 
| jjdg¡iƒ¡ dS |S )z*Reject usernames that differ only in case.rG   )Úusername__iexactN)rd   re   r]   rW   ÚobjectsÚfilterÚexistsÚ_update_errorsr   rk   Úunique_error_message)r'   rG   r   r   r   Úclean_usernamež   s   ÿþ
ÿÿÿÿ
zUserCreationForm.clean_usernameN)r3   r4   r5   r‚   r   r   r   r   r{   �   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 )ÚUserChangeFormrN   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   rR   c                   @   rU   )zUserChangeForm.MetaÚ__all__rG   NrV   r   r   r   r   rZ   ¼   r[   rZ   c                    sb   t ƒ j|i |¤Ž | j d¡}|r|j d| jj› d�¡|_| j d¡}|r/|j 	d¡|_d S d S )Nrm   z../../z
/password/Úuser_permissionsÚcontent_type)
r    r=   rX   re   rR   Úformatrk   ÚpkÚquerysetÚselect_related)r'   r?   r@   rm   r…   r.   r   r   r=   Á   s   ÿ
ÿÿzUserChangeForm.__init__)	r3   r4   r5   r9   rw   rm   rZ   r=   r8   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.
    r\   TrP   )rA   rN   FrI   úcurrent-password©r   rQ   rA   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|i |¤Ž tj tj¡| _| jj	pd}|| j
d _	|| j
d jjd< | j
d jdu r@t| 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éþ   rG   Ú	maxlength)ÚrequestÚ
user_cacher    r=   Ú	UserModelr]   Ú	get_fieldr^   Úusername_fieldrD   rX   rA   r)   r   r   Úverbose_name)r'   r’   r?   r@   Úusername_max_lengthr.   r   r   r=   ä   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 )NrG   rm   )rG   rm   )rd   re   r   r’   r“   Úget_invalid_login_errorÚconfirm_login_allowed)r'   rG   rm   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.
        r�   rb   N)Ú	is_activer   rf   )r'   rt   r   r   r   rš     s   þÿz(AuthenticationForm.confirm_login_allowedc                 C   s   | j S r0   )r“   ©r'   r   r   r   Úget_user  s   zAuthenticationForm.get_userc                 C   s   t | jd dd| jjid�S )NrŽ   rG   )rc   Úparams)r   rf   r–   r—   r�   r   r   r   r™     s
   
ýz*AuthenticationForm.get_invalid_login_errorr0   )r3   r4   r5   rv   rB   r   Ú	TextInputrG   rx   rw   ry   rm   rf   r=   r›   rš   rž   r™   r8   r   r   r.   r   r‹   Ï   s$    ýÿûr‹   c                	   @   s\   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 )ÚPasswordResetFormÚEmailr�   rI   ÚemailrP   )r   rD   rA   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                    s:   t  ¡ ‰t jjdi 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__iexactrœ   Tc                 3   s,   � | ]}|  ¡ rtˆ t|ˆƒƒr|V  qd S r0   )Úhas_usable_passwordr   Úgetattr)Ú.0Úu©r£   Úemail_field_namer   r   Ú	<genexpr>M  s   € ýü
ÿz.PasswordResetForm.get_users.<locals>.<genexpr>Nr   )r”   Úget_email_field_nameÚ_default_managerr~   )r'   r£   Ú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 }
|st|ƒ}|j}|j}n| }}t ¡ }|  |
¡D ],}t||ƒ}|||tt	|j
ƒƒ|| |¡|r8dnddœ|	p>i ¥}| 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Úuidrt   ÚtokenÚprotocol)r®   N)rd   r
   r(   rÁ   r”   r»   r¾   rµ   r   r   rˆ   Ú
make_tokenr³   )r'   Údomain_overriderª   r«   Ú	use_httpsÚtoken_generatorr¬   r’   r®   Úextra_email_contextr£   Úcurrent_siterÂ   rÁ   r¹   rt   Ú
user_emailr*   r   r   r   rq   T  s8   


ùø
úôzPasswordResetForm.saver0   )r3   r4   r5   r   Ú
EmailFieldrw   Ú
EmailInputr£   r³   r¾   r	   rq   r   r   r   r   r¡      s&    ý
ùör¡   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 )ÚSetPasswordFormzZ
    A form that lets a user set their password without entering the old
    password
    rL   rM   zNew passwordrI   rO   rP   FrT   zNew password confirmationr�   c                    ó   || _ tƒ j|i |¤Ž d S r0   ©rt   r    r=   ©r'   rt   r?   r@   r.   r   r   r=   —  ó   zSetPasswordForm.__init__c                 C   óL   | j  d¡}| j  d¡}|r|r||krt| jd dd�‚t || j¡ |S )NÚnew_password1Únew_password2rL   rb   ©rd   re   r   rf   r   rj   rt   rg   r   r   r   Úclean_new_password2›  ó   þz#SetPasswordForm.clean_new_password2Tc                 C   ó*   | j d }| j |¡ |r| j ¡  | jS )NrÕ   ©rd   rt   rr   rq   ©r'   ro   rm   r   r   r   rq   ¦  s
   

zSetPasswordForm.saveru   )r3   r4   r5   rv   rw   rf   r   rx   ry   r   rz   rÕ   rÖ   r=   rØ   rq   r8   r   r   r.   r   rÏ   ‚  s$    ÿüýrÏ   c                   @   sX   e Zd ZdZi ej¥dedƒi¥Zejedƒdej	dddœ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 passwordFrŒ   T©rI   r\   rP   r�   )Ú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Þ   rb   )rd   rt   Úcheck_passwordr   rf   )r'   rà   r   r   r   Úclean_old_passwordÄ  s   
þz%PasswordChangeForm.clean_old_passwordN)r3   r4   r5   rv   rÏ   rf   rw   r   rx   ry   rà   Úfield_orderrâ   r   r   r   r   rÝ   ®  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.
    rL   rM   r:   rN   rO   Trß   rP   FrT   zPassword (again)rI   rS   c                    rÐ   r0   rÑ   rÒ   r.   r   r   r=   é  rÓ   z AdminPasswordChangeForm.__init__c                 C   rÔ   r_   r×   rg   r   r   r   rh   í  rÙ   z'AdminPasswordChangeForm.clean_password2c                 C   rÚ   )zSave the new password.r`   rÛ   rÜ   r   r   r   rq   ø  s
   

zAdminPasswordChangeForm.savec                    s*   t ƒ j}| jD ]
}||vrg   S qdgS )Nrm   )r    Úchanged_datarX   )r'   Údatar(   r.   r   r   rå      s   
ÿz$AdminPasswordChangeForm.changed_dataru   )r3   r4   r5   rv   rw   rf   Úrequired_css_classr   rx   ry   r   rz   r`   ra   r=   rh   rq   Úpropertyrå   r8   r   r   r.   r   rä   Ñ  s0    ÿÿúü
rä   )2r   Ú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.exceptionsr   Údjango.core.mailr   Údjango.templater   Údjango.utils.encodingr   Údjango.utils.httpr   Údjango.utils.textr   Údjango.utils.translationr   r   rw   r”   r   ÚWidgetr   ÚFieldr9   rx   rB   Ú	ModelFormrK   r{   rƒ   ÚFormr‹   r¡   rÏ   rÝ   rä   r   r   r   r   Ú<module>   s8    	AQb,#