def guess_djt_panels(settings_dict): if not settings_dict["DEBUG"] or not settings_dict["USE_DEBUG_TOOLBAR"]: return [] panels = list(DEBUG_TOOLBAR_PANELS) if is_package_present("djt_og"): panels.insert(2, "djt_og.panel.OpenGraphPanel") if is_package_present("djt_csp"): panels.insert(2, "djt_csp.panel.SecurityPanel") if is_package_present("djt_nvu"): panels.insert(2, "djt_nvu.panel.W3ValidatorPanel") return panels
def cache_setting(settings_dict): """Automatically compute cache settings: * if debug mode is set, then caching is disabled * if django_redis is available, then Redis is used for caching * else memory is used :param settings_dict: :return: """ parsed_url = urlparse(settings_dict["CACHE_URL"]) django_version = get_complete_version() if settings_dict["DEBUG"]: return {"default": {"BACKEND": "django.core.cache.backends.dummy.DummyCache"}} elif django_version >= (4, 0) and parsed_url.scheme == "redis": # noinspection PyUnresolvedReferences return { "default": { "BACKEND": "django.core.cache.backends.redis.RedisCache", "LOCATION": "{CACHE_URL}", } } elif parsed_url.scheme == "redis": if is_package_present("django_redis"): # noinspection PyUnresolvedReferences return { "default": { "BACKEND": "django_redis.cache.RedisCache", "LOCATION": "{CACHE_URL}", "OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient"}, } } elif parsed_url.scheme == "memcache": if django_version >= (3, 2) and is_package_present("pymemcache"): backend = "django.core.cache.backends.memcached.PyMemcacheCache" elif django_version >= (3, 2) and is_package_present("pylibmc"): backend = "django.core.cache.backends.memcached.PyLibMCCache" elif is_package_present("memcache"): backend = "django.core.cache.backends.memcached.MemcachedCache" else: raise ImproperlyConfigured("Please install 'pylibmc' package before using memcache engine.") location = "%s:%s" % ( parsed_url.hostname or "localhost", parsed_url.port or 11211, ) return {"default": {"BACKEND": backend, "LOCATION": location,}} return { "default": { "BACKEND": "django.core.cache.backends.locmem.LocMemCache", "LOCATION": "unique-snowflake", } }
def use_sentry(settings_dict: Dict) -> bool: sentry_dsn = settings_dict["SENTRY_DSN"] if not sentry_dsn: return False if not is_package_present("sentry_sdk"): settings_check_results.append( Warning("sentry_sdk must be installed.", obj="configuration")) return False # noinspection PyUnresolvedReferences import sentry_sdk # noinspection PyUnresolvedReferences from sentry_sdk.integrations.django import DjangoIntegration # noinspection PyUnresolvedReferences from sentry_sdk.integrations.celery import CeleryIntegration integrations = [DjangoIntegration()] if settings_dict["USE_CELERY"]: integrations.append(CeleryIntegration()) sentry_sdk.init( dsn=sentry_dsn, integrations=integrations, traces_sample_rate=1.0, debug=settings_dict["DEBUG"], send_default_pii=True, ) return True
def run_gunicorn(self): sys.argv.pop(0) # noinspection PyPackageRequirements,PyUnresolvedReferences from gunicorn.config import KNOWN_SETTINGS, Setting # noinspection PyPackageRequirements,PyUnresolvedReferences from gunicorn.app.wsgiapp import WSGIApplication if settings.USE_WEBSOCKETS: application = self.get_asgi_application() worker_cls = "uvicorn.workers.UvicornWorker" if not is_package_present("uvicorn.workers"): self.stderr.write( "you must install uvicorn to use websockets with Gunicorn." ) return else: application = self.get_wsgi_application() worker_cls = "gunicorn.workers.gthread.ThreadWorker" class Application(WSGIApplication): def init(self, parser, opts, args): if not args: args.append(application) super().init(parser, opts, args) for setting in KNOWN_SETTINGS: # type: Setting if setting.name == "bind": setting.default = settings.LISTEN_ADDRESS elif setting.name == "worker_class": setting.default = worker_cls return Application("%(prog)s [OPTIONS] [APP_MODULE]").run()
def guess_pipeline_extension( candidates: List[Tuple[str, Optional[str], Optional[str]]], settings_dict: Dict, one: bool = False, ) -> Union[List[str], Optional[str]]: extensions = [] for extension, setting, package in candidates: if package and not is_package_present(package): continue if setting and not shutil.which(settings_dict[setting]): continue if one: return extension extensions.append(extension) if one: return None return extensions
use_sentry, web_server, csp_connect, ) from df_config.guesses.pipeline import ( pipeline_compilers, pipeline_css_compressor, pipeline_js_compressor, ) from df_config.guesses.staticfiles import ( pipeline_enabled, static_finder, static_storage, ) from df_config.utils import guess_version, is_package_present USE_CELERY = is_package_present("celery") USE_REDIS_SESSIONS = is_package_present("redis_sessions") USE_PIPELINE = is_package_present("pipeline") USE_DEBUG_TOOLBAR = is_package_present("debug_toolbar") USE_ALL_AUTH = is_package_present("allauth") USE_WEBSOCKETS = is_package_present("df_websockets") USE_SITE = is_package_present("df_site") USE_WHITENOISE = is_package_present("whitenoise") USE_CSP = is_package_present("csp") # ###################################################################################################################### # # settings that could be kept as-is for most projects # of course, you can override them in your default settings # # ######################################################################################################################
def test_is_package_present(self): self.assertTrue(is_package_present("df_config")) self.assertFalse(is_package_present("flask2"))