def setup(): """ Configure Django enough to use the ORM. """ cfg = config.parser(section='web_portal') # INSTALLED_APPS doesn't seem necessary so long as you are only accessing # existing tables. # # Setting charset to latin1 is a disgusting kludge, but without # this MySQL 5.6 (and, proably, later) gets tetchy about ASN.1 DER # stored in BLOB columns not being well-formed UTF8 (sic). If you # know of a better solution, tell us. settings.configure( DATABASES={ 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': cfg.get('sql-database'), 'USER': cfg.get('sql-username'), 'PASSWORD': cfg.get('sql-password'), 'OPTIONS': { 'charset': 'latin1', } } }, MIDDLEWARE_CLASSES=(), DOWNLOAD_DIRECTORY=cfg.get('download-directory', '/var/tmp'), ) if django.VERSION >= (1, 7): from django.apps import apps apps.populate(settings.INSTALLED_APPS)
def setup_django(): import django from django.conf import settings if not settings.configured: settings.configure( DEBUG=True, DATABASES={ 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:', } }, INSTALLED_APPS=( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django_ftpserver', ) ) django.setup() from django.apps import apps if not apps.ready: apps.populate()
def initialize_django(settings_module: str) -> Tuple["Apps", "LazySettings"]: with temp_environ(): os.environ["DJANGO_SETTINGS_MODULE"] = settings_module # add current directory to sys.path sys.path.append(os.getcwd()) def noop_class_getitem(cls, key): return cls from django.db import models models.QuerySet.__class_getitem__ = classmethod(noop_class_getitem) # type: ignore models.Manager.__class_getitem__ = classmethod(noop_class_getitem) # type: ignore # Define mypy builtins, to not cause NameError during setting up Django. # TODO: temporary/unpatch builtins.reveal_type = lambda _: None builtins.reveal_locals = lambda: None from django.apps import apps from django.conf import settings apps.get_models.cache_clear() # type: ignore apps.get_swappable_settings_name.cache_clear() # type: ignore if not settings.configured: settings._setup() apps.populate(settings.INSTALLED_APPS) assert apps.apps_ready assert settings.configured return apps, settings
def setup(): """ Configure Django enough to use the ORM. """ cfg = config.parser(section='web_portal') # INSTALLED_APPS doesn't seem necessary so long as you are only accessing # existing tables. # # Setting charset to latin1 is a disgusting kludge, but without # this MySQL 5.6 (and, proably, later) gets tetchy about ASN.1 DER # stored in BLOB columns not being well-formed UTF8 (sic). If you # know of a better solution, tell us. settings.configure( DATABASES={ 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': cfg.get('sql-database'), 'USER': cfg.get('sql-username'), 'PASSWORD': cfg.get('sql-password'), 'OPTIONS': { 'charset': 'latin1', } } }, MIDDLEWARE_CLASSES = (), DOWNLOAD_DIRECTORY = cfg.get('download-directory', '/var/tmp'), ) if django.VERSION >= (1, 7): from django.apps import apps apps.populate(settings.INSTALLED_APPS)
def reinitialize_django_apps(self): apps.app_configs = OrderedDict() # set ready to false so that populate will work apps.ready = False apps.loading = False # re-initialize them all; is there a way to add just one without reloading them all? apps.populate(settings.INSTALLED_APPS)
def initialize_django(settings_module: str) -> Tuple['Apps', 'LazySettings']: with temp_environ(): os.environ['DJANGO_SETTINGS_MODULE'] = settings_module # add current directory to sys.path sys.path.append(os.getcwd()) def noop_class_getitem(cls, key): return cls from django.db import models models.QuerySet.__class_getitem__ = classmethod( noop_class_getitem) # type: ignore models.Manager.__class_getitem__ = classmethod( noop_class_getitem) # type: ignore from django.conf import settings from django.apps import apps apps.get_models.cache_clear() # type: ignore apps.get_swappable_settings_name.cache_clear() # type: ignore if not settings.configured: settings._setup() apps.populate(settings.INSTALLED_APPS) assert apps.apps_ready assert settings.configured return apps, settings
def setup(set_prefix=True): """ Configure the settings (this happens as a side effect of accessing the first settingfrom django.utils.version import get_version VERSION = (3, 0, 4, 'final', 0) __version__ = get_version(VERSION) def setup(set_prefix=True): """ Configure the settings (this happens as a side effect of accessing the first setting), configure logging and populate the app registry. Set the thread-local urlresolvers script prefix if `set_prefix` is True. """ from django.apps import apps from django.conf import settings from django.urls import set_script_prefix from django.utils.log import configure_logging configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) if set_prefix: set_script_prefix( '/' if settings.FORCE_SCRIPT_NAME is None else settings.FORCE_SCRIPT_NAME ) apps.populate(settings.INSTALLED_APPS)
def __init__(self): if not settings.configured: from django.apps.config import AppConfig conf = { "INSTALLED_APPS": [type(self).__name__.lower()], "USE_TZ": True, "DATABASES": { "default": { "ENGINE": "django.db.backends.postgresql", "NAME": "pynab", "USER": "******", "PASSWORD": "", "HOST": "", "PORT": "", } }, } settings.configure(**conf) apps.populate(settings.INSTALLED_APPS) self.reader = None self.writer = None self.loop = None self.running = True signal.signal(signal.SIGUSR1, self.signal_handler)
def setup_django(): from django.conf import settings if not settings.configured: settings.configure( DEBUG=True, DATABASES={ 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:', } }, INSTALLED_APPS=( 'django.contrib.auth', 'django.contrib.contenttypes', 'django_ftpserver', ), MIDDLEWARE_CLASSES=(), FTPSERVER_ROOT='ftproot/', ) from django import VERSION as version # In Django 1.7 or later, using "django.apps" module. if version[0] == 1 and version[1] >= 7: from django.apps import apps if not apps.ready: apps.populate(settings.INSTALLED_APPS) from django.core.management import call_command call_command('syncdb', interactive=False) else: from django.db.models.loading import cache as model_cache if not model_cache.loaded: model_cache._populate() from django.core.management import call_command call_command('syncdb', interactive=False)
def configure(appname): if not settings.configured: from django.apps.config import AppConfig conf = { "INSTALLED_APPS": [appname], "USE_TZ": True, "DATABASES": { "default": { "ENGINE": "django.db.backends.postgresql", "NAME": "pynab", "USER": "******", "PASSWORD": "", "HOST": "", "PORT": "", } }, } if "PGDATABASE" in os.environ: conf["DATABASES"]["default"]["NAME"] = os.environ["PGDATABASE"] if "PGUSER" in os.environ: conf["DATABASES"]["default"]["USER"] = os.environ["PGUSER"] if "PGPASSWORD" in os.environ: conf["DATABASES"]["default"]["PASSWORD"] = os.environ["PGPASSWORD"] if "PGHOST" in os.environ: conf["DATABASES"]["default"]["HOST"] = os.environ["PGHOST"] if "PGPORT" in os.environ: conf["DATABASES"]["default"]["PORT"] = os.environ["PGPORT"] settings.configure(**conf) apps.populate(settings.INSTALLED_APPS)
def setup(set_prefix=True): """ Configure the settings (this happens as a side effect of accessing the first setting), configure logging and populate the app registry. Set the thread-local urlresolvers script prefix if `set_prefix` is True. 1 配置Django 加载配置; 设置日志; 初始化应用注册表 2 自动调用该函数 运行一个通过 DJANGO的 WSGI 支持的HTTP 服务 调用管理命令 """ # 0 加载配置 from django.apps import apps from django.conf import settings from django.urls import set_script_prefix from django.utils.log import configure_logging # 1 配置日志(导入dictConfig模块, 载入自定义配置) configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) if set_prefix: set_script_prefix('/' if settings.FORCE_SCRIPT_NAME is None else settings.FORCE_SCRIPT_NAME) # 2 初始化apps, 遍历调用AppConfig.create() apps.populate(settings.INSTALLED_APPS)
def set_up(): # windows system mysql driver pymysql.install_as_MySQLdb() conf = { 'INSTALLED_APPS': [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.messages', 'django.contrib.sessions', 'django.contrib.sitemaps', 'django.contrib.sites', 'django.contrib.staticfiles', 'web_api', 'user_info' ], 'DATABASES': { 'default': { 'ENGINE': db_conf.get('ENGINE'), 'NAME': db_conf.get('NAME'), 'USER': db_conf.get('USER'), 'PASSWORD': db_conf.get('PASSWORD'), 'HOST': db_conf.get('HOST'), 'PORT': db_conf.get('PORT') } }, 'TIME_ZONE': 'UTC' } settings.configure(**conf) apps.populate(settings.INSTALLED_APPS)
def setup_django(): from django.conf import settings if not settings.configured: settings.configure( DEBUG=True, DATABASES={ 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:', } }, INSTALLED_APPS=( 'django.contrib.auth', 'django.contrib.contenttypes', 'django_ftpserver', ), MIDDLEWARE_CLASSES=(), ) from django import VERSION as version # In Django 1.7 or later, using "django.apps" module. if version[0] == 1 and version[1] >= 7: from django.apps import apps if not apps.ready: apps.populate(settings.INSTALLED_APPS) from django.core.management import call_command call_command('migrate', interactive=False) else: from django.db.models.loading import cache as model_cache if not model_cache.loaded: model_cache._populate() from django.core.management import call_command call_command('syncdb', interactive=False)
def django_setup(): """ Our version of django.setup() that doesn't configure logging """ from django.apps import apps from django.conf import settings apps.populate(settings.INSTALLED_APPS)
def main(): apps.populate(settings.INSTALLED_APPS) setup() markdown_files = glob("**/*.md", recursive=True) exit_code = 0 for markdown_file in markdown_files: failed, attempted = testfile(markdown_file, module_relative=False) exit_code += failed sys.exit(exit_code)
def PluginInstall(self, pk): plugin = PyPlugin.objects.get(id=pk) plugin.installed = True with open('installed_apps.py', 'a+') as installed_apps_file: if installed_apps_file.write('apps.{}\n'.format(plugin.name.lower())): print('yes') else: print("no") # Como no se cargar una sola app, se leen todas las app que estan # como plugins en tiempo de ejecución al instalar cualquier app with open('%s/installed_apps.py' % settings.BASE_DIR, 'r') as ins_apps_file: for line in ins_apps_file.readlines(): settings.INSTALLED_APPS += (line.strip().rstrip('\n'), ) apps.app_configs = OrderedDict() apps.apps_ready = apps.models_ready = apps.loading = apps.ready = False apps.clear_cache() try: # Se recargan todas las aplicaciones ¿como cargar solo una? apps.populate(settings.INSTALLED_APPS) except: # plugin.installed = False print('Fallo el proceso de poblado de la app') try: # Se contruyen las migraciones del plugin call_command('makemigrations', plugin.name.lower(), interactive=False) except: # plugin.installed = False print('No hay migración de la app') try: # Se ejecutan las migraciones de la app call_command('migrate', plugin.name.lower(), interactive=False) except: # plugin.installed = False print('No se migro la app') try: # Se ejecutan las migraciones de la app call_command('loaddata', '{}.json'.format(plugin.name.lower()), interactive=False) except: # plugin.installed = False print('No se cargaron datos de la app') plugin.save() # subprocess.run[PROJECT_RELOAD] # Recargo en memoria la rutas del proyecto urlconf = settings.ROOT_URLCONF if urlconf in sys.modules: clear_url_caches() reload(sys.modules[urlconf]) return redirect(reverse('PyPlugin:list'))
def setUpClass(cls): super(DjangoUnitTest, cls).setUpClass() if apps: apps.populate(cls.apps) conn = connections['default'] with conn.schema_editor() as editor: for model in cls.models: editor.create_model(model)
def main(): apps.populate(settings.INSTALLED_APPS) management.call_command("migrate") management.call_command("loaddata", "examples.yaml") markdown_files = glob("**/*.md", recursive=True) exit_code = 0 for markdown_file in markdown_files: failed, attempted = testfile(markdown_file, module_relative=False) exit_code += failed sys.exit(exit_code)
def setup(): """ Configure the settings (this happens as a side effect of accessing the first setting), configure logging and populate the app registry. """ from django.apps import apps from django.conf import settings from django.utils.log import configure_logging configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) apps.populate(settings.INSTALLED_APPS)
def setup(): DB_FILE = NAME + '.db' with open(DB_FILE, 'w'): pass # wipe the database settings.configure(DEBUG=True, DATABASES={ DEFAULT_DB_ALIAS: { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': DB_FILE } }, LOGGING={ 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'debug': { 'format': '%(asctime)s[%(levelname)s]' '%(name)s.%(funcName)s(): %(message)s', 'datefmt': '%Y-%m-%d %H:%M:%S' } }, 'handlers': { 'console': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', 'formatter': 'debug' } }, 'root': { 'handlers': ['console'], 'level': 'WARN' }, 'loggers': { "django.db": { "level": "WARN" } } }) app_config = AppConfig(NAME, sys.modules['__main__']) apps.populate([app_config]) django.setup() original_new_func = ModelBase.__new__ @staticmethod def patched_new(cls, name, bases, attrs): if 'Meta' not in attrs: class Meta: app_label = NAME attrs['Meta'] = Meta return original_new_func(cls, name, bases, attrs) ModelBase.__new__ = patched_new
def setUp(self): """Set up test.""" google_provider = "allauth.socialaccount.providers.google" if google_provider not in settings.INSTALLED_APPS: settings.INSTALLED_APPS += (google_provider,) apps.app_configs = OrderedDict() apps.ready = False apps.populate(settings.INSTALLED_APPS) importlib.import_module(google_provider + ".provider") self.url = reverse("admin:superusertools_provider_overview", kwargs={"provider_id": "google"})
def setup(set_prefix=True): from django.apps import apps from django.conf import settings from django.urls import set_script_prefix from django.utils.log import configure_logging configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) if set_prefix: set_script_prefix('/' if settings.FORCE_SCRIPT_NAME is None else settings.FORCE_SCRIPT_NAME) apps.populate(settings.INSTALLED_APPS)
def _setup(): apps.populate(settings.INSTALLED_APPS) responses.add( responses.GET, "http://api.com/users/1/", json={ "id": 1, "name": "John", "surname": "Doe" }, ) responses.start()
def __init__(self, nabio): if not settings.configured: conf = { "INSTALLED_APPS": [type(self).__name__.lower()], "USE_TZ": True, "DATABASES": { "default": { "ENGINE": "django.db.backends.postgresql", "NAME": "pynab", "USER": "******", "PASSWORD": "", "HOST": "", "PORT": "", } }, } settings.configure(**conf) apps.populate(settings.INSTALLED_APPS) self.nabio = nabio self.idle_cv = asyncio.Condition() self.idle_queue = collections.deque() # Current position of ears in idle mode self.ears = { "left": Nabd.INIT_EAR_POSITION, "right": Nabd.INIT_EAR_POSITION, } self.info = {} # Info persists across service connections. self.state = State.IDLE # Dictionary of writers, i.e. connected services # For each writer, value is the list of registered events self.service_writers = {} self.interactive_service_writer = None # Events registered in interactive mode self.interactive_service_events = [] self.running = True self.loop = None self._ears_moved_task = None self.playing_cancelable = False self.playing_request_id = None Nabd.leds_boot(self.nabio, 2) if self.nabio.has_sound_input(): from . import i18n from .asr import ASR from .nlu import NLU config = i18n.Config.load() self._asr_locale = ASR.get_locale(config.locale) self.asr = ASR(self._asr_locale) Nabd.leds_boot(self.nabio, 3) self._nlu_locale = NLU.get_locale(config.locale) self.nlu = NLU(self._nlu_locale) Nabd.leds_boot(self.nabio, 4)
def add_app(app): from django.apps import apps from django.conf import settings from collections import OrderedDict # TODO 判断settings,如果不启用插件中心,就不注册 if app not in settings.INSTALLED_APPS: apps.app_configs = OrderedDict() apps.ready = False apps.loading = False settings.INSTALLED_APPS.append(app) apps.populate(settings.INSTALLED_APPS)
def test_django_single_report(self): self._override_settings( TEST_OUTPUT_DIR=self.tmpdir, TEST_OUTPUT_FILE_NAME='results.xml', TEST_OUTPUT_VERBOSE=0, TEST_RUNNER='xmlrunner.extra.djangotestrunner.XMLTestRunner') apps.populate(settings.INSTALLED_APPS) runner_class = get_runner(settings) runner = runner_class() suite = runner.build_suite() runner.run_suite(suite) expected_file = path.join(self.tmpdir, 'results.xml') self.assertTrue(path.exists(expected_file), 'did not generate xml report where expected.')
def test_django_multiple_reports(self): self._override_settings( TEST_OUTPUT_DIR=self.tmpdir, TEST_OUTPUT_VERBOSE=0, TEST_RUNNER='xmlrunner.extra.djangotestrunner.XMLTestRunner') apps.populate(settings.INSTALLED_APPS) runner_class = get_runner(settings) runner = runner_class() suite = runner.build_suite(test_labels=None) runner.run_suite(suite) test_files = glob.glob(path.join(self.tmpdir, 'TEST*.xml')) self.assertTrue(test_files, 'did not generate xml reports where expected.') self.assertEqual(2, len(test_files))
def done(): # 有app的时候才注册 if len(DATA.get("apps")) == 0: return # print('加载完成,开始注册') # 完成加载后,重新注册app from django.apps import apps from django.conf import settings from collections import OrderedDict apps.app_configs = OrderedDict() apps.ready = False apps.loading = False apps.populate(settings.INSTALLED_APPS)
def __django_resetup(self): ''' django.setup() должна выызваться только один раз что вызвает проблемы динамичного обновления моделей в apps этот метод представляет из себя маленький хак для динамического обновления моделей :return: ''' from django.apps import apps # with the configuration of loaded apps apps.app_configs = OrderedDict() # set ready to false so that populate will work apps.ready = False # re-initialize them all; is there a way to add just one without reloading them all? print('APP: ', self.INSTALLED_APPS) apps.populate(self.INSTALLED_APPS)
def setUp(self): """Set up test.""" google_provider = 'allauth.socialaccount.providers.google' if google_provider not in settings.INSTALLED_APPS: settings.INSTALLED_APPS += (google_provider, ) apps.app_configs = OrderedDict() apps.ready = False apps.populate(settings.INSTALLED_APPS) module = import_module(google_provider + '.provider') for cls in getattr(module, 'provider_classes', []): providers.registry.register(cls) self.url = reverse('admin:superusertools_provider_overview', kwargs={'provider_id': 'google'})
def test_django_runner_extension(self): from xmlrunner.extra.djangotestrunner import XMLTestRunner class MyDjangoRunner(XMLTestRunner): test_runner = mock.Mock() self._override_settings(TEST_OUTPUT_DIR=self.tmpdir, TEST_OUTPUT_VERBOSE=0) apps.populate(settings.INSTALLED_APPS) runner = MyDjangoRunner() suite = runner.build_suite(test_labels=None) runner.run_suite(suite) self.assertTrue(MyDjangoRunner.test_runner.called)
def get_media_dirs(): if not _media_dirs_cache: if not apps.ready: apps.populate(settings.INSTALLED_APPS) media_dirs = GLOBAL_MEDIA_DIRS[:] for app in apps.get_app_configs(): if app.name in IGNORE_APP_MEDIA_DIRS: continue media_dirs.append(os_path.join(app.path, u'static')) media_dirs.append(os_path.join(app.path, u'media')) _media_dirs_cache.extend(media_dirs) return _media_dirs_cache
def setUp(self): """Set up test.""" google_provider = 'allauth.socialaccount.providers.google' if google_provider not in settings.INSTALLED_APPS: settings.INSTALLED_APPS += (google_provider,) apps.app_configs = OrderedDict() apps.ready = False apps.populate(settings.INSTALLED_APPS) module = import_module(google_provider + '.provider') for cls in getattr(module, 'provider_classes', []): providers.registry.register(cls) self.url = reverse( 'admin:superusertools_provider_overview', kwargs={'provider_id': 'google'} )
def test_django_runner_extension(self): from xmlrunner.extra.djangotestrunner import XMLTestRunner class MyDjangoRunner(XMLTestRunner): test_runner = mock.Mock() self._override_settings( TEST_OUTPUT_DIR=self.tmpdir, TEST_OUTPUT_VERBOSE=0) apps.populate(settings.INSTALLED_APPS) runner = MyDjangoRunner() suite = runner.build_suite(test_labels=None) runner.run_suite(suite) self.assertTrue(MyDjangoRunner.test_runner.called)
def model_unpickle(model, vector, db, adding, _cache={}): try: cls = _cache[model] except KeyError: # Only needed in Django 1.8 and 1.9 if not apps.ready: apps.populate(settings.INSTALLED_APPS) cls = _cache[model] = apps.get_model(*model.split('.')) obj = cls.__new__(cls) obj.__dict__.update(izip(attnames(cls), vector)) # Restore state. This is the fastest way to create object I know. obj._state = ModelState.__new__(ModelState) obj._state.__dict__ = {'db': db, 'adding': adding} return obj
def setup(set_prefix=True): """ Configure the settings (this happens as a side effect of accessing the first setting), configure logging and populate the app registry. Set the thread-local urlresolvers script prefix if `set_prefix` is True. """ from django.apps import apps from django.conf import settings from django.urls import set_script_prefix from django.utils.log import configure_logging configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) if set_prefix: set_script_prefix( '/' if settings.FORCE_SCRIPT_NAME is None else settings.FORCE_SCRIPT_NAME ) apps.populate(settings.INSTALLED_APPS)
def setup(): DB_FILE = NAME + '.db' with open(DB_FILE, 'w'): pass # wipe the database settings.configure( DEBUG=True, DATABASES={ DEFAULT_DB_ALIAS: { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': DB_FILE}}, LOGGING={'version': 1, 'disable_existing_loggers': False, 'formatters': { 'debug': { 'format': '%(asctime)s[%(levelname)s]' '%(name)s.%(funcName)s(): %(message)s', 'datefmt': '%Y-%m-%d %H:%M:%S'}}, 'handlers': { 'console': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', 'formatter': 'debug'}}, 'root': { 'handlers': ['console'], 'level': 'WARN'}, 'loggers': { "django.db": {"level": "WARN"}}}) app_config = AppConfig(NAME, sys.modules['__main__']) apps.populate([app_config]) django.setup() original_new_func = ModelBase.__new__ @staticmethod def patched_new(cls, name, bases, attrs): if 'Meta' not in attrs: class Meta: app_label = NAME attrs['Meta'] = Meta return original_new_func(cls, name, bases, attrs) ModelBase.__new__ = patched_new
def setup(): """ Configure Django enough to use the ORM. """ cfg = config.parser(section='web_portal') # INSTALLED_APPS doesn't seem necessary so long as you are only accessing # existing tables. settings.configure( DATABASES={ 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': cfg.get('sql-database'), 'USER': cfg.get('sql-username'), 'PASSWORD': cfg.get('sql-password'), } }, MIDDLEWARE_CLASSES = (), DOWNLOAD_DIRECTORY = cfg.get('download-directory', '/var/tmp'), ) if django.VERSION >= (1, 7): from django.apps import apps apps.populate(settings.INSTALLED_APPS)
def setup_django(): import django from django.conf import settings if not settings.configured: settings.configure( DEBUG=True, DATABASES={ 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:', } }, INSTALLED_APPS=( 'django.contrib.auth', 'django.contrib.contenttypes', 'django_ftpserver', ) ) django.setup() from django.apps import apps if not apps.ready: apps.populate()
def _create_app_config(self, app_module): from django.apps import apps, AppConfig from django.conf import settings app = AppConfig(app_module.__name__, app_module) apps.populate(list(settings.INSTALLED_APPS) + [app]) return app
if not isinstance(settings, LazySettings): # this probably means django globals have been configured already, # which we don't want, since test cases reset and manipulate settings. raise RuntimeError("expected django.conf.settings to be LazySettings: %r" % (settings,)) # else configure a blank settings instance for the unittests if not settings.configured: settings.configure() # # init django apps w/ NO installed apps. # NOTE: required for django >= 1.9 # from django.apps import apps apps.populate(["django.contrib.contenttypes", "django.contrib.auth"]) #============================================================================= # support funcs #============================================================================= # flag for update_settings() to remove specified key entirely UNSET = object() def update_settings(**kwds): """helper to update django settings from kwds""" for k,v in iteritems(kwds): if v is UNSET: if hasattr(settings, k): delattr(settings, k) else:
def body(): global rpki ts = timestamp() for root, dirs, files in os.walk(test_dir, topdown = False): for fn in files: os.unlink(os.path.join(root, fn)) for d in dirs: os.rmdir(os.path.join(root, d)) if not quiet: print print "Reading YAML", yaml_file.name db = allocation_db(yaml.safe_load_all(yaml_file).next()) # Show what we loaded #db.dump() # Do pre-Django SQL setup pre_django_sql_setup(set(d.irdb_name for d in db if not d.is_hosted)) # Now ready for fun with multiple databases in Django! # https://docs.djangoproject.com/en/1.4/topics/db/multi-db/ # https://docs.djangoproject.com/en/1.4/topics/db/sql/ database_template = { "ENGINE" : "django.db.backends.mysql", "USER" : config_overrides["irdbd_sql_username"], "PASSWORD" : config_overrides["irdbd_sql_password"], "HOST" : "", "PORT" : "", "OPTIONS" : { "init_command": "SET storage_engine=INNODB" }} databases = dict((d.irdb_name, dict(database_template, NAME = d.irdb_name)) for d in db if not d.is_hosted) databases["default"] = databases[db.root.irdb_name] import django from django.conf import settings settings.configure( DATABASES = databases, DATABASE_ROUTERS = ["rpki.irdb.router.DBContextRouter"], MIDDLEWARE_CLASSES = (), INSTALLED_APPS = ("rpki.irdb",)) if django.VERSION >= (1, 7): # API change, feh from django.apps import apps apps.populate(settings.INSTALLED_APPS) import rpki.irdb rpki.irdb.models.ca_certificate_lifetime = rpki.sundial.timedelta(days = 3652 * 2) rpki.irdb.models.ee_certificate_lifetime = rpki.sundial.timedelta(days = 3652) ts() for d in db: if not quiet: print print "Configuring", d.name if not d.is_hosted: d.mkdir() if d.runs_pubd: d.mkdir("publication") if d.is_root: d.mkdir("publication.root") if not d.is_hosted: d.dump_conf() d.dump_rsyncd() d.dump_asns("%s.asns.csv" % d.name) d.dump_prefixes("%s.prefixes.csv" % d.name) d.dump_roas("%s.roas.csv" % d.name) d.dump_ghostbusters("%s.ghostbusters.vcard" % d.name) d.dump_router_certificates("%s.routercerts.xml" % d.name) if not d.is_hosted: if not quiet: print "Initializing SQL" d.syncdb() if not quiet: print "Hiring zookeeper" d.hire_zookeeper() with d.irdb: if not quiet: print "Creating identity" x = d.zoo.initialize() if d.is_root: if not quiet: print "Creating RPKI root certificate and TAL" d.dump_root() x = d.zoo.configure_rootd() else: with d.parent.irdb: x = d.parent.zoo.configure_child(x.file)[0] x = d.zoo.configure_parent(x.file)[0] with d.pubd.irdb: x = d.pubd.zoo.configure_publication_client(x.file, flat = flat_publication)[0] d.zoo.configure_repository(x.file) if loopback and not d.is_hosted: with d.irdb: d.zoo.write_bpki_files() ts() if not loopback: if not quiet: print for d in db: d.dump_sql()
import os os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' from django.apps import apps apps.populate(['testapp'])
def main(self): global rpki # pylint: disable=W0602 import django from django.conf import settings startup_msg = self.cfg.get("startup-message", "") if startup_msg: logger.info(startup_msg) # Do -not- turn on DEBUG here except for short-lived tests, # otherwise irdbd will eventually run out of memory and crash. # # If you must enable debugging, use django.db.reset_queries() to # clear the query list manually, but it's probably better just to # run with debugging disabled, since that's the expectation for # production code. # # https://docs.djangoproject.com/en/dev/faq/models/#why-is-django-leaking-memory settings.configure( DATABASES = { "default" : { "ENGINE" : "django.db.backends.mysql", "NAME" : self.cfg.get("sql-database"), "USER" : self.cfg.get("sql-username"), "PASSWORD" : self.cfg.get("sql-password"), "HOST" : "", "PORT" : "" }}, INSTALLED_APPS = ("rpki.irdb",), MIDDLEWARE_CLASSES = (), # API change, feh ) if django.VERSION >= (1, 7): # API change, feh from django.apps import apps apps.populate(settings.INSTALLED_APPS) import rpki.irdb # pylint: disable=W0621 # Entirely too much fun with read-only access to transactional databases. # # http://stackoverflow.com/questions/3346124/how-do-i-force-django-to-ignore-any-caches-and-reload-data # http://devblog.resolversystems.com/?p=439 # http://groups.google.com/group/django-users/browse_thread/thread/e25cec400598c06d # http://stackoverflow.com/questions/1028671/python-mysqldb-update-query-fails # http://dev.mysql.com/doc/refman/5.0/en/set-transaction.html # # It turns out that MySQL is doing us a favor with this weird # transactional behavior on read, because without it there's a # race condition if multiple updates are committed to the IRDB # while we're in the middle of processing a query. Note that # proper transaction management by the committers doesn't protect # us, this is a transactional problem on read. So we need to use # explicit transaction management. Since irdbd is a read-only # consumer of IRDB data, this means we need to commit an empty # transaction at the beginning of processing each query, to reset # the transaction isolation snapshot. import django.db.transaction self.start_new_transaction = django.db.transaction.commit_manually(django.db.transaction.commit) self.dispatch_vector = { rpki.left_right.list_resources_elt : self.handle_list_resources, rpki.left_right.list_roa_requests_elt : self.handle_list_roa_requests, rpki.left_right.list_ghostbuster_requests_elt : self.handle_list_ghostbuster_requests, rpki.left_right.list_ee_certificate_requests_elt : self.handle_list_ee_certificate_requests} try: self.http_server_host = self.cfg.get("server-host", "") self.http_server_port = self.cfg.getint("server-port") except: # pylint: disable=W0702 # # Backwards compatibility, remove this eventually. # u = urlparse.urlparse(self.cfg.get("http-url")) if (u.scheme not in ("", "http") or u.username is not None or u.password is not None or u.params or u.query or u.fragment): raise self.http_server_host = u.hostname self.http_server_port = int(u.port) self.cms_timestamp = None rpki.http.server( host = self.http_server_host, port = self.http_server_port, handlers = self.handler)
<span class="mini quiet">{% trans 'Unknown content' %}</span> {% endif %} </li> {% endfor %} </ul> {% endif %} </div> </div> {% endblock %} """ settings.configure() apps.populate(( 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', )) elapsed = 0 for i in xrange(500): #if pyston_loaded: # __pyston__.clearStats() start = time.time() template = Template(template_source, None, "admin/index.html") elapsed = time.time() - start print "took %4.1fms for last iteration" % (elapsed * 1000.0,)
from django.utils.version import get_version
def setup(): # Configure the settings (this happens as a side effect of accessing # INSTALLED_APPS or any other setting) and populate the app registry. from django.apps import apps from django.conf import settings apps.populate(settings.INSTALLED_APPS)
from django.conf import settings from django.core.management import call_command sys.path.insert(0, os.path.abspath(os.path.dirname(__file__))) settings.configure( INSTALLED_APPS=('futupayments',), DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':MEMORY:', } }, FUTUPAYMENTS_MERCHANT_ID='1', FUTUPAYMENTS_SECRET_KEY='1', FUTUPAYMENTS_TEST_MODE=True, ROOT_URLCONF='example.example.urls', MIDDLEWARE_CLASSES=(), ALLOWED_HOSTS=['*'], ) if __name__ == "__main__": try: from django.apps import apps except ImportError: pass else: apps.populate(settings.INSTALLED_APPS) call_command('test', 'futupayments')
class X(models.Model): class Meta: app_label = __name__ class Y(models.Model): xs = models.ManyToManyField(X, related_name="ys") class Meta: app_label = __name__ if __name__ == "__main__": from django.apps import apps apps.populate([__name__]) assert apps.get_models() != [] create_table(X) create_table(Y) xs = X.objects.bulk_create([X(id=1), X(id=2), X(id=3)]) ys = Y.objects.bulk_create([Y(id=1), Y(id=2), Y(id=3)]) xs[0].ys.add(ys[0]) xs[0].ys.add(ys[1]) xs[1].ys.add(ys[1]) xs[2].ys.add(ys[2]) print(xs[0].ys.all()) print(ys[0].xs.all()) # error?