B
    bK                 @   s  d dl mZ d dl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 d dlm Z  d dl!m"Z" d dl#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l0m1Z1 e Z2G dd dej3Z4G dd dej5Z6G dd dej7Z8G dd dej9Z:G dd  d ej9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<ZAdS )+    )unicode_literalsN)forms)authenticateget_user_modelpassword_validation)UNUSABLE_PASSWORD_PREFIXidentify_hasher)SystemSetting)User)default_token_generator)get_current_site)EmailMultiAlternatives)ModelChoiceField)loader)timezone)force_bytes)urlsafe_base64_encode)capfirst)gettextgettext_lazy)ZKPasswordInputZKEmailInput)DateTimeField)delivery_passwordchange_email)MyUser)get_current_company_idc                   s    e Zd ZdZ fddZ  ZS )ReadOnlyPasswordHashWidgetz-accounts/widgets/read_only_password_hash.htmlc       	         s   t t| |||}g }|r&|tr:|dtdi ndyt|}W n& tk
rl   |dtdi Y n2X x.|	|
 D ]\}}|t||d q~W ||d< |S )NlabelzNo password set.z5Invalid password format or unknown hashing algorithm.)r   valuesummary)superr   get_context
startswithr   appendr   r   
ValueErrorZsafe_summaryitems)	selfnamer   attrscontextr   ZhasherkeyZvalue_)	__class__ ?G:\easytimepro\master/mysite/base\admin\forms\auth_user_form.pyr!   )   s    z&ReadOnlyPasswordHashWidget.get_context)__name__
__module____qualname__Ztemplate_namer!   __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t| j|| d S )NrequiredF)
setdefaultr    r2   __init__)r&   argskwargs)r+   r,   r-   r5   =   s    z"ReadOnlyPasswordHashField.__init__c             C   s   |S )Nr,   )r&   datainitialr,   r,   r-   
bound_dataA   s    z$ReadOnlyPasswordHashField.bound_datac             C   s   dS )NFr,   )r&   r9   r8   r,   r,   r-   has_changedF   s    z%ReadOnlyPasswordHashField.has_changed)	r.   r/   r0   r   widgetr5   r:   r;   r1   r,   r,   )r+   r-   r2   :   s   r2   c                   s   e Zd Z fddZ  ZS )UsernameFieldc                s   t dtt| |S )NZNFKC)unicodedataZ	normalizer    r=   	to_python)r&   r   )r+   r,   r-   r?   K   s    zUsernameField.to_python)r.   r/   r0   r?   r1   r,   r,   )r+   r-   r=   J   s   r=   c                   s   e Zd ZdZdediZejeddeddZ	ejedde
e dZejed	e
dd
ZG dd dZ fddZ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_mismatchZpassword_mismatch_errorMessageEmailF)r   stripr<   r3   Zuser_createForm_password)r   rC   r<   	help_textZ$user_createForm_passwordConfirmation)r   r<   rC   c               @   s   e Zd ZeZdZdS )zUserCreationForm.Meta)nicknameemailN)r.   r/   r0   r   modelfieldsr,   r,   r,   r-   Metal   s   rI   c                s8   t t| j|| d| jkr4| jd jjddi d S )NrE   	autofocusT)r    r@   r5   rH   r<   r(   update)r&   r6   r7   )r+   r,   r-   r5   q   s    
zUserCreationForm.__init__c             C   sv   | j d}| j d}|r<|r<||kr<tj| jd dd| j d| j_| j d| j_t	| j d| j |S )N	password1	password2rA   )coderE   )
cleaned_datagetr   ValidationErrorerror_messagesinstanceusernamerE   r   validate_password)r&   rL   rM   r,   r,   r-   clean_password2x   s    z UserCreationForm.clean_password2Tc                s2   t t| jdd}|| jd  |r.|  |S )NF)commitrL   )r    r@   saveset_passwordrO   )r&   rW   user)r+   r,   r-   rX      s
    zUserCreationForm.save)T)r.   r/   r0   __doc___rR   r   	CharFieldr   rF   r   r   "password_validators_help_text_htmlrL   rM   rI   r5   rV   rX   r1   r,   r,   )r+   r-   r@   O   s(   r@   c                   sr   e Zd ZeededdZeedejdddZ	eedejdddZ
G d	d
 d
Z fddZdd Z  ZS )UserChangeFormPasswordzRaw passwords are not stored, so there is no way to see this user's password, but you can change the password using toolbar above.)r   rD   Zuser_field_dateJoinedTF)r   r9   Zdisabledr3   Zuser_field_lastLoginc               @   s   e Zd ZeZdZdeiZdS )zUserChangeForm.Meta__all__rT   N)r.   r/   r0   r
   rG   rH   r=   Zfield_classesr,   r,   r,   r-   rI      s   rI   c                s8   t t| j|| | jd}|d k	r4|jd|_d S )NZuser_permissionsZcontent_type)r    r_   r5   rH   rP   ZquerysetZselect_related)r&   r6   r7   f)r+   r,   r-   r5      s    zUserChangeForm.__init__c             C   s
   | j d S )Npassword)r9   )r&   r,   r,   r-   clean_password   s    zUserChangeForm.clean_password)r.   r/   r0   r2   r\   rc   r   r   ZnowZdate_joinedZ
last_loginrI   r5   rd   r1   r,   r,   )r+   r-   r_      s   
r_   c                   s   e Zd ZdZed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.
       rJ   T)r(   )
max_lengthr<   r`   F)r   rC   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                sV   || _ d| _tt| j|| tjtj| _	| j
d jdkrRt| j	j| j
d _dS )z
        The 'request' parameter is set for custom auth use by subclasses.
        The form data comes in via the standard 'data' kwarg.
        NrT   )request
user_cacher    re   r5   	UserModelZ_metaZ	get_fieldZUSERNAME_FIELDusername_fieldrH   r   r   verbose_name)r&   rj   r6   r7   )r+   r,   r-   r5      s    zAuthenticationForm.__init__c             C   sr   | j d}| j d}|d k	rl|rlt| j||d| _| jd kr`tj| jd dd| jj	idn| 
| j | j S )NrT   rc   )rT   rc   rh   )rN   Zparams)rO   rP   r   rj   rk   r   rQ   rR   rm   rn   confirm_login_allowed)r&   rT   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.
        ri   )rN   N)	is_activer   rQ   rR   )r&   rZ   r,   r,   r-   ro      s    z(AuthenticationForm.confirm_login_allowedc             C   s   | j r| j jS d S )N)rk   id)r&   r,   r,   r-   get_user_id   s    zAuthenticationForm.get_user_idc             C   s   | j S )N)rk   )r&   r,   r,   r-   get_user   s    zAuthenticationForm.get_user)N)r.   r/   r0   r[   r=   r   Z	TextInputrT   r]   r\   PasswordInputrc   rR   r5   rp   ro   rs   rt   r1   r,   r,   )r+   r-   re      s    
re   c            	   @   sL   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 )PasswordResetFormrB   rf   )r   rg   Nc             C   sb   t ||}d| }t ||}t||||g}	|dk	rVt ||}
|	|
d |	  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_emailto_emailhtml_email_template_nameZsubjectZbodyZemail_messageZ
html_emailr,   r,   r-   	send_mail   s    zPasswordResetForm.send_mailc             C   s,   t jjf 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__iexactrq   Tc             s   s   | ]}|  r|V  qd S )N)Zhas_usable_password).0ur,   r,   r-   	<genexpr>  s    z.PasswordResetForm.get_users.<locals>.<genexpr>)rl   Z_default_managerfilterZget_email_field_name)r&   rF   Z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| |
D ]z}|s4t|}|j}|j}n| }}|
||tt|j||||r`dndd}|	dk	rz|	|	 | j
|||||
|d qW dS )ze
        Generates a one-use only link for resetting password and sends to the
        user.
        rF   ZhttpsZhttp)rF   domain	site_nameuidrZ   tokenZprotocolN)r   )rO   r   r   r'   r   r   r   ZpkZ
make_tokenrK   r   )r&   Zdomain_overrider{   r|   Z	use_httpsZtoken_generatorr}   rj   r   Zextra_email_contextrF   rZ   Zcurrent_siter   r   r)   r,   r,   r-   rX     s&    


zPasswordResetForm.save)N)r.   r/   r0   r   Z
EmailFieldr\   rF   r   r   r   rX   r,   r,   r,   r-   rv      s   
rv   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   z%The two password fields didn't match.zNew passwordF)r   r<   rC   rD   zNew password confirmation)r   rC   r<   c                s   || _ tt| j|| d S )N)rZ   r    r   r5   )r&   rZ   r6   r7   )r+   r,   r-   r5   S  s    zSetPasswordForm.__init__c             C   sN   | j d}| j d}|r<|r<||kr<tj| jd ddt|| j |S )NrL   rM   rA   )rN   )rO   rP   r   rQ   rR   r   rU   rZ   )r&   rL   rM   r,   r,   r-   rV   W  s    zSetPasswordForm.clean_password2Tc             C   s*   | j d }| j| |r$| j  | jS )NrL   )rO   rZ   rY   rX   )r&   rW   rc   r,   r,   r-   rX   c  s
    

zSetPasswordForm.save)T)r.   r/   r0   r[   r\   rR   r   r]   ru   r   r^   rL   rM   r5   rV   rX   r1   r,   r,   )r+   r-   r   ?  s   
r   c               @   sb   e Zd ZdZeejfdediZej	eddej
ddidd	Zd
ddgZdd Zd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.original_passwordFrJ   T)r(   )r   rC   r<   password_originalrL   rM   c             C   s.   | j d }| j|s*tj| jd dd|S )zC
        Validates that the old_password field is correct.
        r   r   )rN   )rO   rZ   check_passwordr   rQ   rR   )r&   r   r,   r,   r-   clean_password_original{  s    
z*PasswordChangeForm.clean_password_originalc             C   sV   | j | jd r6|  }| j | |rR| j   ndtdtdf }t|d S )Nr   z%s %sr   base_inmedit_field_error)rZ   r   rO   rV   rY   rX   r\   r$   )r&   rW   rc   err_msgr,   r,   r-   rX     s    zPasswordChangeForm.saveN)T)r.   r/   r0   r[   dictr   rR   r\   r   r]   ru   r   Zfield_orderr   rX   r,   r,   r,   r-   r   k  s   
r   c                   s   e Zd ZdZededdZdZejedej	ddid	d
ed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.
    rA   Zsame_password_as_old)rA   password_same_as_oldr3   r   classzlayui-input)r(   Fz(Enter the original password for the user)r   r<   rC   rD   Z staffSetPwdForm_itemLabel_newPwdT)rJ   r   Z$staffSetPwdForm_itemLabel_confirmPwdz4Enter the same password as before, for verification.c                s   || _ tt| j|| d S )N)rZ   r    r   r5   )r&   rZ   r6   r7   )r+   r,   r-   r5     s    z AdminPasswordChangeForm.__init__c             C   sx   | j d}| j d}| j d}|rf|rf||krJtj| jd ddn||krftj| jd ddt|| j |S )NrL   rM   r   rA   )rN   r   )rO   rP   r   rQ   rR   r   rU   rZ   )r&   rL   rM   r   r,   r,   r-   rV     s    
z'AdminPasswordChangeForm.clean_password2c       
      C   s  | j | jd r|  }| j | |rt }tjjd|d	d}|rt
|d d }d}|| kryXtjj|| j d	dd	}x8|D ]0}i }|d |d
< |d	 |d< t|| j j qW W q   Y qX n | j jdgd ndtdtdf }	t|	dS )z)
        Saves the new password.
        r   Zalert_setting)r'   
company_idr   r   Zadminpasschange_alert)current_companyrE   rF   rE   Zmail_idrZ   rc   )Zupdate_fieldsz%s %sr   r   N)rZ   r   rO   rV   rY   r   r	   Zobjectsr   valuesjsonloadskeysrl   r   Zdelayr   rX   r\   r$   )
r&   rW   rc   r   Zpassword_change_alertr*   Z
user_emailxr~   r   r,   r,   r-   rX     s,    
zAdminPasswordChangeForm.savec                s2   t t| j}x| j D ]}||krg S qW dgS )Nrc   )r    r   changed_datarH   r   )r&   r8   r'   )r+   r,   r-   r     s
    z$AdminPasswordChangeForm.changed_data)T)r.   r/   r0   r[   r\   rR   Zrequired_css_classr   r]   ru   r   r   r^   rL   rM   r5   rV   rX   propertyr   r1   r,   r,   )r+   r-   r     s.   
(r   )BZ
__future__r   r>   r   Zdjangor   Zdjango.contrib.authr   r   r   Zdjango.contrib.auth.hashersr   r   Zmysite.base.modelsr	   Zdjango.contrib.auth.modelsr
   Zdjango.contrib.auth.tokensr   Zdjango.contrib.sites.shortcutsr   Zdjango.core.mailr   Zdjango.formsr   Zdjango.templater   Zdjango.utilsr   Zdjango.utils.encodingr   Zdjango.utils.httpr   Zdjango.utils.textr   Zdjango.utils.translationr   r   r\   Zmysite.admin.forms.widgetsr   r   Zmysite.admin.formsr   Zmysite.base.tasksr   Zmysite.accounts.modelsr   Zmysite.att.utilsr   rl   ZWidgetr   ZFieldr2   r]   r=   Z	ModelFormr@   r_   ZFormre   rv   r   r   r   r,   r,   r,   r-   <module>   sB   >QD,'