U
    fr                     @   s   d 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 ddlmZmZ dd	 Zd
d ZddeefddZdd ZdefddZdS )zC
Creates permissions for all installed apps that need permissions.
    N)apps)get_permission_codename)create_contenttypes)
exceptions)DEFAULT_DB_ALIASrouterc                 C   s   t | | jS )zH
    Return (codename, name) for all permissions in the given opts.
    )_get_builtin_permissionspermissions)opts r   K/tmp/pip-unpacked-wheel-_jot26k8/django/contrib/auth/management/__init__.py_get_all_permissions   s    r   c                 C   s2   g }| j D ]"}|t|| d|| jf f q
|S )z
    Return (codename, name) for all autogenerated permissions.
    By default, this is ('add', 'change', 'delete', 'view')
    z	Can %s %s)Zdefault_permissionsappendr   Zverbose_name_raw)r
   permsactionr   r   r   r      s    
r      Tc                 K   st  | j s
d S t| f||||d| | j}z&||} |dd}|dd}W n tk
rf   Y d S X t||sxd S g }	t }
| 	 D ]B}|j
|j|dd}|
| t|jD ]}|	||f qqt|j
|j|
dd	d
}g }|	D ]D\}\}}|j|f|kr| }||j_||_||_||_|| q|j
|| |dkrp|D ]}td|  q\d S )N)	verbosityinteractiveusingr   ZcontenttypesContentTypeauth
PermissionF)Zfor_concrete_model)Zcontent_type__incontent_typecodenamer   zAdding permission '%s')Zmodels_moduler   labelZget_app_configZ	get_modelLookupErrorr   Zallow_migrate_modelsetZ
get_modelsZobjects
db_managerZget_for_modeladdr   _metar   r   filterZvalues_listpk_statedbr   namer   Zbulk_createprint)Z
app_configr   r   r   r   kwargsZ	app_labelr   r   Zsearched_permsctypesklassctypepermZ	all_permsr   ctr   r$   Z
permissionr   r   r   create_permissions%   sh    	
 
 
r,   c               	   C   s,   zt  } W n ttfk
r&   Y dS X | S )zt
    Return the current system user's username, or an empty string if the
    username could not be determined.
     )getpassgetuserImportErrorKeyError)resultr   r   r   get_system_usernamer   s
    r3   c                 C   s   ddl m} |jjjrdS t }z*td|dd	d
dd }W n tk
r`   Y dS X z|jjd| W n tjk
r   Y dS X | r|rz|jj|j|d	 W n |jjk
r   Y nX dS |S )
a  
    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).
    :param database: The database where the unique check will be performed.
    :returns: The username, or an empty string if no username can be
        determined or the suggested username is already taken.
    r   )modelsr-   ZNFKDasciiignore username)r8   )django.contrib.authr4   ZUserr   Zswappedr3   unicodedata	normalizeencodedecodereplacelowerUnicodeDecodeError	get_fieldZrun_validatorsr   ZValidationErrorZ_default_managerr   getZDoesNotExist)Zcheck_dbZdatabaseZauth_appZdefault_usernamer   r   r   get_default_username   s<    
  
rC   )__doc__r.   r:   Zdjango.appsr   Zglobal_appsr9   r   Z&django.contrib.contenttypes.managementr   Zdjango.corer   Z	django.dbr   r   r   r   r,   r3   rC   r   r   r   r   <module>   s    
M