-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy path__init__.py
280 lines (208 loc) · 8.55 KB
/
__init__.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
"""
Django settings for housewatch project.
Generated by 'django-admin startproject' using Django 4.1.1.
For more information on this file, see
https://docs.djangoproject.com/en/4.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.1/ref/settings/
"""
import os
import sys
from datetime import timedelta
from pathlib import Path
from typing import Any, Callable, Optional
import dj_database_url
from django.core.exceptions import ImproperlyConfigured
from kombu import Exchange, Queue
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration
from housewatch.utils import str_to_bool
sentry_sdk.init(
dsn="https://[email protected]/4505874503237633",
integrations=[DjangoIntegration()],
# If you wish to associate users to errors (assuming you are using
# django.contrib.auth) you may enable sending PII data.
send_default_pii=True,
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for performance monitoring.
# We recommend adjusting this value in production.
traces_sample_rate=1.0,
# Set profiles_sample_rate to 1.0 to profile 100%
# of sampled transactions.
# We recommend adjusting this value in production.
profiles_sample_rate=1.0,
)
# TODO: Figure out why things dont work on cloud without debug
DEBUG = os.getenv("DEBUG", "false").lower() in ["true", "1"]
if "mypy" in sys.modules:
DEBUG = True
def get_from_env(key: str, default: Any = None, *, optional: bool = False, type_cast: Optional[Callable] = None) -> Any:
value = os.getenv(key)
if value is None or value == "":
if optional:
return None
if default is not None:
return default
else:
if not DEBUG:
raise ImproperlyConfigured(f'The environment variable "{key}" is required to run HouseWatch!')
if type_cast is not None:
return type_cast(value)
return value
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
TEST = False
if "pytest" in sys.modules or "mypy" in sys.modules:
TEST = True
# this is ok for now as we don't have auth, crypto, or anything that uses the secret key
SECRET_KEY = get_from_env("SECRET_KEY", "not-so-secret")
if DEBUG:
print("WARNING: Running debug mode!")
is_development = DEBUG and not TEST
SECURE_SSL_REDIRECT = False
if not DEBUG and not TEST:
USE_X_FORWARDED_HOST = True
USE_X_FORWARDED_PORT = True
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
CSRF_TRUSTED_ORIGINS = ["https://*.posthog.dev", "https://*.posthog.com"]
APPEND_SLASH = False
# Application definition
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"housewatch.apps.HouseWatchConfig",
"rest_framework",
"corsheaders",
]
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"corsheaders.middleware.CorsMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]
ROOT_URLCONF = "housewatch.urls"
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]
ALLOWED_HOSTS = ["*"]
CORS_ORIGIN_ALLOW_ALL = True
WSGI_APPLICATION = "housewatch.wsgi.application"
DATABASE_URL = get_from_env("DATABASE_URL", "")
if DATABASE_URL:
DATABASES = {"default": dj_database_url.config(default=DATABASE_URL, conn_max_age=600)}
elif not DEBUG:
raise ImproperlyConfigured("DATABASE_URL environment variable not set!")
# Password validation
# https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
{
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
},
{
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
},
{
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
},
]
# Internationalization
# https://docs.djangoproject.com/en/4.1/topics/i18n/
LANGUAGE_CODE = "en-us"
TIME_ZONE = "UTC"
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.1/howto/static-files/
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
# Django REST Framework
# https://github.com/encode/django-rest-framework/
REST_FRAMEWORK = {
"DEFAULT_AUTHENTICATION_CLASSES": [
# "rest_framework.authentication.BasicAuthentication",
# "rest_framework.authentication.SessionAuthentication",
],
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination",
"DEFAULT_PERMISSION_CLASSES": [],
"DEFAULT_RENDERER_CLASSES": ["rest_framework.renderers.JSONRenderer"],
"PAGE_SIZE": 100,
"EXCEPTION_HANDLER": "exceptions_hog.exception_handler",
"TEST_REQUEST_DEFAULT_FORMAT": "json",
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.openapi.AutoSchema",
}
# See https://docs.djangoproject.com/en/3.2/ref/settings/#std:setting-DATABASE-DISABLE_SERVER_SIDE_CURSORS
DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
# DRF Exceptions Hog
# https://github.com/PostHog/drf-exceptions-hog#readme
# EXCEPTIONS_HOG = {
# "EXCEPTION_REPORTING": "housewatch.utils.exception_reporter",
# }
EVENT_USAGE_CACHING_TTL = get_from_env("EVENT_USAGE_CACHING_TTL", 12 * 60 * 60, type_cast=int)
if TEST or DEBUG:
REDIS_URL = get_from_env("REDIS_URL", "redis://localhost:6379")
RABBITMQ_URL = get_from_env("RABBITMQ_URL", "amqp://localhost:5672")
else:
REDIS_URL = get_from_env("REDIS_URL")
RABBITMQ_URL = get_from_env("RABBITMQ_URL")
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": REDIS_URL,
"OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient"},
"KEY_PREFIX": "housewatch",
}
}
# Only listen to the default queue "celery", unless overridden via the CLI
CELERY_QUEUES = (Queue("celery", Exchange("celery"), "celery"),)
CELERY_DEFAULT_QUEUE = "celery"
CELERY_IMPORTS = [] # type: ignore
CELERY_BROKER_URL = RABBITMQ_URL # celery connects to rabbitmq
CELERY_BEAT_MAX_LOOP_INTERVAL = 30 # sleep max 30sec before checking for new periodic events
CELERY_RESULT_BACKEND = REDIS_URL # stores results for lookup when processing
CELERY_IGNORE_RESULT = True # only applies to delay(), must do @shared_task(ignore_result=True) for apply_async
CELERY_RESULT_EXPIRES = timedelta(days=4) # expire tasks after 4 days instead of the default 1
REDBEAT_LOCK_TIMEOUT = 45 # keep distributed beat lock for 45sec
if TEST:
import celery
celery.current_app.conf.task_always_eager = True
celery.current_app.conf.task_eager_propagates = True
POSTHOG_PROJECT_API_KEY = get_from_env("POSTHOG_PROJECT_API_KEY", "123456789")
# ClickHouse
CLICKHOUSE_HOST = get_from_env("CLICKHOUSE_HOST", "localhost")
CLICKHOUSE_VERIFY = str_to_bool(get_from_env("CLICKHOUSE_VERIFY", True))
CLICKHOUSE_CA = get_from_env("CLICKHOUSE_CA", optional=True)
CLICKHOUSE_SECURE = str_to_bool(get_from_env("CLICKHOUSE_SECURE", True))
CLICKHOUSE_DATABASE = get_from_env("CLICKHOUSE_DATABASE", "default")
CLICKHOUSE_USER = get_from_env("CLICKHOUSE_USER", "default")
CLICKHOUSE_PASSWORD = get_from_env("CLICKHOUSE_PASSWORD", "")
# AWS settings for Backups
AWS_ACCESS_KEY_ID = get_from_env("AWS_ACCESS_KEY_ID", "")
AWS_SECRET_ACCESS_KEY = get_from_env("AWS_SECRET_ACCESS_KEY", "")
AWS_DEFAULT_REGION = get_from_env("AWS_DEFAULT_REGION", "us-east-1")