a
    ¥Q•hr  ã                   @   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   g t | ƒ¢| j¢S )zH
    Return (codename, name) for all permissions in the given opts.
    )Ú_get_builtin_permissionsÚpermissions)Úopts© r   ú`/var/www/sistema_ama/venv/lib/python3.9/site-packages/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)Údefault_permissionsÚappendr   Úverbose_name_raw)r
   ÚpermsÚactionr   r   r   r      s    
þÿr   é   Tc                 K   sr  | j s
d S t| f||||dœ|¤Ž | j}z&| |¡} | dd¡}| dd¡}W n tyd   Y d S 0 t ||¡svd S g }	tƒ }
|  	¡ D ]B}|j
 |¡j|dd�}|
 |¡ t|jƒD ]}|	 ||f¡ q¶qˆt|j
 |¡j|
d� d	d
¡ƒ}g }|	D ]D\}\}}|j|f|vrô|ƒ }||j_||_||_||_| |¡ qô|j
 |¡ |¡ |dk�rn|D ]}td| ƒ �qZd S )N)Ú	verbosityÚinteractiveÚusingr   ZcontenttypesÚContentTypeÚauthÚ
PermissionF)Úfor_concrete_model)Zcontent_type__inÚcontent_typeÚcodenamer   zAdding permission '%s')Úmodels_moduler   ÚlabelÚget_app_configÚ	get_modelÚLookupErrorr   Úallow_migrate_modelÚsetÚ
get_modelsÚobjectsÚ
db_managerÚget_for_modelÚaddr   Ú_metar   r   ÚfilterÚvalues_listÚpkÚ_stateÚdbr   Únamer   Úbulk_createÚprint)Ú
app_configr   r   r   r   ÚkwargsÚ	app_labelr   r   Zsearched_permsZctypesÚklassÚctypeÚpermZ	all_permsr   Úctr   r/   Ú
permissionr   r   r   Úcreate_permissions%   sd    ÿûú	
ÿ
þüÿ
r:   c               	   C   s*   zt  ¡ } W n ttfy$   Y dS 0 | S )zt
    Return the current system user's username, or an empty string if the
    username could not be determined.
    Ú )ÚgetpassZgetuserÚImportErrorÚKeyError)Úresultr   r   r   Úget_system_usernamer   s
    r@   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y^   Y dS 0 z|jj d¡ |¡ W n tjyŽ   Y dS 0 | rÎ|rÎz|jj |¡j|d	� W n |jjyÈ   Y n0 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;   ÚNFKDÚasciiÚignoreú Úusername)rF   )Údjango.contrib.authrA   ZUserr)   Úswappedr@   ÚunicodedataÚ	normalizeÚencodeÚdecodeÚreplaceÚlowerÚUnicodeDecodeErrorÚ	get_fieldÚrun_validatorsr   ÚValidationErrorÚ_default_managerr&   ÚgetÚDoesNotExist)Zcheck_dbÚdatabaseZauth_appZdefault_usernamer   r   r   Úget_default_username�   s8    
ÿþýÿÿ
rW   )Ú__doc__r<   rI   Údjango.appsr   Zglobal_appsrG   r   Z&django.contrib.contenttypes.managementr   Údjango.corer   Ú	django.dbr   r   r   r   r:   r@   rW   r   r   r   r   Ú<module>   s    û
M