From a9fbc08848def3ba485d803e70094b273011d9bd Mon Sep 17 00:00:00 2001 From: FedeG Date: Tue, 1 May 2018 01:40:20 -0300 Subject: [PATCH] #7 - Add django-configuration and update settings --- config/requirements.txt | 1 + website/manage.py | 3 +- website/website/settings.py | 249 ++++++++++++++++++++---------------- website/website/wsgi.py | 3 +- 4 files changed, 141 insertions(+), 115 deletions(-) diff --git a/config/requirements.txt b/config/requirements.txt index 50b4688..c9e8a1c 100644 --- a/config/requirements.txt +++ b/config/requirements.txt @@ -3,3 +3,4 @@ django-extensions==2.0.7 gunicorn==19.8.0 psycopg2_binary==2.7.4 django-crispy-forms==1.7.2 +django-configurations==2.0 diff --git a/website/manage.py b/website/manage.py index 15d6595..df4a7d4 100755 --- a/website/manage.py +++ b/website/manage.py @@ -4,8 +4,9 @@ if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "website.settings") + os.environ.setdefault('DJANGO_CONFIGURATION', 'Dev') try: - from django.core.management import execute_from_command_line + from configurations.management import execute_from_command_line except ImportError as exc: raise ImportError( "Couldn't import Django. Are you sure it's installed and " diff --git a/website/website/settings.py b/website/website/settings.py index 2d68229..5353f54 100644 --- a/website/website/settings.py +++ b/website/website/settings.py @@ -11,128 +11,151 @@ """ import os +from configurations import Configuration # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -# Quick-start development settings - unsuitable for production -# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/ - -# SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'svz&bkp-k(zydvn+v9$kqmds=ncl8w8(i-sp^1u280vez=g-zj' - -# SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True - -ALLOWED_HOSTS = ['*'] - - -# Application definition - -INSTALLED_APPS = [ - 'django.contrib.admin', - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.messages', - 'django.contrib.staticfiles', - 'django_extensions', - 'members', - - 'crispy_forms', -] - -MIDDLEWARE = [ - 'django.middleware.security.SecurityMiddleware', - 'django.contrib.sessions.middleware.SessionMiddleware', - 'django.middleware.common.CommonMiddleware', - 'django.middleware.csrf.CsrfViewMiddleware', - 'django.contrib.auth.middleware.AuthenticationMiddleware', - 'django.contrib.messages.middleware.MessageMiddleware', - 'django.middleware.clickjacking.XFrameOptionsMiddleware', -] - -ROOT_URLCONF = 'website.urls' - -TEMPLATES = [ - { - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], - 'APP_DIRS': True, - 'OPTIONS': { - 'context_processors': [ - 'django.template.context_processors.debug', - 'django.template.context_processors.request', - 'django.contrib.auth.context_processors.auth', - 'django.contrib.messages.context_processors.messages', - ], +class Base(Configuration): + """ + Base configuration for django app + """ + # Quick-start development settings - unsuitable for production + # See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/ + + # SECURITY WARNING: keep the secret key used in production secret! + SECRET_KEY = 'svz&bkp-k(zydvn+v9$kqmds=ncl8w8(i-sp^1u280vez=g-zj' + + # SECURITY WARNING: don't run with debug turned on in production! + DEBUG = True + ALLOWED_HOSTS = ['*'] + + # Application definition + + INSTALLED_APPS = [ + 'django.contrib.admin', + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.messages', + 'django.contrib.staticfiles', + 'django_extensions', + 'members', + + 'crispy_forms', + ] + + MIDDLEWARE = [ + 'django.middleware.security.SecurityMiddleware', + 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', + 'django.middleware.csrf.CsrfViewMiddleware', + 'django.contrib.auth.middleware.AuthenticationMiddleware', + 'django.contrib.messages.middleware.MessageMiddleware', + 'django.middleware.clickjacking.XFrameOptionsMiddleware', + ] + + ROOT_URLCONF = 'website.urls' + + TEMPLATES = [ + { + 'BACKEND': 'django.template.backends.django.DjangoTemplates', + 'DIRS': [], + 'APP_DIRS': True, + 'OPTIONS': { + 'context_processors': [ + 'django.template.context_processors.debug', + 'django.template.context_processors.request', + 'django.contrib.auth.context_processors.auth', + 'django.contrib.messages.context_processors.messages', + ], + }, }, - }, -] + ] -WSGI_APPLICATION = 'website.wsgi.application' + WSGI_APPLICATION = 'website.wsgi.application' + # Database + # https://docs.djangoproject.com/en/2.0/ref/settings/#databases -# Database -# https://docs.djangoproject.com/en/2.0/ref/settings/#databases - -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.postgresql', - 'NAME': os.environ.get('DB_NAME', "memberships"), - 'USER': os.environ.get('DB_USER', "postgres"), - 'PASSWORD': os.environ.get('DB_PASS', ""), - 'HOST': os.environ.get('DB_SERVICE', "localhost"), - 'PORT': os.environ.get('DB_PORT', 5432), + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + } } -} - - -# Password validation -# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators - -AUTH_PASSWORD_VALIDATORS = [ - { - 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', - }, - { - 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', - }, -] - - -# Internationalization -# https://docs.djangoproject.com/en/2.0/topics/i18n/ - -LANGUAGE_CODE = 'en-us' - -TIME_ZONE = 'UTC' - -USE_I18N = True - -USE_L10N = True -USE_TZ = True + # Password validation + # https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators -DATE_INPUT_FORMATS = ('%d/%M/%Y','%d-%M-%Y') - -LOCALE_PATHS = (os.path.join(BASE_DIR, "locale"), ) - -# Static files (CSS, JavaScript, Images) -# https://docs.djangoproject.com/en/2.0/howto/static-files/ - -STATIC_URL = '/static/' -STATIC_ROOT = os.path.join(BASE_DIR, '..', 'static') - -STATICFILES_DIRS = [ - os.path.join(BASE_DIR, "static"), -] - -CRISPY_TEMPLATE_PACK = 'bootstrap3' + AUTH_PASSWORD_VALIDATORS = [ + { + 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', + }, + { + 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', + }, + ] + + + # Internationalization + # https://docs.djangoproject.com/en/2.0/topics/i18n/ + + LANGUAGE_CODE = 'en-us' + TIME_ZONE = 'UTC' + USE_I18N = True + USE_L10N = True + USE_TZ = True + DATE_INPUT_FORMATS = ('%d/%M/%Y', '%d-%M-%Y') + LOCALE_PATHS = (os.path.join(BASE_DIR, "locale"),) + + # Static files (CSS, JavaScript, Images) + # https://docs.djangoproject.com/en/2.0/howto/static-files/ + + STATIC_URL = '/static/' + STATIC_ROOT = os.path.join(BASE_DIR, '..', 'static') + + STATICFILES_DIRS = [ + os.path.join(BASE_DIR, "static"), + ] + + CRISPY_TEMPLATE_PACK = 'bootstrap3' + + +class Dev(Base): + """ + Development configuration + """ + pass + + +class Prod(Base): + """ + Production configuration + """ + DEBUG = False + TEMPLATE_DEBUG = False + SECRET_KEY = os.getenv( + 'SECRET_KEY', + '!a44%)(r2!1wp89@ds(tqzpo#f0qgfxomik)a$16v5v@b%)ecu') + ALLOWED_HOSTS = [os.getenv('APP_DOMAIN')] + # Database + # https://docs.djangoproject.com/en/2.0/ref/settings/#databases + + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql', + 'NAME': os.environ.get('POSTGRES_DB', "memberships"), + 'USER': os.environ.get('POSTGRES_USER', "postgres"), + 'PASSWORD': os.environ.get('POSTGRES_PASSWORD', "secret"), + 'HOST': os.environ.get('POSTGRES_HOST', "localhost"), + 'PORT': os.environ.get('POSTGRES_PORT', 5432), + } + } diff --git a/website/website/wsgi.py b/website/website/wsgi.py index c7a9f0f..2d6f94a 100644 --- a/website/website/wsgi.py +++ b/website/website/wsgi.py @@ -9,8 +9,9 @@ import os -from django.core.wsgi import get_wsgi_application +from configurations.wsgi import get_wsgi_application os.environ.setdefault("DJANGO_SETTINGS_MODULE", "website.settings") +os.environ.setdefault('DJANGO_CONFIGURATION', 'Dev') application = get_wsgi_application()