o
    Ti#C                     @   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   Y/var/www/html/evchargy.com/venv/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   
ValueErrorZsafe_summaryitems)	selfnamer   attrscontextr   ZhasherkeyZvalue_	__class__r   r   r!   %   s$   	z&ReadOnlyPasswordHashWidget.get_contextc                 C   s   d S Nr   )r&   Zid_r   r   r   id_for_label;   s   z'ReadOnlyPasswordHashWidget.id_for_label)__name__
__module____qualname__Ztemplate_nameZ	read_onlyr!   r.   __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   r7   B   s   z"ReadOnlyPasswordHashField.__init__)r/   r0   r1   r   widgetr7   r2   r   r   r+   r   r3   ?   s    r3   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   r=   I   s   zUsernameField.to_pythonc                    s   i t  |dddS )Nnoneusername)Zautocapitalizeautocomplete)r    widget_attrs)r&   r;   r+   r   r   rA   L   s   
zUsernameField.widget_attrs)r/   r0   r1   r=   rA   r2   r   r   r+   r   r<   H   s    r<   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.PasswordFr@   new-passwordr(   )r   stripr;   	help_textzPassword confirmation4Enter the same password as before, for verification.r   r;   rH   rI   c                   @      e Zd ZeZdZdeiZdS )zBaseUserCreationForm.Meta)r?   r?   Nr/   r0   r1   r   modelfieldsr<   Zfield_classesr   r   r   r   Metaj       rP   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    r7   _metarN   USERNAME_FIELDrO   r;   r(   r8   r+   r   r   r7   o   s   zBaseUserCreationForm.__init__c                 C   s>   | j d}| j d}|r|r||krt| jd dd|S N	password1	password2rC   code)cleaned_datagetr   error_messagesr&   rV   rW   r   r   r   clean_password2v   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 )NrW   )	r    _post_cleanrZ   r[   r   validate_passwordinstancer   Z	add_error)r&   passworderrorr+   r   r   r_      s   
z BaseUserCreationForm._post_cleanTc                    s@   t  jdd}|| jd  |r|  t| dr|   |S )NF)commitrV   save_m2m)r    saveset_passwordrZ   hasattrre   )r&   rd   userr+   r   r   rf      s   
zBaseUserCreationForm.saveT)r/   r0   r1   __doc___r\   r   	CharFieldPasswordInputr   "password_validators_help_text_htmlrV   rW   rP   r7   r^   r_   rf   r2   r   r   r+   r   rB   T   s*    
rB   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.r?   )Zusername__iexactN)rZ   r[   rS   rN   objectsfilterexistsZ_update_errorsr   ra   Zunique_error_message)r&   r?   r   r   r   clean_username   s   

zUserCreationForm.clean_usernameN)r/   r0   r1   rt   r   r   r   r   rp      s    rp   c                       s>   e Zd ZeededdZG dd dZ fddZ  ZS )UserChangeFormrE   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   rI   c                   @   rL   )zUserChangeForm.Meta__all__r?   NrM   r   r   r   r   rP      rQ   rP   c                    sb   t  j|i | | jd}|r|jd| jj d|_| jd}|r/|j	d|_d S d S )Nrb   z../../z
/password/user_permissionscontent_type)
r    r7   rO   r[   rI   formatra   pkZquerysetZselect_related)r&   r9   r:   rb   rw   r+   r   r   r7      s   
zUserChangeForm.__init__)	r/   r0   r1   r3   rl   rb   rP   r7   r2   r   r   r+   r   ru      s    	ru   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.
    rR   TrG   )r;   rE   Fr@   current-passwordr   rH   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                    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   r?   Z	maxlength)request
user_cacher    r7   	UserModelrS   	get_fieldrT   username_field
max_lengthrO   r;   r(   r   r   verbose_name)r&   r   r9   r:   Zusername_max_lengthr+   r   r   r7      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 )Nr?   rb   )r?   rb   )rZ   r[   r   r   r   get_invalid_login_errorconfirm_login_allowed)r&   r?   rb   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   rX   N)	is_activer   r\   )r&   ri   r   r   r   r      s   z(AuthenticationForm.confirm_login_allowedc                 C   s   | j S r-   )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~   r?   )rY   params)r   r\   r   r   r   r   r   r   r     s
   
z*AuthenticationForm.get_invalid_login_errorr-   )r/   r0   r1   rk   r<   r   Z	TextInputr?   rm   rl   rn   rb   r\   r7   r   r   r   r   r2   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 )PasswordResetFormZEmailr   r@   emailrG   )r   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   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                    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 r-   )Zhas_usable_passwordr   getattr).0ur   email_field_namer   r   	<genexpr>E  s    
z.PasswordResetForm.get_users.<locals>.<genexpr>Nr   )r   get_email_field_nameZ_default_managerrr   )r&   r   Zactive_usersr   r   r   	get_users7  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uidri   tokenprotocol)r   N)rZ   r
   r'   r   r   r   r   r   r   r   rz   Z
make_tokenr   )r&   Zdomain_overrider   r   Z	use_httpsZtoken_generatorr   r   r   Zextra_email_contextr   Zcurrent_siter   r   r   ri   Z
user_emailr)   r   r   r   rf   L  s8   



zPasswordResetForm.saver-   )r/   r0   r1   r   Z
EmailFieldrl   Z
EmailInputr   r   r   r	   rf   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
    rC   rD   zNew passwordr@   rF   rG   FrK   zNew password confirmationr}   c                       || _ t j|i | d S r-   ri   r    r7   r&   ri   r9   r:   r+   r   r   r7        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_password2rC   rX   rZ   r[   r   r\   r   r`   ri   r]   r   r   r   clean_new_password2     z#SetPasswordForm.clean_new_password2Tc                 C   *   | j d }| j| |r| j  | jS )Nr   rZ   ri   rg   rf   r&   rd   rb   r   r   r   rf     s
   

zSetPasswordForm.saverj   )r/   r0   r1   rk   rl   r\   r   rm   rn   r   ro   r   r   r7   r   rf   r2   r   r   r+   r   r   z  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r@   rR   rG   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   rX   )rZ   ri   Zcheck_passwordr   r\   )r&   r   r   r   r   clean_old_password  s   
z%PasswordChangeForm.clean_old_passwordN)r/   r0   r1   rk   r   r\   rl   r   rm   rn   r   Z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.
    rC   rD   r4   rE   rF   Tr   rG   FrK   zPassword (again)r@   rJ   c                    r   r-   r   r   r+   r   r   r7     r   z AdminPasswordChangeForm.__init__c                 C   r   rU   r   r]   r   r   r   r^     r   z'AdminPasswordChangeForm.clean_password2c                 C   r   )zSave the new password.rV   r   r   r   r   r   rf     s
   

zAdminPasswordChangeForm.savec                    s*   t  j}| jD ]
}||vrg   S qdgS )Nrb   )r    changed_datarO   )r&   datar'   r+   r   r   r     s   
z$AdminPasswordChangeForm.changed_datarj   )r/   r0   r1   rk   rl   r\   Zrequired_css_classr   rm   rn   r   ro   rV   rW   r7   r^   rf   propertyr   r2   r   r   r+   r   r     s0    
r   )2r   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   rl   r   r   ZWidgetr   Fieldr3   rm   r<   Z	ModelFormrB   rp   ru   ZFormr{   r   r   r   r   r   r   r   r   <module>   s8    	AQb,#