U
    Ha                     @   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-3jxiddxt/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                    s:  | 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
 fdd|D } j
|| |dkr6|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codenamec                    s0   g | ](\}\}}|j |fkr |||d qS ))r   namer   )pk).0ctr   r   r   Z	all_permsr   r   
<listcomp>N   s   
z&create_permissions.<locals>.<listcomp>r   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_listZbulk_createprint)Z
app_configr   r   r   r   kwargsZ	app_labelr   Zsearched_permsctypesklassctypepermr   r   r   r   create_permissions#   s@    

 
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_usernameY   s
    r4   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)r9   )django.contrib.authr5   ZUserr%   Zswappedr4   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_usernameh   s<    
  
rD   )__doc__r/   r;   Zdjango.appsr   Zglobal_appsr:   r   Z&django.contrib.contenttypes.managementr   Zdjango.corer   Z	django.dbr   r   r   r   r-   r4   rD   r   r   r   r   <module>   s   6