-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.py
256 lines (222 loc) · 9.14 KB
/
config.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import logging
import os
from dotenv import load_dotenv
class EnvironmentConfig:
"""Base class for configuration."""
""" Most settings can be defined through environment variables. """
# Load configuration from file
load_dotenv(
os.path.normpath(
os.path.join(os.path.dirname(__file__), "..", "tasking-manager.env")
)
)
# The base url the application is reachable
APP_BASE_URL = os.getenv("TM_APP_BASE_URL", "http://127.0.0.1:8000/").rstrip("/")
API_VERSION = os.getenv("TM_APP_API_VERSION", "v2")
ORG_CODE = os.getenv("TM_ORG_CODE", "HOT")
ORG_NAME = os.getenv("TM_ORG_NAME", "Humanitarian OpenStreetMap Team")
ORG_LOGO = os.getenv(
"TM_ORG_LOGO",
"https://cdn.hotosm.org/tasking-manager/uploads/1588741335578_hot-logo.png",
)
ENVIRONMENT = os.getenv("TM_ENVIRONMENT", "")
# The default tag used in the OSM changeset comment
DEFAULT_CHANGESET_COMMENT = os.getenv(
"TM_DEFAULT_CHANGESET_COMMENT", "#hot-tm-stage-project"
)
# The address to use as the sender on auto generated emails
EMAIL_FROM_ADDRESS = os.getenv("TM_EMAIL_FROM_ADDRESS", "[email protected]")
# The address to use as the receiver in contact form.
EMAIL_CONTACT_ADDRESS = os.getenv("TM_EMAIL_CONTACT_ADDRESS", "[email protected]")
# A freely definable secret key for connecting the front end with the back end
SECRET_KEY = os.getenv("TM_SECRET", None)
# OSM API, Nomimatim URLs
OSM_SERVER_URL = os.getenv("OSM_SERVER_URL", "https://www.openstreetmap.org")
OSM_NOMINATIM_SERVER_URL = os.getenv(
"OSM_NOMINATIM_SERVER_URL", "https://nominatim.openstreetmap.org"
)
# Database connection
POSTGRES_USER = os.getenv("POSTGRES_USER", "postgres")
POSTGRES_PASSWORD = os.getenv("POSTGRES_PASSWORD", None)
POSTGRES_ENDPOINT = os.getenv("POSTGRES_ENDPOINT", "localhost")
POSTGRES_DB = os.getenv("POSTGRES_DB", "postgres")
POSTGRES_PORT = os.getenv("POSTGRES_PORT", "5432")
# Assamble the database uri
if os.getenv("TM_DB", False):
SQLALCHEMY_DATABASE_URI = os.getenv("TM_DB", None)
elif os.getenv("DB_CONNECT_PARAM_JSON", False):
"""
This section reads JSON formatted Database connection parameters passed
from AWS Secrets Manager with the ENVVAR key `DB_CONNECT_PARAM_JSON`
and forms a valid SQLALCHEMY DATABASE URI
"""
import json
_params = json.loads(os.getenv("DB_CONNECT_PARAM_JSON", None))
SQLALCHEMY_DATABASE_URI = (
f"postgresql://{_params.get('username')}"
+ f":{_params.get('password')}"
+ f"@{_params.get('host')}"
+ f":{_params.get('port')}"
+ f"/{_params.get('dbname')}"
)
else:
SQLALCHEMY_DATABASE_URI = (
f"postgresql://{POSTGRES_USER}"
+ f":{POSTGRES_PASSWORD}"
+ f"@{POSTGRES_ENDPOINT}:"
+ f"{POSTGRES_PORT}"
+ f"/{POSTGRES_DB}"
)
# Logging settings
LOG_LEVEL = os.getenv("TM_LOG_LEVEL", logging.DEBUG)
LOG_DIR = os.getenv("TM_LOG_DIR", "/home/appuser/logs")
# Mapper Level values represent number of OSM changesets
MAPPER_LEVEL_INTERMEDIATE = int(os.getenv("TM_MAPPER_LEVEL_INTERMEDIATE", 250))
MAPPER_LEVEL_ADVANCED = int(os.getenv("TM_MAPPER_LEVEL_ADVANCED", 500))
# Time to wait until task auto-unlock (e.g. '2h' or '7d' or '30m' or '1h30m')
TASK_AUTOUNLOCK_AFTER = os.getenv("TM_TASK_AUTOUNLOCK_AFTER", "2h")
# Configuration for sending emails
MAIL_SERVER = os.getenv("TM_SMTP_HOST", None)
MAIL_PORT = os.getenv("TM_SMTP_PORT", "587")
MAIL_USE_TLS = bool(int(os.getenv("TM_SMTP_USE_TLS", True)))
MAIL_USE_SSL = bool(int(os.getenv("TM_SMTP_USE_SSL", False)))
MAIL_USERNAME = os.getenv("TM_SMTP_USER", None)
MAIL_PASSWORD = os.getenv("TM_SMTP_PASSWORD", None)
MAIL_DEFAULT_SENDER = os.getenv("TM_EMAIL_FROM_ADDRESS", "[email protected]")
MAIL_DEBUG = True if LOG_LEVEL == "DEBUG" else False
if os.getenv("SMTP_CREDENTIALS", False):
"""
This section reads JSON formatted SMTP connection parameters passed
from AWS Secrets Manager with the ENVVAR key `SMTP_CREDENTIALS`.
"""
import json
_params = json.loads(os.getenv("SMTP_CREDENTIALS", None))
MAIL_SERVER = _params.get("SMTP_HOST", None)
MAIL_PORT = _params.get("SMTP_PORT", "587")
MAIL_USE_TLS = bool(int(_params.get("SMTP_USE_TLS", True)))
MAIL_USE_SSL = bool(int(_params.get("SMTP_USE_SSL", False)))
MAIL_USERNAME = _params.get("SMTP_USER", None)
MAIL_PASSWORD = _params.get("SMTP_PASSWORD", None)
# If disabled project update emails will not be sent.
SEND_PROJECT_EMAIL_UPDATES = bool(os.getenv("TM_SEND_PROJECT_EMAIL_UPDATES", True))
# Languages offered by the Tasking Manager
# Please note that there must be exactly the same number of Codes as languages.
SUPPORTED_LANGUAGES = {
"codes": os.getenv(
"TM_SUPPORTED_LANGUAGES_CODES",
", ".join(
[
"ar",
"cs",
"de",
"el",
"en",
"es",
"fa_IR",
"fr",
"he",
"hu",
"id",
"it",
"ja",
"ko",
"mg",
"ml",
"nl_NL",
"pt",
"pt_BR",
"ru",
"sv",
"sw",
"tl",
"tr",
"uk",
"zh_TW",
]
),
),
"languages": os.getenv(
"TM_SUPPORTED_LANGUAGES",
", ".join(
[
"عربى",
"Čeština",
"Deutsch",
"Ελληνικά",
"English",
"Español",
"فارسی",
"Français",
"עברית",
"Magyar",
"Indonesia",
"Italiano",
"日本語",
"한국어",
"Malagasy",
"Malayalam",
"Nederlands",
"Português",
"Português (Brasil)",
"Русский язык",
"Svenska",
"Kiswahili",
"Filipino (Tagalog)",
"Türkçe",
"Українська",
"繁體中文",
]
),
),
}
# Connection to OSM authentification system
OAUTH_API_URL = "{}/api/0.6/".format(OSM_SERVER_URL)
OAUTH_CLIENT_ID = os.getenv("TM_CLIENT_ID", None)
OAUTH_CLIENT_SECRET = os.getenv("TM_CLIENT_SECRET", None)
OAUTH_SCOPE = os.getenv("TM_SCOPE", "read_prefs write_api")
OAUTH_REDIRECT_URI = os.getenv("TM_REDIRECT_URI", None)
if os.getenv("OAUTH2_APP_CREDENTIALS", False):
"""
This section reads JSON formatted OAuth2 app credentials passed
from AWS Secrets Manager with the ENVVAR key `OAUTH2_APP_CREDENTIALS`.
"""
import json
_params = json.loads(os.getenv("OAUTH2_APP_CREDENTIALS", None))
OAUTH_CLIENT_ID = _params.get("CLIENT_ID", None)
OAUTH_CLIENT_SECRET = _params.get("CLIENT_SECRET", None)
OAUTH_REDIRECT_URI = _params.get("REDIRECT_URI", None)
OAUTH_SCOPE = _params.get("ACCESS_SCOPE", "read_prefs write_api")
# Some more definitions (not overridable)
SQLALCHEMY_ENGINE_OPTIONS = {
"pool_size": 10,
"max_overflow": 10,
}
SEND_FILE_MAX_AGE_DEFAULT = 0
SQLALCHEMY_TRACK_MODIFICATIONS = False
# Image upload Api
IMAGE_UPLOAD_API_KEY = os.getenv("TM_IMAGE_UPLOAD_API_KEY", None)
IMAGE_UPLOAD_API_URL = os.getenv("TM_IMAGE_UPLOAD_API_URL", None)
if os.getenv("IMAGE_UPLOAD_CREDENTIALS", False):
"""
This section reads JSON formatted Image Upload credentials passed
from AWS Secrets Manager with the ENVVAR key `IMAGE_UPLOAD_CREDENTIALS`.
"""
import json
_params = json.loads(os.getenv("IMAGE_UPLOAD_CREDENTIALS"), None)
IMAGE_UPLOAD_API_KEY = _params.get("IMAGE_UPLOAD_API_KEY", None)
IMAGE_UPLOAD_API_URL = _params.get("IMAGE_UPLOAD_API_URL", None)
# Sentry backend DSN
SENTRY_BACKEND_DSN = os.getenv("TM_SENTRY_BACKEND_DSN", None)
# Ohsome Stats Token
OHSOME_STATS_TOKEN = os.getenv("OHSOME_STATS_TOKEN", None)
class TestEnvironmentConfig(EnvironmentConfig):
POSTGRES_TEST_DB = os.getenv("POSTGRES_TEST_DB", None)
ENVIRONMENT = "test"
SQLALCHEMY_DATABASE_URI = (
f"postgresql://{EnvironmentConfig.POSTGRES_USER}"
+ f":{EnvironmentConfig.POSTGRES_PASSWORD}"
+ f"@{EnvironmentConfig.POSTGRES_ENDPOINT}:"
+ f"{EnvironmentConfig.POSTGRES_PORT}"
+ f"/{POSTGRES_TEST_DB}"
)
LOG_LEVEL = "DEBUG"