-
Notifications
You must be signed in to change notification settings - Fork 74
/
settings_env.py
147 lines (129 loc) · 5.21 KB
/
settings_env.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import os
import os.path
# Normally you should not import ANYTHING from Django directly
# into your settings, but ImproperlyConfigured is an exception.
from django.core.exceptions import ImproperlyConfigured
NOT_SET = object()
def get_env_variable(var_name, default=NOT_SET):
""" Get the environment variable or return exception """
try:
return os.environ[var_name]
except KeyError:
if default is NOT_SET:
error_msg = "Set the {} environment variable".format(var_name)
raise ImproperlyConfigured(error_msg)
else:
return default
def get_env_listvar(var_name, default=NOT_SET):
strvalue = get_env_variable(var_name, default)
return [item.strip() for item in strvalue.split(',')]
def get_env_boolvar(var_name, default=NOT_SET):
strvalue = get_env_variable(var_name, default)
return (strvalue.lower() == 'true')
###############################################################################
ALLOWED_HOSTS = get_env_listvar('ALLOWED_HOSTS', default="localhost,127.0.0.1")
try:
ADMINS = [("Site Owner", get_env_variable('ADMIN_EMAIL'))]
except ImproperlyConfigured:
pass
import dj_database_url
if not os.path.exists("local"): os.makedirs('local') # ensure directory for default sqlite db exists
DEFAULT_DATABASE_URL = 'sqlite:///' + os.path.dirname(__file__) + '/local/database.sqlite'
DATABASE_URL = get_env_variable('DATABASE_URL', DEFAULT_DATABASE_URL)
DATABASES = {
'default': dj_database_url.parse(DATABASE_URL)
}
if DATABASES['default']['ENGINE'] == 'django.db.backends.mysql':
DATABASES['default']['OPTIONS']['charset'] = 'utf8mb4' # MySQL's true Unicode character set
import django_cache_url
CACHE_URL = get_env_variable('CACHE_URL', default="locmem://opendataiscool")
CACHES = {
'default': django_cache_url.parse(CACHE_URL)
}
import dj_haystack_url
HAYSTACK_CONNECTIONS = {
'default': { # required but not used
'ENGINE': 'haystack.backends.simple_backend.SimpleEngine',
},
'person': dj_haystack_url.parse(get_env_variable('HAYSTACK_PERSON_CONNECTION', default='xapian:local/xapian_index_person')),
'bill': dj_haystack_url.parse(get_env_variable('HAYSTACK_BILL_CONNECTION', default='xapian:local/xapian_index_bill')),
}
from django.utils.crypto import get_random_string
default_secret_key = get_random_string(50, 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)')
SECRET_KEY = get_env_variable('SECRET_KEY', default=default_secret_key)
RSS_CAMPAIGN_QUERYSTRING = get_env_variable('RSS_CAMPAIGN_QUERYSTRING', default="?utm_campaign=govtrack_feed&utm_source=govtrack/feed&utm_medium=rss")
# Copy some environment variables into the Django settings object.
copy_env_vars = [
# For accounts logic.
"RECAPTCHA_SITE_KEY",
"RECAPTCHA_SECRET_KEY",
"TWITTER_OAUTH_TOKEN", # also for automated tweets and used to update @GovTrack/Members-of-Congress twitter list
"TWITTER_OAUTH_TOKEN_SECRET",
"GOOGLE_APP_ID",
"GOOGLE_APP_SECRET",
"GOOGLE_AUTH_SCOPE",
"GOOGLE_API_KEY",
# For email (if one SMTP backend)...
"EMAIL_HOST",
"EMAIL_PORT",
"EMAIL_HOST_USER",
"EMAIL_HOST_PASSWORD",
"EMAIL_USE_TLS",
"EMAIL_BACKEND",
"VOTERAMA_EMAIL_TO",
# Various other accounts we have.
"GOOGLE_ANALYTICS_KEY",
"TWITTER_ACCESS_TOKEN", # for automated tweets and to update @GovTrack/Members-of-Congress twitter list
"TWITTER_ACCESS_TOKEN_SECRET",
"PAYPAL_CLIENT_MODE",
"PAYPAL_CLIENT_ID",
"PAYPAL_CLIENT_SECRET",
"MAPBOX_ACCESS_TOKEN",
"MAPBOX_MAP_STYLE",
"MAPBOX_MAP_ID",
"DISTRICT_BBOXES_FILE",
"COMMUNITY_FORUM_URL",
"COMMUNITY_FORUM_SSO_KEY",
"PROPUBLICA_CONGRESS_API_KEY",
"DRAFTABLE_ACCOUNT_ID",
"DRAFTABLE_AUTH_TOKEN",
"MASTODON_GOVTRACK_PUSH_BOT_ACCESS_TOKEN",
# Data paths.
"CONGRESS_DATA_PATH",
"CONGRESS_PROJECT_PATH",
"MISCONDUCT_DATABASE_PATH",
"PRONUNCIATION_DATABASE_PATH",
"SCORECARDS_DATABASE_PATH",
]
for var in copy_env_vars:
val = get_env_variable(var, default='')
if val != "":
if var == "EMAIL_PORT": val = int(val)
if var == "EMAIL_USE_TLS": val = (val.lower()=="true")
locals()[var] = val
# The hide-the-ads payment requires Paypal integration:
if locals().get("PAYPAL_CLIENT_ID"):
import paypalrestsdk
paypalrestsdk.configure(mode=PAYPAL_CLIENT_MODE, client_id=PAYPAL_CLIENT_ID, client_secret=PAYPAL_CLIENT_SECRET)
# Multiple email backends?
from django.core.mail.backends.smtp import EmailBackend as SMTPEmailBackend
EMAIL_BACKENDS = { }
i = 1
while "EMAIL_{}_HOST".format(i) in os.environ:
backend_class = SMTPEmailBackend
backend_args = {
"host": get_env_variable("EMAIL_{}_HOST".format(i)),
"port": int(get_env_variable("EMAIL_{}_PORT".format(i))),
"username": get_env_variable("EMAIL_{}_HOST_USER".format(i)),
"password": get_env_variable("EMAIL_{}_HOST_PASSWORD".format(i)),
"use_tls": get_env_boolvar("EMAIL_{}_USE_TLS".format(i)),
}
def make_backend(backend_class, backend_args):
def create_backend(*args, **kwargs):
return backend_class(
*args,
**backend_args,
**kwargs)
return create_backend
EMAIL_BACKENDS[str(i)] = make_backend(backend_class, backend_args)
i += 1