o 3aiD@stddlZddlZddlZddlZddlmZmZddlmZddlm Z m Z ddl m Z GdddZ e dd ZdS) N)Counter defaultdict)partial)AppRegistryNotReadyImproperlyConfigured) AppConfigc@seZdZdZd/ddZd0ddZdd Zd d Zd d ZddZ e j ddd1ddZ d2ddZ ddZddZddZddZe j dddd Zd!d"Zd#d$Zd%d&Zd'd(Zd)d*Zd+d,Zd-d.ZdS)3Appsz A registry that stores the configuration of installed applications. It also keeps track of models, e.g. to provide reverse relations. cCs|durttjtdrtdtt|_i|_g|_ d|_ |_ |_ t |_t |_d|_tt|_|durA||dSdS)Nappsz+You must supply an installed_apps argument.F)hasattrsysmodules__name__ RuntimeErrorrdict all_models app_configsstored_app_configs apps_ready models_readyready threadingEvent ready_eventRLock_lockloadinglist_pending_operationspopulate)selfinstalled_appsr r 6/usr/lib/python3/dist-packages/django/apps/registry.py__init__s    z Apps.__init__NcCsD|jrdS|j|jr WddS|jrtdd|_|D]%}t|tr+|}nt|}|j|jvr=t d|j||j|j<||_ q!t dd|j D}dd| D}|rgt d d |d|_|j D]}|qo|d|_|D]}|qd|_|jWddS1swYdS) z Load application configurations and models. Import each application module and then each model module. It is thread-safe and idempotent, but not reentrant. Nzpopulate() isn't reentrantTz0Application labels aren't unique, duplicates: %scss|]}|jVqdSNname.0 app_configr r r# es z Apps.populate..cSsg|] \}}|dkr|qS)rr )r)r'countr r r# gsz!Apps.populate..z/Application names aren't unique, duplicates: %s, )rrrr isinstancercreatelabelrrr rvalues most_commonjoinr import_models clear_cacherget_app_configsrset)r!r"entryr*counts duplicatesr r r#r =sV        "z Apps.populatecCs$|jsddlm}|jtddS)z9Raise an exception if all apps haven't been imported yet.r)settingszApps aren't loaded yet.N)r django.confr<INSTALLED_APPSr)r!r<r r r#check_apps_readys  zApps.check_apps_readycCs|jstddS)z;Raise an exception if all models haven't been imported yet.zModels aren't loaded yet.N)rrr!r r r#check_models_readyszApps.check_models_readycCs||jS)z:Import applications and return an iterable of app configs.)r?rr2r@r r r#r7s zApps.get_app_configscCsd|z|j|WSty1d|}|D]}|j|kr,|d|j7}t|qt|w)z Import applications and returns an app config for the given label. Raise LookupError if no application exists with this label. z!No installed app with label '%s'.z Did you mean '%s'?)r?rKeyErrorr7r'r1 LookupError)r! app_labelmessager*r r r#get_app_configs    zApps.get_app_config)maxsizeFcCs2|g}|jD] }||||q |S)a\ Return a list of all installed models. By default, the following models aren't included: - auto-created models for many-to-many relations without an explicit intermediate table, - models that have been swapped out. Set the corresponding keyword argument to True to include such models. )rArr2extend get_models)r!include_auto_createdinclude_swappedresultr*r r r#rIs  zApps.get_modelsTcCsZ|r|n||dur|d\}}||}|s&|jdur&||j||dS)a Return the model matching the given app_label and model_name. As a shortcut, app_label may be in the form .. model_name is case-insensitive. Raise LookupError if no application exists with this label, or no model exists with this name in the application. Raise ValueError if called with a single argument that doesn't contain exactly one dot. N.) require_ready)rAr?splitrFmodelsr5 get_model)r!rD model_namerNr*r r r#rQs  zApps.get_modelcCs|jj}|j|}||vr6|j||jkr*|j||jkr*tjd||ftddn td|||||f|||<| || dS)NzModel '%s.%s' was already registered. Reloading models is not advised as it can lead to inconsistencies, most notably with related models.) stacklevelz7Conflicting '%s' models in application '%s': %s and %s.) _metarRrr __module__warningswarnRuntimeWarningrdo_pending_operationsr6)r!rDmodelrR app_modelsr r r#register_models&   zApps.register_modelcs$|tfdd|jDS)z Check whether an application with this name exists in the registry. app_name is the full name of the app e.g. 'django.contrib.admin'. c3s|]}|jkVqdSr%r&)r)acapp_namer r#r+sz$Apps.is_installed..)r?anyrr2)r!r`r r_r# is_installedszApps.is_installedcCst|g}|jD] }||jr+|t|jd}|dks&|ddkr+||q |r8t|ddddSdS)a Look for an app config containing a given object. object_name is the dotted Python path to the object. Return the app config for the inner application in case of nesting. Return None if the object isn't in any registered app config. NrrMcSs t|j Sr%)lenr')r^r r r#s z0Apps.get_containing_app_config..)key)r?rr2 startswithr'rdappendsorted)r! object_name candidatesr*subpathr r r#get_containing_app_configs   zApps.get_containing_app_configcCs0|j||}|durtd||f|S)z Similar to get_model(), but doesn't require that an app exists with the given app_label. It's safe to call this method at import time, even while the registry is being populated. NzModel '%s.%s' not registered.)rgetlowerrC)r!rDrRr[r r r#get_registered_models  zApps.get_registered_modelcCsV|jddD]"}|jj}|r||kr|jjS|jjr(|jj|kr(|jjSqdS)a For a given model string (e.g. "auth.User"), return the name of the corresponding settings name if it refers to a swappable model. If the referred model is not swappable, return None. This method is decorated with lru_cache because it's performance critical when it comes to migrations. Since the swappable settings don't change after Django has loaded the settings, there is no reason to get the respective settings attribute over and over again. T)rKN)rIrUswapped swappabler1)r! to_stringr[rqr r r#get_swappable_settings_names    z Apps.get_swappable_settings_namecsntdd|D}|stdd||j|jfdd|jD|_| dS)at Restrict the set of installed apps used by get_app_config[s]. available must be an iterable of application names. set_available_apps() must be balanced with unset_available_apps(). Primarily used for performance optimization in TransactionTestCase. This method is safe in the sense that it doesn't trigger any imports. cSsh|]}|jqSr r&r(r r r# 8sz*Apps.set_available_apps..z?Available apps isn't a subset of installed apps, extra apps: %sr.cs i|] \}}|jvr||qSr r&)r)r1r* availabler r# @s  z+Apps.set_available_apps..N) r8r7issubset ValueErrorr4rrhritemsr6)r!rw installedr rvr#set_available_apps+s     zApps.set_available_appscCs|j|_|dS)z/Cancel a previous call to set_available_apps().N)rpoprr6r@r r r#unset_available_appsGs  zApps.unset_available_appscCsP|jstd|j|ji|_d|_|_|_|_|| |dS)a Enable a different set of installed apps for get_app_config[s]. installed must be an iterable in the same format as INSTALLED_APPS. set_installed_apps() must be balanced with unset_installed_apps(), even if it exits with an exception. Primarily used as a receiver of the setting_changed signal in tests. This method may trigger new imports, which may add new models to the registry of all imported models. They will stay in the registry even after unset_installed_apps(). Since it isn't possible to replay imports safely (e.g. that could lead to registering listeners twice), models are registered when they're imported and never removed. zApp registry isn't ready yet.FN) rrrrhrrrrr6r )r!r|r r r#set_installed_appsLszApps.set_installed_appscCs*|j|_d|_|_|_|dS)z/Cancel a previous call to set_installed_apps().TN)rr~rrrrr6r@r r r#unset_installed_appses  zApps.unset_installed_appscCsD|j|jr|jD]}|jddD]}|jqq dSdS)z| Clear all internal caches, for methods that alter the app registry. This is mostly used in tests. T)rJN)rI cache_clearrrr2rU _expire_cache)r!r*r[r r r#r6ks  zApps.clear_cachecsl|s|dS|^}fdd|_zj|}Wnty/j|YdSw|dS)a` Take a function and a number of ("app_label", "modelname") tuples, and when all the corresponding models have been imported and registered, call the function with the model classes as its arguments. The function passed to this method must accept exactly n models as arguments, where n=len(model_keys). cs"tj|}j|gRdSr%)rfunclazy_model_operation)r[ next_functionapply_next_model more_modelsr!r r#rs z3Apps.lazy_model_operation..apply_next_modelN)rrprCrrh)r!function model_keys next_model model_classr rr#r{s   zApps.lazy_model_operationcCs0|jj|jjf}|j|gD]}||qdS)z Take a newly-prepared model and pass it to each function waiting for it. This is called at the very end of Apps.register_model(). N)rUrDrRrr~)r!r[rfrr r r#rZs zApps.do_pending_operations)r r%)FF)NT)rrV __qualname____doc__r$r r?rAr7rF functools lru_cacherIrQr]rbrmrprtr}rrrr6rrZr r r r#r s0  )B        'r )r")rr rrW collectionsrrrdjango.core.exceptionsrrconfigrr r r r r r#s  "