diff --git a/backend/.gitignore b/backend/.gitignore index 853f25a..2e188b9 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -86,4 +86,5 @@ __pycache__/ local_settings.py .env +.config/ db.sqlite3 diff --git a/backend/triplannet/.config/settings_base.json b/backend/triplannet/.config/settings_base.json new file mode 100644 index 0000000..14ebc7e --- /dev/null +++ b/backend/triplannet/.config/settings_base.json @@ -0,0 +1,5 @@ +{ + "django": { + "secret_key": "swpp2019-team6" + } +} diff --git a/backend/triplannet/.config/settings_debug.json b/backend/triplannet/.config/settings_debug.json new file mode 100644 index 0000000..71db950 --- /dev/null +++ b/backend/triplannet/.config/settings_debug.json @@ -0,0 +1,8 @@ +{ + "django": { + "allowed_hosts": [ + "localhost", + "127.0.0.1" + ] + } +} diff --git a/backend/triplannet/.config/settings_deploy.json b/backend/triplannet/.config/settings_deploy.json new file mode 100644 index 0000000..350ce7d --- /dev/null +++ b/backend/triplannet/.config/settings_deploy.json @@ -0,0 +1,16 @@ +{ + "django": { + "allowed_hosts": [ + "localhost", + "127.0.0.1" + ], + "secret_key": "SECRET_KEY", + "database": { + "NAME": "SCHEMA", + "USER": "USER", + "PASSWORD": "PASSWORD", + "HOST": "HOST", + "PORT": 3306 + } + } +} diff --git a/backend/triplannet/manage.py b/backend/triplannet/manage.py index 80299c5..17d8806 100755 --- a/backend/triplannet/manage.py +++ b/backend/triplannet/manage.py @@ -5,7 +5,7 @@ def main(): - os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'triplannet.settings') + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'triplannet.settings.debug') try: from django.core.management import execute_from_command_line except ImportError as exc: diff --git a/backend/triplannet/sonar-project.properties b/backend/triplannet/sonar-project.properties index 619e648..8f96c43 100644 --- a/backend/triplannet/sonar-project.properties +++ b/backend/triplannet/sonar-project.properties @@ -9,6 +9,6 @@ sonar.sources=. #sonar.python.pylint.reportPath=pylint-report.txt sonar.test=./triplannet -sonar.exclusions=**/migrations/* +sonar.exclusions=**/migrations/*,./.config/*,./triplannet/settings/*,./triplannet/wsgi/* sonar.test.inclusions=**/tests.py sonar.python.coverage.reportPath=./coverage.xml diff --git a/backend/triplannet/triplannet/settings/__init__.py b/backend/triplannet/triplannet/settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/triplannet/triplannet/settings.py b/backend/triplannet/triplannet/settings/base.py similarity index 88% rename from backend/triplannet/triplannet/settings.py rename to backend/triplannet/triplannet/settings/base.py index f21635b..c73f5dc 100644 --- a/backend/triplannet/triplannet/settings.py +++ b/backend/triplannet/triplannet/settings/base.py @@ -9,18 +9,28 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/2.2/ref/settings/ """ - import os +import json +from os.path import join as pjoin +from os.path import dirname as pdir from datetime import timedelta # Build paths inside the project like this: os.path.join(BASE_DIR, ...) -BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) +BASE_DIR = pdir(pdir(pdir(os.path.abspath(__file__)))) + +# load config files +CONFIG_DIR = pjoin(BASE_DIR, '.config') +CONFIG_BASE_FILE = pjoin(CONFIG_DIR, 'settings_base.json') +CONFIG_DEBUG_FILE = pjoin(CONFIG_DIR, 'settings_debug.json') +CONFIG_DEPLOY_FILE = pjoin(CONFIG_DIR, 'settings_deploy.json') + +config_base = json.loads(open(CONFIG_BASE_FILE).read()) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'q^mnq)5qnon98ghvps#dbuj!-bvgip(7@9y=%d#-p%^3!z4rbs' +SECRET_KEY = config_base['django']['secret_key'] # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True @@ -118,7 +128,6 @@ }, ] -WSGI_APPLICATION = 'triplannet.wsgi.application' # Database @@ -127,7 +136,7 @@ DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + 'NAME': pjoin(BASE_DIR, 'db.sqlite3'), } } diff --git a/backend/triplannet/triplannet/settings/debug.py b/backend/triplannet/triplannet/settings/debug.py new file mode 100644 index 0000000..071b73e --- /dev/null +++ b/backend/triplannet/triplannet/settings/debug.py @@ -0,0 +1,17 @@ +from .base import * + +config_debug = json.loads(open(CONFIG_DEBUG_FILE).read()) + +DEBUG = True + +ALLOWED_HOSTS = config_debug['django']['allowed_hosts'] + +WSGI_APPLICATION = 'triplannet.wsgi.debug.application' + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': pjoin(BASE_DIR, 'db.sqlite3'), + } +} + diff --git a/backend/triplannet/triplannet/settings/deploy.py b/backend/triplannet/triplannet/settings/deploy.py new file mode 100644 index 0000000..ff231aa --- /dev/null +++ b/backend/triplannet/triplannet/settings/deploy.py @@ -0,0 +1,26 @@ +from .base import * + +config_deploy = json.loads(open(CONFIG_DEPLOY_FILE).read()) + +DEBUG = False + +ALLOWED_HOSTS = config_deploy['django']['allowed_hosts'] + +WSGI_APPLICATION = 'triplannet.wsgi.deploy.application' + +SECRET_KEY = config_deploy['django']['secret_key'] + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.mysql', + 'NAME': config_deploy['django']['database']['NAME'], + 'USER': config_deploy['django']['database']['USER'], + 'PASSWORD': config_deploy['django']['database']['PASSWORD'], + 'HOST': config_deploy['django']['database']['HOST'], + 'PORT': config_deploy['django']['database']['PORT'], + 'OPTIONS': { + 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'" + } + } +} + diff --git a/backend/triplannet/triplannet/wsgi/__init__.py b/backend/triplannet/triplannet/wsgi/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/triplannet/triplannet/wsgi.py b/backend/triplannet/triplannet/wsgi/debug.py similarity index 96% rename from backend/triplannet/triplannet/wsgi.py rename to backend/triplannet/triplannet/wsgi/debug.py index 77214c4..51f23e0 100644 --- a/backend/triplannet/triplannet/wsgi.py +++ b/backend/triplannet/triplannet/wsgi/debug.py @@ -11,6 +11,6 @@ from django.core.wsgi import get_wsgi_application -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'triplannet.settings') +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'triplannet.settings.debug') application = get_wsgi_application() diff --git a/backend/triplannet/triplannet/wsgi/deploy.py b/backend/triplannet/triplannet/wsgi/deploy.py new file mode 100644 index 0000000..89b9f39 --- /dev/null +++ b/backend/triplannet/triplannet/wsgi/deploy.py @@ -0,0 +1,16 @@ +""" +WSGI config for triplannet project. + +It exposes the WSGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'triplannet.settings.deploy') + +application = get_wsgi_application()