Skip to content

Commit 7bb8fe6

Browse files
authored
Merge pull request #9 from curious725/08_divide_settings
08 divide settings
2 parents 795afe6 + b7d9efb commit 7bb8fe6

File tree

8 files changed

+180
-4
lines changed

8 files changed

+180
-4
lines changed

Diff for: Vagrantfile

+6-2
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,21 @@ Vagrant.configure("2") do |config|
2929
dev.vm.box = "ubuntu/trusty64"
3030
dev.vm.hostname = "django-dev"
3131

32+
django_settings = config.jsonconfig.get "dev_django_settings"
33+
3234
provisioning(dev, [db_root_password,db_name,db_user,db_password,
33-
test_db_name])
35+
test_db_name, django_settings])
3436

3537
dev.vm.network :forwarded_port, host: 8000, guest: 8000, host_ip: "127.0.0.1"
3638
end
3739

3840
config.vm.define "prod" do |prod|
3941
prod.vm.box = "dummy"
4042

43+
django_settings = config.jsonconfig.get "prod_django_settings"
44+
4145
provisioning(prod, [db_root_password,db_name,db_user,db_password,
42-
test_db_name])
46+
test_db_name,django_settings])
4347

4448
prod.vm.provider "aws" do |aws, override|
4549
aws.security_groups = ["vagrant"]

Diff for: polls/manage.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import sys
44

55
if __name__ == "__main__":
6-
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "polls.settings")
6+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "polls.settings.dev")
77

88
from django.core.management import execute_from_command_line
99

Diff for: polls/polls/settings/__init__.py

Whitespace-only changes.

Diff for: polls/polls/settings/base.py

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
"""
2+
Common settings and globals for project.
3+
4+
Generated by 'django-admin startproject' using Django 1.9.8.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/1.9/topics/settings/
8+
9+
For the full list of settings and their values, see
10+
https://docs.djangoproject.com/en/1.9/ref/settings/
11+
"""
12+
13+
import os
14+
import json
15+
16+
# Normally you should not import ANYTHING from Django directly
17+
# into your settings, but ImproperlyConfigured is an exception.
18+
from django.core.exceptions import ImproperlyConfigured
19+
20+
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
21+
BASE_DIR = os.path.dirname(
22+
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
23+
)
24+
25+
# JSON-based secrets module
26+
with open(os.path.join(BASE_DIR, '..', 'secrets.json')) as f:
27+
secrets = json.loads(f.read())
28+
29+
30+
def get_secret(setting, secrets=secrets):
31+
""" Get the secret variable or return explicit exception."""
32+
try:
33+
return secrets[setting]
34+
except KeyError:
35+
error_msg = "Set the {0} environment variable".format(
36+
setting
37+
)
38+
raise ImproperlyConfigured(error_msg)
39+
40+
41+
# Quick-start development settings - unsuitable for production
42+
# See https://docs.djangoproject.com/en/1.9/howto/deployment/checklist/
43+
44+
# SECURITY WARNING: keep the secret key used in production secret!
45+
SECRET_KEY = get_secret("secret_key")
46+
47+
# SECURITY WARNING: don't run with debug turned on in production!
48+
DEBUG = False
49+
50+
ALLOWED_HOSTS = []
51+
52+
53+
# Application definition
54+
55+
INSTALLED_APPS = [
56+
'django.contrib.admin',
57+
'django.contrib.auth',
58+
'django.contrib.contenttypes',
59+
'django.contrib.sessions',
60+
'django.contrib.messages',
61+
'django.contrib.staticfiles',
62+
'polls_app.apps.PollsAppConfig',
63+
]
64+
65+
MIDDLEWARE_CLASSES = [
66+
'django.middleware.security.SecurityMiddleware',
67+
'django.contrib.sessions.middleware.SessionMiddleware',
68+
'django.middleware.common.CommonMiddleware',
69+
'django.middleware.csrf.CsrfViewMiddleware',
70+
'django.contrib.auth.middleware.AuthenticationMiddleware',
71+
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
72+
'django.contrib.messages.middleware.MessageMiddleware',
73+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
74+
]
75+
76+
ROOT_URLCONF = 'polls.urls'
77+
78+
TEMPLATES = [
79+
{
80+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
81+
'DIRS': [],
82+
'APP_DIRS': True,
83+
'OPTIONS': {
84+
'context_processors': [
85+
'django.template.context_processors.debug',
86+
'django.template.context_processors.request',
87+
'django.contrib.auth.context_processors.auth',
88+
'django.contrib.messages.context_processors.messages',
89+
],
90+
},
91+
},
92+
]
93+
94+
WSGI_APPLICATION = 'polls.wsgi.application'
95+
96+
97+
# Database
98+
# https://docs.djangoproject.com/en/1.9/ref/settings/#databases
99+
100+
DATABASES = {
101+
'default': {
102+
'ENGINE': get_secret("db_engine"),
103+
'NAME': get_secret('db_name'),
104+
'USER': get_secret('db_user'),
105+
'PASSWORD': get_secret('db_password'),
106+
'HOST': get_secret('db_host'),
107+
'PORT': get_secret('db_port'),
108+
'TEST': {
109+
'NAME': get_secret('test_db_name'),
110+
},
111+
}
112+
}
113+
114+
115+
# Password validation
116+
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators
117+
118+
AUTH_PASSWORD_VALIDATORS = [
119+
{
120+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
121+
},
122+
{
123+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
124+
},
125+
{
126+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
127+
},
128+
{
129+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
130+
},
131+
]
132+
133+
134+
# Internationalization
135+
# https://docs.djangoproject.com/en/1.9/topics/i18n/
136+
137+
LANGUAGE_CODE = 'en-us'
138+
139+
TIME_ZONE = 'Europe/Kiev'
140+
141+
USE_I18N = True
142+
143+
USE_L10N = True
144+
145+
USE_TZ = True
146+
147+
148+
# Static files (CSS, JavaScript, Images)
149+
# https://docs.djangoproject.com/en/1.9/howto/static-files/
150+
151+
STATIC_URL = '/static/'

Diff for: polls/polls/settings/dev.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
"""Development settings and globals."""
2+
from .base import *
3+
4+
5+
# DEBUG CONFIGURATION
6+
DEBUG = True
7+
8+
ALLOWED_HOSTS = get_secret("dev_allowed_hosts")

Diff for: polls/polls/settings/prod.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"""Production settings and globals."""
2+
from .base import *
3+
4+
ALLOWED_HOSTS = get_secret("prod_allowed_hosts")

Diff for: polls/polls/wsgi.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111

1212
from django.core.wsgi import get_wsgi_application
1313

14-
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "polls.settings")
14+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "polls.settings.prod")
1515

1616
application = get_wsgi_application()

Diff for: provision.sh

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ DB_NAME=$2
55
DB_USER=$3
66
DB_PASSWORD=$4
77
TEST_DB_NAME=$5
8+
DJANGO_SETTINGS=$6
89

910
#Updating and instaling dependencies
1011
sudo apt-get -y update
@@ -25,6 +26,14 @@ source ~/.bashrc
2526
echo "PATH=$PATH:$HOME/.local/bin/">>~/.bashrc
2627
source ~/.bashrc
2728

29+
# set DJANGO_SETTINGS_MODULE environment variable
30+
echo "export DJANGO_SETTINGS_MODULE=polls.settings.dev">>~/.bashrc
31+
source ~/.bashrc
32+
33+
# update PYTHONPATH
34+
echo "export PYTHONPATH=$PYTHONPATH:/vagrant">>~/.bashrc
35+
source ~/.bashrc
36+
2837
# Python dev packages
2938
dpkg -s build-essential &>/dev/null || {
3039
sudo apt-get install -y build-essential

0 commit comments

Comments
 (0)