def ready(self):
from django.core.management import call_command
from morango.models import InstanceIDModel
from morango.certificates import ScopeDefinition
from .signals import add_to_deleted_models # noqa: F401
# NOTE: Warning: https://docs.djangoproject.com/en/1.10/ref/applications/#django.apps.AppConfig.ready
# its recommended not to execute queries in this method, but we are producing the same result after the first call, so its OK
# call this on app load up to get most recent system config settings
try:
InstanceIDModel.get_or_create_current_instance()
if not ScopeDefinition.objects.filter():
call_command("loaddata", "scopedefinitions")
# we catch this error in case the database has not been migrated, b/c we can't query it until its been created
except (OperationalError, ProgrammingError):
pass
# add models to be synced by profile
add_syncable_models()
python类AppConfig()的实例源码
def test_get_all_contenttypes_for_app_config(mocker):
model1 = mocker.Mock(_meta=mocker.Mock(model_name='mymodel1'))
model2 = mocker.Mock(_meta=mocker.Mock(model_name='mymodel2'))
ct1 = ContentType(app_label='myapp', model='mymodel1')
ct2 = ContentType(app_label='myapp', model='mymodel2')
app_config = CustomAppConfig('myapp', mocker.Mock())
mocker.patch(
'django.apps.AppConfig.get_models', return_value=[model1, model2])
mock_get_for_model = mocker.patch(
'django.contrib.contenttypes.models.ContentTypeManager.get')
mock_get_for_model.side_effect = [ct1, ct2]
result = permissions.get_all_contenttypes_for_app_config(app_config)
assert result == [(ct1, model1), (ct2, model2)]
mock_get_for_model.side_effect = [ct1, ContentType.DoesNotExist]
result = permissions.get_all_contenttypes_for_app_config(app_config)
assert result == [(ct1, model1)]
def __init__(self):
"""Creates a new migration simulator with an empty
project state and no migrations."""
import psqlextra.apps
self.app_label = self._generate_random_name()
self.app_config = type(self.app_label, (AppConfig,), dict(
name=self.app_label,
verbose_name=self.app_label
))(self.app_label, psqlextra.apps)
self.app_config.models = {}
self.apps = Apps()
self.apps.ready = False
self.apps.populate(installed_apps=[self.app_config])
self.project_state = ProjectState()
self.migrations = []
def test_redirects_app_config():
"""Test 'redirects' module `AppConfig` instance"""
redirects_app_config = fakester_apps.get_app_config('redirects')
assert isinstance(redirects_app_config, AppConfig)
assert redirects_app_config.name == 'redirects'
assert redirects_app_config.verbose_name == 'redirects'
def ready(self):
# XXX: should be imported in the AppConfig of the respective app itself
import ecs.core.triggers
import ecs.votes.triggers
import ecs.meetings.triggers
# Patch the user __str__ method, so the hash in the username field
# does not show up.
# XXX: We should be using a custom user model instead.
from django.contrib.auth.models import User
from ecs.users.utils import get_full_name
User.__str__ = get_full_name
def ready(self):
"""Override the default :meth:`~django.apps.AppConfig.ready` method.
Registers :mod:`~tags.signals` used in the app.
"""
import tags.signals # noqa: F401
def ready(self):
"""Override the default :meth:`~django.apps.AppConfig.ready` method.
Registers :mod:`~distilleries.signals` used in the app.
"""
import distilleries.signals # noqa: F401
def ready(self):
"""Override the default :meth:`~django.apps.AppConfig.ready` method.
Registers :mod:`~warehouses.signals` used in the app.
"""
import warehouses.signals # noqa: F401
def test_get_known_models_for_app_config(mocker):
model1 = mocker.Mock(_meta=mocker.Mock(model_name='mymodel1'))
model2 = mocker.Mock(_meta=mocker.Mock(model_name='mymodel2'))
app_config = CustomAppConfig('myapp', mocker.Mock())
mocker.patch(
'django.apps.AppConfig.get_models', return_value=[model1, model2])
result = contenttypes.get_known_models_for_app_config(app_config)
assert result == {
'mymodel1': model1,
'mymodel2': model2,
}
def test_app_import(self):
"""Test importing djangolg app."""
from djangolg import apps
assert issubclass(apps.DjangolgConfig, AppConfig)
assert apps.DjangolgConfig.name == "djangolg"