U
    5øi  ã                   @   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-gcyiijx6/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¡ q¸qŠtˆ 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Ô|rÔz|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