def apply_settings(vars): MODE = os.environ.get('DJANGO_MODE', None) if MODE is None: raise ImproperlyConfigured( 'Environment variable DJANGO_MODE is not set') cbs.apply('Settings%s' % MODE, vars)
ADMINS = [('Gin', '*****@*****.**')] MANAGERS = ADMINS # NOTE: define the correct hosts in production! ALLOWED_HOSTS = [ 'qzandronum.com', '70.35.196.63', 'www.qzandronum.com', 'q-zandronum.com', 'www.q-zandronum.com' ] DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'qzandronum', } } @cbs.env def SECRET_KEY(self): '''Gets its value from os.environ['{cbs.DEFAULT_ENV_PREFIX}_SECRET_KEY']''' raise ImproperlyConfigured( 'SECRET_KEY must be securily provided via env variable!') MODE = environ.get(f'{cbs.DEFAULT_ENV_PREFIX}MODE', 'Dev') SETTINGS_CLASS = f'{MODE.title()}Settings' cbs.apply(SETTINGS_CLASS, globals()) print(f"{PROJECT_NAME.title()} v.{__version__}" # noqa: F821 f" on python {platform.python_version()}" f" [{SETTINGS_CLASS}] DEBUG={DEBUG}") # noqa: F821
def test_apply_settings_string_reference(self): cbs.apply(__name__ + '.AttrSettings', globals()) self.assertEqual(PROJECT_NAME, 'fancy_project')
def test_precedence_non_empty_global_setting(self): g = {} cbs.apply(MySettings, g) self.assertIn('default', g['CACHES']) self.assertIn('custom', g['CACHES'])
return '/media/' @cbs.env(key='DJANGO_SECRET_KEY') def SECRET_KEY(self): return '' @cbs.env(key='DJANGO_STATIC_ROOT') def STATIC_ROOT(self): return os.path.join(self.BASE_DIR, 'static') @cbs.env(key='DJANGO_STATIC_URL') def STATIC_URL(self): return '/static/' class API(Base): ROOT_URLCONF = 'api.urls' WSGI_APPLICATION = 'api.wsgi.application' class Testing(Base): ALLOWED_HOSTS = ['localhost', '127.0.0.1', '[::1]'] class Development(Testing): pass MODE = os.environ['DJANGO_MODE'] cbs.apply(MODE, globals())
def test_apply_settings_string_local(self): g = {'AttrSettings': AttrSettings} cbs.apply('AttrSettings', g) self.assertEqual(g['PROJECT_NAME'], 'fancy_project')
def test_apply_settings_attr(self): g = {} cbs.apply(AttrSettings, g) self.assertEqual(g['PROJECT_NAME'], 'fancy_project')
def test_apply_settings_method(self): cbs.apply(MethodSettings, globals()) self.assertEqual(PROJECT_NAME, 'fancy_project')
'image/webp', ] MARKDOWNX_SERVER_CALL_LATENCY = 2500 class DevSettings(BaseSettings): pass class TestSettings(DevSettings): pass class BetaSettings(BaseSettings): pass class ProdSettings(BetaSettings): STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage' ALLOWED_HOSTS = [ 'localhost', '127.0.0.1', '::1', '172.105.169.83', 'uniquode.io', 'www.uniquode.io', ] cbs.apply(f'{MODE}Settings', globals())
def test_apply_settings_string_reference(self): g = {} cbs.apply(__name__ + '.AttrSettings', g) self.assertEqual(g['PROJECT_NAME'], 'fancy_project')
def test_apply_settings_method(self): g = {} cbs.apply(MethodSettings, g) self.assertEqual(g['PROJECT_NAME'], 'fancy_project')
def test_dont_call(self): g = {} cbs.apply(MethodSettings, g) self.assertTrue(g['CALLABLE_THING']() is True)
def test_precedence(self): g = {} cbs.apply(MySettings, g) self.assertTrue(len(g['TEMPLATE_LOADERS']), 3)
import os import cbs mode = os.environ.get('DJANGO_MODE', 'Local') cbs.apply('settings.{}.{}Settings'.format(mode.lower(), mode.title()), globals())
def test_apply_settings_attr(self): cbs.apply(AttrSettings, globals()) self.assertEqual(PROJECT_NAME, 'fancy_project')
DEBUG = True DEFAULT_DB = 'postgres://*****:*****@localhost:5434/yanlog' INSTALLED_APPS = Base.INSTALLED_APPS + ( 'debug_toolbar', 'django_extensions', ) class Test(Base): """ Settings for testing env """ @cbs.env def DEFAULT_DB(self): # Uses postgres started by docker-compose by default return 'postgres://*****:*****@localhost:5434/yanlog' SECRET_KEY = 'ul06ndw!fop^owsfzx1x#zh)!%2scv!#!ox1e^9%rrz1&v^bf-' class Prod(Base): """ Settings for production """ DEBUG = False MODE = os.environ.get('DJANGO_MODE', 'Local').title() cbs.apply(MODE, globals())
'default': dj_database_url.config( default='postgres://*****:*****@localhost:5432/connectdb' ) } # TESTING TEST_RUNNER = 'django_behave.runner.DjangoBehaveTestSuiteRunner' class ProductionSettings(BaseSettings): @property def INSTALLED_APPS(self): return super().INSTALLED_APPS + ('djrill',) EMAIL_BACKEND = 'djrill.mail.backends.djrill.DjrillBackend' MANDRILL_API_KEY = os.getenv('MANDRILL_API_KEY') # DATABASE # set this config by setting an environment called DATABASE_URL, # eg: postgres://username:password@ip-addres:port/database-name DATABASES = { 'default': dj_database_url.config( default='postgres://*****:*****@localhost:5432/connectdb' ) } # Now, let's activate our settings MODE = os.getenv('DJANGO_MODE', 'Local') cbs.apply('{}Settings'.format(MODE.title()), globals())
def test_precedence_empty_global_setting(self): g = {} cbs.apply(MySettings, g) self.assertEqual(['test'], g['INSTALLED_APPS'])