forked from govtrack/govtrack.us-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings_env.py
119 lines (101 loc) · 4.06 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
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)
}
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",
"FACEBOOK_APP_ID", # also used for Facebook widgets
"FACEBOOK_APP_SECRET",
"FACEBOOK_AUTH_SCOPE",
"GOOGLE_APP_ID",
"GOOGLE_APP_SECRET",
"GOOGLE_AUTH_SCOPE",
# For email...
"EMAIL_HOST",
"EMAIL_PORT",
"EMAIL_HOST_USER",
"EMAIL_HOST_PASSWORD",
"EMAIL_USE_TLS",
"EMAIL_BACKEND",
# For us...
"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",
"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)