
 Xu:                 @   s#  d  d l  m Z d  d l 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 m Z d  d l m Z m Z d  d l m Z d  d l m Z d d l m Z m  Z  d d   Z! e j" e!  Gd d   d e j#  Z$ e Gd d   d e j%   Z& Gd d   d e j#  Z' e Gd d   d e j%   Z( Gd d   d e  Z) d d   Z* d d   Z+ d  d!   Z, Gd" d#   d# e j%  Z- Gd$ d%   d% e e-  Z. Gd& d'   d' e.  Z/ e Gd( d)   d) e0   Z1 d* S)+    )unicode_literals)auth)AbstractBaseUserBaseUserManager)user_logged_in)ContentType)PermissionDenied)	send_mail)models)EmptyManager)sixtimezone)CallableFalseCallableTrue)python_2_unicode_compatible)ugettext_lazy   )ASCIIUsernameValidatorUnicodeUsernameValidatorc             K   s&   t  j   | _ | j d d g  d S)zZ
    A signal receiver which updates the last_login date for
    the user logging in.
    Zupdate_fields
last_loginN)r   nowr   save)Zsenderuserkwargs r   H/home/ubuntu/projects/ifolica/build/django/django/contrib/auth/models.pyupdate_last_login   s    r   c               @   s"   e  Z d  Z d Z d d   Z d S)PermissionManagerTc             C   s1   |  j  d | d t j j |  j  j | |   S)Ncodenamecontent_type)getr   objectsZ
db_managerdbget_by_natural_key)selfr   	app_labelmodelr   r   r   r#       s    	z$PermissionManager.get_by_natural_keyN)__name__
__module____qualname__use_in_migrationsr#   r   r   r   r   r      s   r   c               @   s   e  Z d  Z d Z e j e d  d d Z e j e	 e j
 d e d  Z e j e d  d d Z e   Z Gd	 d
   d
  Z d d   Z d d   Z d g e _ d S)
Permissiona   
    The permissions system provides a way to assign permissions to specific
    users and groups of users.

    The permission system is used by the Django admin site, but may also be
    useful in your own code. The Django admin site uses permissions as follows:

        - The "add" permission limits the user's ability to view the "add" form
          and add an object.
        - The "change" permission limits a user's ability to view the change
          list, view the "change" form and change an object.
        - The "delete" permission limits the ability to delete an object.

    Permissions are set globally per type of object, not per specific object
    instance. It is possible to say "Mary may change news stories," but it's
    not currently possible to say "Mary may change news stories, but only the
    ones she created herself" or "Mary may only change news stories that have a
    certain status or publication date."

    Three basic permissions -- add, change and delete -- are automatically
    created for each Django model.
    name
max_length   verbose_namezcontent typer   d   c               @   s4   e  Z d  Z e d  Z e d  Z d	 Z d
 Z d S)zPermission.MetaZ
permissionpermissionsr   r   content_type__app_labelcontent_type__modelNzcontent_typecodename)r4   )r2   r3   r5   )r'   r(   r)   _r/   verbose_name_pluralZunique_togetherZorderingr   r   r   r   MetaH   s
    r8   c             C   s8   d t  j |  j j  t  j |  j  t  j |  j  f S)Nz%s | %s | %s)r   	text_typer   r%   r,   )r$   r   r   r   __str__O   s    zPermission.__str__c             C   s   |  j  f |  j j   S)N)r   r   natural_key)r$   r   r   r   r;   U   s    zPermission.natural_keyzcontenttypes.contenttypeN)r'   r(   r)   __doc__r
   	CharFieldr6   r,   Z
ForeignKeyr   ZCASCADEr   r   r   r!   r8   r:   r;   Zdependenciesr   r   r   r   r+   '   s   		r+   c               @   s(   e  Z d  Z d Z d Z d d   Z d S)GroupManagerz1
    The manager for the auth's Group model.
    Tc             C   s   |  j  d |  S)Nr,   )r    )r$   r,   r   r   r   r#   `   s    zGroupManager.get_by_natural_keyN)r'   r(   r)   r<   r*   r#   r   r   r   r   r>   Z   s   r>   c               @   s   e  Z d  Z d Z e j e d  d d d d Z e j e	 d e d  d	 d Z
 e   Z Gd
 d   d  Z d d   Z d d   Z d S)Groupa  
    Groups are a generic way of categorizing users to apply permissions, or
    some other label, to those users. A user can belong to any number of
    groups.

    A user in a group automatically has all the permissions granted to that
    group. For example, if the group Site editors has the permission
    can_edit_home_page, any user in that group will have that permission.

    Beyond permissions, groups are a convenient way to categorize users to
    apply some label, or extended functionality, to them. For example, you
    could create a group 'Special users', and you could write code that would
    do special things to those users -- such as giving them access to a
    members-only portion of your site, or sending them members-only email
    messages.
    r,   r-   P   uniqueTr/   r1   blankc               @   s(   e  Z d  Z e d  Z e d  Z d S)z
Group.MetagroupgroupsN)r'   r(   r)   r6   r/   r7   r   r   r   r   r8      s   r8   c             C   s   |  j  S)N)r,   )r$   r   r   r   r:      s    zGroup.__str__c             C   s
   |  j  f S)N)r,   )r$   r   r   r   r;      s    zGroup.natural_keyN)r'   r(   r)   r<   r
   r=   r6   r,   ManyToManyFieldr+   r1   r>   r!   r8   r:   r;   r   r   r   r   r?   d   s   !		r?   c               @   s@   e  Z d  Z d Z d d   Z d d d d  Z d d   Z d S)	UserManagerTc             K   su   | s t  d   n  |  j |  } |  j j |  } |  j d | d | |  } | j |  | j d |  j  | S)zW
        Creates and saves a User with the given username, email and password.
        zThe given username must be setusernameemailZusing)
ValueErrorZnormalize_emailr&   Znormalize_usernameset_passwordr   _db)r$   rG   rH   passwordextra_fieldsr   r   r   r   _create_user   s    zUserManager._create_userNc             K   s6   | j  d d  | j  d d  |  j | | | |  S)Nis_staffFis_superuser)
setdefaultrN   )r$   rG   rH   rL   rM   r   r   r   create_user   s    zUserManager.create_userc             K   s~   | j  d d  | j  d d  | j d  d k	 rD t d   n  | j d  d k	 rh t d   n  |  j | | | |  S)NrO   TrP   z"Superuser must have is_staff=True.z&Superuser must have is_superuser=True.)rQ   r    rI   rN   )r$   rG   rH   rL   rM   r   r   r   create_superuser   s    zUserManager.create_superuser)r'   r(   r)   r*   rN   rR   rS   r   r   r   r   rF      s   rF   c             C   sO   t    } x? t j   D]1 } t | d  r | j | j |  |   q q W| S)Nget_all_permissions)setr   get_backendshasattrupdaterT   )r   objr1   backendr   r   r   _user_get_all_permissions   s
    	 r[   c             C   sf   x_ t  j   D]Q } t | d  s( q n  y | j |  | |  rD d SWq t k
 r] d SYq Xq Wd S)zV
    A backend can raise `PermissionDenied` to short-circuit permission checking.
    has_permTF)r   rV   rW   r\   r   )r   permrY   rZ   r   r   r   _user_has_perm   s    r^   c             C   sc   x\ t  j   D]N } t | d  s( q n  y | j |  |  rA d SWq t k
 rZ d SYq Xq Wd S)zV
    A backend can raise `PermissionDenied` to short-circuit permission checking.
    has_module_permsTF)r   rV   rW   r_   r   )r   r%   rZ   r   r   r   _user_has_module_perms   s    r`   c               @   s
  e  Z d  Z d Z e j e d  d d d e d  Z e j e	 d e d  d	 d
 d e d  d d d d Z
 e j e d e d  d	 d
 d e d  d d d d Z Gd d   d  Z d d d  Z d d d  Z d d d  Z d d d  Z d d   Z d S)PermissionsMixinz
    A mixin class that adds the fields and methods necessary to support
    Django's Group and Permission model using the ModelBackend.
    zsuperuser statusdefaultF	help_textzPDesignates that this user has all permissions without explicitly assigning them.r/   rD   rB   TzaThe groups this user belongs to. A user will get all permissions granted to each of their groups.Zrelated_nameZuser_setZrelated_query_namer   zuser permissionsz#Specific permissions for this user.c               @   s   e  Z d  Z d Z d S)zPermissionsMixin.MetaTN)r'   r(   r)   abstractr   r   r   r   r8      s   r8   Nc             C   sO   t    } x? t j   D]1 } t | d  r | j | j |  |   q q W| S)z
        Returns a list of permission strings that this user has through their
        groups. This method queries all available auth backends. If an object
        is passed in, only permissions matching this object are returned.
        get_group_permissions)rU   r   rV   rW   rX   re   )r$   rY   r1   rZ   r   r   r   re      s
    	 z&PermissionsMixin.get_group_permissionsc             C   s   t  |  |  S)N)r[   )r$   rY   r   r   r   rT     s    z$PermissionsMixin.get_all_permissionsc             C   s&   |  j  r |  j r d St |  | |  S)a  
        Returns True if the user has the specified permission. This method
        queries all available auth backends, but returns immediately if any
        backend returns True. Thus, a user who has permission from a single
        auth backend is assumed to have permission in general. If an object is
        provided, permissions for this specific object are checked.
        T)	is_activerP   r^   )r$   r]   rY   r   r   r   r\     s    
zPermissionsMixin.has_permc             C   s+   x$ | D] } |  j  | |  s d Sq Wd S)z
        Returns True if the user has each of the specified permissions. If
        object is passed, it checks if the user has all required perms for this
        object.
        FT)r\   )r$   	perm_listrY   r]   r   r   r   	has_perms  s    zPermissionsMixin.has_permsc             C   s#   |  j  r |  j r d St |  |  S)z
        Returns True if the user has any permissions in the given app label.
        Uses pretty much the same logic as has_perm, above.
        T)rf   rP   r`   )r$   r%   r   r   r   r_      s    z!PermissionsMixin.has_module_perms)r'   r(   r)   r<   r
   BooleanFieldr6   rP   rE   r?   rD   r+   user_permissionsr8   re   rT   r\   rh   r_   r   r   r   r   ra      s6   			ra   c               @   s  e  Z d  Z d Z e j r$ e   n e   Z e	 j
 e d  d d d d d e d  d	 e g d
 i e d  d 6Z e	 j
 e d  d d d d Z e	 j
 e d  d d d d Z e	 j e d  d d Z e	 j e d  d d d e d  Z e	 j e d  d d d e d  Z e	 j e d  d e j Z e   Z d Z d g Z Gd d   d  Z d d   Z d d   Z d d  d!  Z d S)"AbstractUserz
    An abstract base class implementing a fully featured User model with
    admin-compliant permissions.

    Username and password are required. Other fields are optional.
    rG   r-      rA   Trc   zFRequired. 150 characters or fewer. Letters, digits and @/./+/-/_ only.
validatorsZerror_messagesz)A user with that username already exists.z
first name   rB   z	last namezemail addresszstaff statusrb   Fz9Designates whether the user can log into this admin site.ZactivezeDesignates whether this user should be treated as active. Unselect this instead of deleting accounts.zdate joinedrH   c               @   s.   e  Z d  Z e d  Z e d  Z d Z d S)zAbstractUser.Metar   ZusersTN)r'   r(   r)   r6   r/   r7   rd   r   r   r   r   r8   V  s   r8   c             C   s    d |  j  |  j f } | j   S)zU
        Returns the first_name plus the last_name, with a space in between.
        z%s %s)
first_name	last_namestrip)r$   Z	full_namer   r   r   get_full_name[  s    zAbstractUser.get_full_namec             C   s   |  j  S)z$Returns the short name for the user.)ro   )r$   r   r   r   get_short_nameb  s    zAbstractUser.get_short_nameNc             K   s    t  | | | |  j g |  d S)z.
        Sends an email to this User.
        N)r	   rH   )r$   subjectmessageZ
from_emailr   r   r   r   
email_userf  s    zAbstractUser.email_user) r'   r(   r)   r<   r   PY3r   r   Zusername_validatorr
   r=   r6   rG   ro   rp   Z
EmailFieldrH   ri   rO   rf   ZDateTimeFieldr   r   Zdate_joinedrF   r!   ZUSERNAME_FIELDZREQUIRED_FIELDSr8   rr   rs   rv   r   r   r   r   rk   ,  s<   	!!		rk   c               @   s/   e  Z d  Z d Z Gd d   d e j  Z d S)Userz
    Users within the Django authentication system are represented by this
    model.

    Username, password and email are required. Other fields are optional.
    c               @   s   e  Z d  Z d Z d S)z	User.MetaZAUTH_USER_MODELN)r'   r(   r)   Z	swappabler   r   r   r   r8   t  s   r8   N)r'   r(   r)   r<   rk   r8   r   r   r   r   rx   m  s   rx   c               @   s`  e  Z d  Z d Z d Z d Z d Z d Z d Z e	 e
  Z e	 e  Z d d   Z d d   Z d d	   Z d
 d   Z d d   Z d d   Z d d   Z d d   Z d d   Z d d   Z e e  Z d d   Z e e  Z d d d  Z d d d  Z d d d  Z d d  d!  Z d" d#   Z  e d$ d%    Z! e d& d'    Z" d( d)   Z# d S)*AnonymousUserN Fc             C   s   d  S)Nr   )r$   r   r   r   __init__  s    zAnonymousUser.__init__c             C   s   d S)Nry   r   )r$   r   r   r   r:     s    zAnonymousUser.__str__c             C   s   t  | |  j  S)N)
isinstance	__class__)r$   otherr   r   r   __eq__  s    zAnonymousUser.__eq__c             C   s   |  j  |  S)N)r   )r$   r~   r   r   r   __ne__  s    zAnonymousUser.__ne__c             C   s   d S)Nr   r   )r$   r   r   r   __hash__  s    zAnonymousUser.__hash__c             C   s   t  d   d  S)Nz=Django doesn't provide a DB representation for AnonymousUser.)NotImplementedError)r$   r   r   r   r     s    zAnonymousUser.savec             C   s   t  d   d  S)Nz=Django doesn't provide a DB representation for AnonymousUser.)r   )r$   r   r   r   delete  s    zAnonymousUser.deletec             C   s   t  d   d  S)Nz=Django doesn't provide a DB representation for AnonymousUser.)r   )r$   raw_passwordr   r   r   rJ     s    zAnonymousUser.set_passwordc             C   s   t  d   d  S)Nz=Django doesn't provide a DB representation for AnonymousUser.)r   )r$   r   r   r   r   check_password  s    zAnonymousUser.check_passwordc             C   s   |  j  S)N)_groups)r$   r   r   r   _get_groups  s    zAnonymousUser._get_groupsc             C   s   |  j  S)N)_user_permissions)r$   r   r   r   _get_user_permissions  s    z#AnonymousUser._get_user_permissionsc             C   s   t    S)N)rU   )r$   rY   r   r   r   re     s    z#AnonymousUser.get_group_permissionsc             C   s   t  |  d | S)NrY   )r[   )r$   rY   r   r   r   rT     s    z!AnonymousUser.get_all_permissionsc             C   s   t  |  | d | S)NrY   )r^   )r$   r]   rY   r   r   r   r\     s    zAnonymousUser.has_permc             C   s+   x$ | D] } |  j  | |  s d Sq Wd S)NFT)r\   )r$   rg   rY   r]   r   r   r   rh     s    zAnonymousUser.has_permsc             C   s   t  |  |  S)N)r`   )r$   moduler   r   r   r_     s    zAnonymousUser.has_module_permsc             C   s   t  S)N)r   )r$   r   r   r   is_anonymous  s    zAnonymousUser.is_anonymousc             C   s   t  S)N)r   )r$   r   r   r   is_authenticated  s    zAnonymousUser.is_authenticatedc             C   s   |  j  S)N)rG   )r$   r   r   r   get_username  s    zAnonymousUser.get_username)$r'   r(   r)   idpkrG   rO   rf   rP   r   r?   r   r+   r   r{   r:   r   r   r   r   r   rJ   r   r   propertyrD   r   rj   re   rT   r\   rh   r_   r   r   r   r   r   r   r   ry   x  s:   ry   N)2
__future__r   Zdjango.contribr   Zdjango.contrib.auth.base_userr   r   Zdjango.contrib.auth.signalsr   Z"django.contrib.contenttypes.modelsr   Zdjango.core.exceptionsr   Zdjango.core.mailr	   Z	django.dbr
   Zdjango.db.models.managerr   Zdjango.utilsr   r   Zdjango.utils.deprecationr   r   Zdjango.utils.encodingr   Zdjango.utils.translationr   r6   rm   r   r   r   connectManagerr   ZModelr+   r>   r?   rF   r[   r^   r`   ra   rk   rx   objectry   r   r   r   r   <module>   s<   
2
%"ZA