3
v^b                 @   s|   d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	 ddl
mZmZ ddlmZ G dd	 d	e	jZG d
d de	jZdS )zg
This module allows importing AbstractBaseUser even when django.contrib.auth is
not in INSTALLED_APPS.
    N)password_validation)check_passwordis_password_usablemake_password)models)get_random_stringsalted_hmac)gettext_lazyc               @   s*   e Zd Zedd Zd
ddZdd Zd	S )BaseUserManagerc             C   sJ   |pd}y|j  jdd\}}W n tk
r4   Y nX |d |j  }|S )zS
        Normalize the email address by lowercasing the domain part of it.
         @   )striprsplit
ValueErrorlower)clsemailZ
email_namedomain_part r   A/usr/lib/python3.6/site-packages/django/contrib/auth/base_user.pynormalize_email   s    zBaseUserManager.normalize_email
   7abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789c             C   s
   t ||S )z
        Generate a random password with the given length and given
        allowed_chars. The default value of allowed_chars does not have "I" or
        "O" or letters and digits that look similar -- just to avoid confusion.
        )r   )selflengthallowed_charsr   r   r   make_random_password    s    	z$BaseUserManager.make_random_passwordc             C   s   | j f | jj|iS )N)getmodelUSERNAME_FIELD)r   usernamer   r   r   get_by_natural_key+   s    z"BaseUserManager.get_by_natural_keyN)r   r   )__name__
__module____qualname__classmethodr   r   r"   r   r   r   r   r
      s    

r
   c                   s   e Zd ZejedddZejeddddZdZ	g Z
dZG dd	 d	Zd
d Z fddZdd Zdd Zdd Zedd Zedd Zdd Zdd Zdd Zdd Zd d! Zed"d# Zed$d% Z  ZS )&AbstractBaseUserpassword   )
max_lengthz
last loginT)blanknullNc               @   s   e Zd ZdZdS )zAbstractBaseUser.MetaTN)r#   r$   r%   abstractr   r   r   r   Meta;   s   r.   c             C   s   | j  S )N)get_username)r   r   r   r   __str__>   s    zAbstractBaseUser.__str__c                s0   t  j|| | jd k	r,tj| j|  d | _d S )N)supersave	_passwordr   Zpassword_changed)r   argskwargs)	__class__r   r   r2   A   s    
zAbstractBaseUser.savec             C   s   t | | jS )z"Return the username for this User.)getattrr    )r   r   r   r   r/   G   s    zAbstractBaseUser.get_usernamec             C   s   t | | j| j| j  d S )N)setattrr    normalize_usernamer/   )r   r   r   r   cleanK   s    zAbstractBaseUser.cleanc             C   s
   | j  fS )N)r/   )r   r   r   r   natural_keyN   s    zAbstractBaseUser.natural_keyc             C   s   dS )zj
        Always return False. This is a way of comparing User objects to
        anonymous users.
        Fr   )r   r   r   r   is_anonymousQ   s    zAbstractBaseUser.is_anonymousc             C   s   dS )zt
        Always return True. This is a way to tell if the user has been
        authenticated in templates.
        Tr   )r   r   r   r   is_authenticatedY   s    z!AbstractBaseUser.is_authenticatedc             C   s   t || _|| _d S )N)r   r(   r3   )r   raw_passwordr   r   r   set_passworda   s    
zAbstractBaseUser.set_passwordc                s    fdd}t | j|S )z~
        Return a boolean of whether the raw_password was correct. Handles
        hashing formats behind the scenes.
        c                s"    j |  d  _ jdgd d S )Nr(   )update_fields)r?   r3   r2   )r>   )r   r   r   setterj   s    
z/AbstractBaseUser.check_password.<locals>.setter)r   r(   )r   r>   rA   r   )r   r   r   e   s    zAbstractBaseUser.check_passwordc             C   s   t d | _d S )N)r   r(   )r   r   r   r   set_unusable_passwordq   s    z&AbstractBaseUser.set_unusable_passwordc             C   s
   t | jS )zX
        Return False if set_unusable_password() has been called for this user.
        )r   r(   )r   r   r   r   has_usable_passwordu   s    z$AbstractBaseUser.has_usable_passwordc             C   s   d}t || jj S )z7
        Return an HMAC of the password field.
        zAdjango.contrib.auth.models.AbstractBaseUser.get_session_auth_hash)r   r(   	hexdigest)r   key_saltr   r   r   get_session_auth_hash{   s    z&AbstractBaseUser.get_session_auth_hashc             C   s    y| j S  tk
r   dS X d S )Nr   )EMAIL_FIELDAttributeError)r   r   r   r   get_email_field_name   s    z%AbstractBaseUser.get_email_field_namec             C   s   t |trtjd|S |S )NNFKC)
isinstancestrunicodedata	normalize)r   r!   r   r   r   r9      s    z#AbstractBaseUser.normalize_username)r#   r$   r%   r   	CharField_r(   DateTimeField
last_login	is_activeREQUIRED_FIELDSr3   r.   r0   r2   r/   r:   r;   propertyr<   r=   r?   r   rB   rC   rF   r&   rI   r9   __classcell__r   r   )r6   r   r'   /   s(   r'   )__doc__rM   django.contrib.authr   Zdjango.contrib.auth.hashersr   r   r   	django.dbr   django.utils.cryptor   r   django.utils.translationr	   rP   Managerr
   Modelr'   r   r   r   r   <module>   s   