ó
®â0_c           @  sÔ   d  Z  d d l m Z d d l Z d d l 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 d d	 l m Z d
 „  Z d „  Z d e e e d „ Z d „  Z e d „ Z d S(   uC   
Creates permissions for all installed apps that need permissions.
iÿÿÿÿ(   t   unicode_literalsN(   t   apps(   t   get_permission_codename(   t
   exceptions(   t   DEFAULT_DB_ALIASt   router(   t   six(   t   DEFAULT_LOCALE_ENCODINGc         C  s#   t  |  ƒ } t |  j ƒ } | | S(   uI   
    Returns (codename, name) for all permissions in the given opts.
    (   t   _get_builtin_permissionst   listt   permissions(   t   optst   builtint   custom(    (    sI   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/auth/management/__init__.pyt   _get_all_permissions   s    c         C  sG   g  } x: |  j  D]/ } | j t | |  ƒ d | |  j f f ƒ q W| S(   uy   
    Returns (codename, name) for all autogenerated permissions.
    By default, this is ('add', 'change', 'delete')
    u	   Can %s %s(   t   default_permissionst   appendR   t   verbose_name_raw(   R   t   permst   action(    (    sI   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/auth/management/__init__.pyR      s    i   c      	   K  s»  |  j  s d  S|  j } y7 | j | ƒ }  | j d d ƒ } | j d d ƒ } Wn t k
 ra d  SXt j | | ƒ sx d  St ƒ  }	 t ƒ  }
 xi |  j	 ƒ  D][ } | j
 j | ƒ j | ƒ } |
 j | ƒ x* t | j ƒ D] } |	 j | | f ƒ qÕ Wq— Wt | j
 j | ƒ j d |
 ƒ j d d ƒ ƒ } g  |	 D]B \ } \ } } | j | f | k r-| d | d	 | d
 | ƒ ^ q-} | j
 j | ƒ j | ƒ | d k r·x | D] } d | GHq¡Wn  d  S(   Nu   contenttypesu   ContentTypeu   authu
   Permissiont   content_type__inu   content_typeu   codenamet   codenamet   namet   content_typei   u   Adding permission '%s'(   t   models_modulet   labelt   get_app_configt	   get_modelt   LookupErrorR   t   allow_migrate_modelR	   t   sett
   get_modelst   objectst
   db_managert   get_for_modelt   addR   t   _metaR   t   usingt   filtert   values_listt   pkt   bulk_create(   t
   app_configt	   verbosityt   interactiveR%   R   t   kwargst	   app_labelt   ContentTypet
   Permissiont   searched_permst   ctypest   klasst   ctypet   permt	   all_permst   ctR   R   R   (    (    sI   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/auth/management/__init__.pyt   create_permissions(   s8    					6c          C  sc   y t  j ƒ  }  Wn t t f k
 r* d SXt j r_ y |  j t ƒ }  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    (	   t   getpasst   getusert   ImportErrort   KeyErrorR   t   PY2t   decodeR   t   UnicodeDecodeError(   t   result(    (    sI   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/auth/management/__init__.pyt   get_system_usernameY   s    	c         C  s  d d l  m } | j j j r# d St ƒ  } y= t j d | ƒ j d d ƒ j	 d ƒ j
 d d ƒ j ƒ  } Wn t k
 r} d SXy  | j j j d ƒ j | ƒ Wn t j k
 rµ d SX|  rý | rý y | j j j d	 | ƒ Wn | 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.
    iÿÿÿÿ(   t   modelsu    u   NFKDu   asciiu   ignoreu    u   usernamet   username(   t   django.contrib.authRB   t   UserR$   t   swappedRA   t   unicodedatat	   normalizet   encodeR>   t   replacet   lowerR?   t	   get_fieldt   run_validatorsR   t   ValidationErrort   _default_managert   gett   DoesNotExist(   t   check_dbt   auth_appt   default_username(    (    sI   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/auth/management/__init__.pyt   get_default_usernamep   s*    
	 (   t   __doc__t
   __future__R    R9   RG   t   django.appsR   t   global_appsRD   R   t   django.coreR   t	   django.dbR   R   t   django.utilsR   t   django.utils.encodingR   R   R   t   TrueR8   RA   RU   (    (    (    sI   /tmp/pip-unpacked-wheel-BAJOf3/django/contrib/auth/management/__init__.pyt   <module>   s   			1	