o a@sdZddlZddlZddlmZddlmZddlm Z ddl m Z ddl m Z mZdd Zd d Zd d e efddZddZd e fddZdS)zC Creates permissions for all installed apps that need permissions. N)apps)get_permission_codename)create_contenttypes) exceptions)DEFAULT_DB_ALIASroutercCsgt||jS)zH Return (codename, name) for all permissions in the given opts. )_get_builtin_permissions permissions)optsr I/usr/lib/python3/dist-packages/django/contrib/auth/management/__init__.py_get_all_permissionssr cCs2g}|jD]}|t||d||jffq|S)z Return (codename, name) for all autogenerated permissions. By default, this is ('add', 'change', 'delete', 'view') z Can %s %s)default_permissionsappendrverbose_name_raw)r permsactionr r r rs  rTc s8|jsdSt|f||||d||j}z||}|dd}|ddWn ty2YdSwt|s;dSg}t} | D]!} |j |j | dd} | | t| jD] } || | fq[qDtj |j| dd d fd d |D} j || |d kr| D] } td| qdSdS)N) verbosity interactiveusingr contenttypes ContentTypeauth PermissionF)for_concrete_model)content_type__in content_typecodenamecs0g|]\}\}}|j|fvr|||dqS))rnamer)pk).0ctrrr all_permsr r Ns   z&create_permissions..rzAdding permission '%s') models_modulerlabelget_app_config get_model LookupErrorrallow_migrate_modelset get_modelsobjects db_manager get_for_modeladdr _metarrfilter values_list bulk_createprint) app_configrrrrkwargs app_labelrsearched_permsctypesklassctypepermrr r#r create_permissions#sJ       r?c Cs(zt}W|SttfyYdSw)zt Return the current system user's username, or an empty string if the username could not be determined. )getpassgetuser ImportErrorKeyError)resultr r r get_system_usernameYs  rFcCsddlm}|jjjr dSt}ztd|dd d dd }Wn t y/YdSwz |jj d|Wn tjyGYdSw|rg|rgz|jj|j|d WdS|jjyfY|Sw|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@NFKDasciiignore username)rL)django.contrib.authrGUserr2swappedrF unicodedata normalizeencodedecodereplacelowerUnicodeDecodeError get_fieldrun_validatorsrValidationError_default_managerr/get DoesNotExist)check_dbdatabaseauth_appdefault_usernamer r r get_default_usernamehs8    ra)__doc__rArP django.appsr global_appsrMr&django.contrib.contenttypes.managementr django.corer django.dbrrr rr?rFrar r r r s    6