Skip to content

Commit

Permalink
[Setting] Database supports MySQL (#57)
Browse files Browse the repository at this point in the history
* separate settings files into debug and deploy

* django settings sonar exclustions

* mysql setting
  • Loading branch information
deploy-soon authored Nov 24, 2019
1 parent 64a321f commit cb06ff0
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 8 deletions.
1 change: 1 addition & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,5 @@ __pycache__/
local_settings.py

.env
.config/
db.sqlite3
5 changes: 5 additions & 0 deletions backend/triplannet/.config/settings_base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"django": {
"secret_key": "swpp2019-team6"
}
}
8 changes: 8 additions & 0 deletions backend/triplannet/.config/settings_debug.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"django": {
"allowed_hosts": [
"localhost",
"127.0.0.1"
]
}
}
16 changes: 16 additions & 0 deletions backend/triplannet/.config/settings_deploy.json
Original file line number Diff line number Diff line change
@@ -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
}
}
}
2 changes: 1 addition & 1 deletion backend/triplannet/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion backend/triplannet/sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -118,7 +128,6 @@
},
]

WSGI_APPLICATION = 'triplannet.wsgi.application'


# Database
Expand All @@ -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'),
}
}

Expand Down
17 changes: 17 additions & 0 deletions backend/triplannet/triplannet/settings/debug.py
Original file line number Diff line number Diff line change
@@ -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'),
}
}

26 changes: 26 additions & 0 deletions backend/triplannet/triplannet/settings/deploy.py
Original file line number Diff line number Diff line change
@@ -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'"
}
}
}

Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
16 changes: 16 additions & 0 deletions backend/triplannet/triplannet/wsgi/deploy.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit cb06ff0

Please sign in to comment.