ó
ø¢TQc           @  s@  d  Z  d d l m Z d d l Z d d l Z d d l 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 l m Z m Z d d	 l m Z d d
 l m Z d „  Z d „  Z d „  Z d „  Z e d „ Z d „  Z d „  Z e d „ Z  e j! j" e d d ƒe j! j" e d e d d ƒd S(   uC   
Creates permissions for all installed apps that need permissions.
iÿÿÿÿ(   t   unicode_literalsN(   t   modelst   get_user_model(   t
   exceptions(   t   CommandError(   t   DEFAULT_DB_ALIASt   router(   t
   get_modelst   signals(   t   six(   t   inputc         C  s   d |  | j  j ƒ  f S(   Nu   %s_%s(   t   object_namet   lower(   t   actiont   opts(    (    s@   ../Django//lib/python/django/contrib/auth/management/__init__.pyt   _get_permission_codename   s    c         C  s3   t  |  ƒ } t |  j ƒ } t | | | ƒ | | S(   uI   
    Returns (codename, name) for all permissions in the given opts.
    (   t   _get_builtin_permissionst   listt   permissionst   _check_permission_clashing(   R   t   ctypet   builtint   custom(    (    s@   ../Django//lib/python/django/contrib/auth/management/__init__.pyt   _get_all_permissions   s    c         C  sD   g  } x7 d D]/ } | j  t | |  ƒ d | |  j f f ƒ q W| S(   uE   
    Returns (codename, name) for all autogenerated permissions.
    u   addu   changeu   deleteu	   Can %s %s(   u   addu   changeu   delete(   t   appendR   t   verbose_name_raw(   R   t   permsR   (    (    s@   ../Django//lib/python/django/contrib/auth/management/__init__.pyR       s
    c         C  s¯   t  ƒ  } t  d „  | Dƒ ƒ } x‰ |  D] \ } } | | k rf t d | | j | j ƒ  j f ƒ ‚ n4 | | k rš t d | | j | j ƒ  j f ƒ ‚ n  | j | ƒ q& Wd S(   uv   
    Check that permissions for a model do not clash. Raises CommandError if
    there are duplicate permissions.
    c         s  s   |  ] } | d  Vq d S(   i    N(    (   t   .0t   p(    (    s@   ../Django//lib/python/django/contrib/auth/management/__init__.pys	   <genexpr>0   s    u=   The permission codename '%s' is duplicated for model '%s.%s'.uQ   The permission codename '%s' clashes with a builtin permission for model '%s.%s'.N(   t   setR   t	   app_labelt   model_classt   __name__t   add(   R   R   R   t   poolt   builtin_codenamest   codenamet   _name(    (    s@   ../Django//lib/python/django/contrib/auth/management/__init__.pyR   *   s    	""c      	   K  s~  t  j | t j ƒ s d  Sd d l m } t |  ƒ } t ƒ  } t ƒ  } xf | D]^ }	 | j	 j
 | ƒ j |	 ƒ }
 | j |
 ƒ x- t |	 j |
 ƒ D] } | j |
 | f ƒ q WqN Wt t j j	 j | ƒ j d | ƒ j d d ƒ ƒ } g  | D]E \ }
 \ } } |
 j | f | k rê t j d | d | d |
 ƒ ^ qê } t j j	 j | ƒ j | ƒ | d	 k rzx | D] } d
 | GHqdWn  d  S(   Niÿÿÿÿ(   t   ContentTypet   content_type__inu   content_typeu   codenameR$   t   namet   content_typei   u   Adding permission '%s'(   R   t   allow_syncdbt   auth_appt
   Permissiont"   django.contrib.contenttypes.modelsR&   R   R   R   t   objectst
   db_managert   get_for_modelR!   R   t   _metaR   t   usingt   filtert   values_listt   pkt   bulk_create(   t   appt   created_modelst	   verbosityt   dbt   kwargsR&   t
   app_modelst   searched_permst   ctypest   klassR   t   permt	   all_permsR$   R(   R   (    (    s@   ../Django//lib/python/django/contrib/auth/management/__init__.pyt   create_permissions=   s*    			9c   	      K  sš   d d l  m } t ƒ  } | | k r– | j d t ƒ r– d } t | ƒ } xJ | d k rj t d ƒ } qL n  | d k r | d d	 t d
 | ƒn  PqL n  d  S(   Niÿÿÿÿ(   t   call_commandu   interactiveu‰   
You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): u   yesu   nou#   Please enter either "yes" or "no": u   createsuperusert   interactivet   database(   u   yesu   no(   t   django.core.managementRC   R   t   gett   TrueR
   (	   R7   R8   R9   R:   R;   RC   t	   UserModelt   msgt   confirm(    (    s@   ../Django//lib/python/django/contrib/auth/management/__init__.pyt   create_superuserf   s    	c          C  s–   y t  j ƒ  }  Wn t t f k
 r* d SXt j s’ y t j ƒ  d } Wn t k
 r\ d SX| sg d Sy |  j	 | ƒ }  Wq’ t
 k
 rŽ d SXn  |  S(   u³   
    Try to determine the current system user's username.

    :returns: The username as a unicode string, or an empty string if the
        username could not be determined.
    u    i   (   t   getpasst   getusert   ImportErrort   KeyErrorR	   t   PY3t   localet   getdefaultlocalet
   ValueErrort   decodet   UnicodeDecodeError(   t   resultt   default_locale(    (    s@   ../Django//lib/python/django/contrib/auth/management/__init__.pyt   get_system_usernamey   s     	c         C  sñ   t  j j j r d St ƒ  } y= t j d | ƒ j d d ƒ j d ƒ j	 d d ƒ j
 ƒ  } Wn t k
 rm d SXy  t  j j j d ƒ j | ƒ Wn t j k
 r¥ d SX|  rí | rí y t  j j j d | ƒ Wn t  j j k
 rå qí Xd Sn  | S(   u?  
    Try to determine the current system user's username to use as a default.

    :param check_db: If ``True``, requires that the username does not match an
        existing ``auth.User`` (otherwise returns an empty string).
    :returns: The username, or an empty string if no username can be
        determined.
    u    u   NFKDu   asciiu   ignoreu    u   usernamet   username(   R+   t   UserR1   t   swappedRY   t   unicodedatat	   normalizet   encodeRU   t   replaceR   RV   t	   get_fieldt   run_validatorsR   t   ValidationErrort   _default_managerRG   t   DoesNotExist(   t   check_dbt   default_username(    (    s@   ../Django//lib/python/django/contrib/auth/management/__init__.pyt   get_default_username–   s&    	+ t   dispatch_uidu1   django.contrib.auth.management.create_permissionst   senderu/   django.contrib.auth.management.create_superuser(#   t   __doc__t
   __future__R    RM   RR   R]   t   django.contrib.authR   R+   R   t   django.coreR   t   django.core.management.baseR   t	   django.dbR   R   t   django.db.modelsR   R   t   django.utilsR	   t   django.utils.six.movesR
   R   R   R   R   RB   RL   RY   RH   Rh   t   post_syncdbt   connect(    (    (    s@   ../Django//lib/python/django/contrib/auth/management/__init__.pyt   <module>   s.   				
	)		%