Update dtb/settings.py
Browse files- dtb/settings.py +59 -94
dtb/settings.py
CHANGED
@@ -1,36 +1,22 @@
|
|
1 |
-
import logging
|
2 |
import os
|
|
|
3 |
import sys
|
|
|
4 |
|
5 |
import dj_database_url
|
6 |
-
import
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
11 |
BASE_DIR = Path(__file__).resolve().parent.parent
|
12 |
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
dotenv_file = BASE_DIR / ".env"
|
16 |
-
if os.path.isfile(dotenv_file):
|
17 |
-
dotenv.load_dotenv(dotenv_file)
|
18 |
-
|
19 |
-
|
20 |
-
# SECURITY WARNING: keep the secret key used in production secret!
|
21 |
-
SECRET_KEY = os.getenv(
|
22 |
-
"DJANGO_SECRET_KEY",
|
23 |
-
'x%#3&%giwv8f0+%r946en7z&d@9*rc$sl0qoql56xr%bh^w2mj',
|
24 |
-
)
|
25 |
-
|
26 |
-
if os.environ.get('DJANGO_DEBUG', default=False) in ['True', 'true', '1', True]:
|
27 |
-
DEBUG = True
|
28 |
-
else:
|
29 |
-
DEBUG = False
|
30 |
-
|
31 |
-
ALLOWED_HOSTS = ["*",] # since Telegram uses a lot of IPs for webhooks
|
32 |
|
|
|
33 |
|
|
|
34 |
INSTALLED_APPS = [
|
35 |
'django.contrib.admin',
|
36 |
'django.contrib.auth',
|
@@ -38,41 +24,24 @@ INSTALLED_APPS = [
|
|
38 |
'django.contrib.sessions',
|
39 |
'django.contrib.messages',
|
40 |
'django.contrib.staticfiles',
|
41 |
-
|
42 |
-
# 3rd party apps
|
43 |
'django_celery_beat',
|
44 |
'debug_toolbar',
|
45 |
-
|
46 |
-
|
47 |
-
'users.apps.UsersConfig',
|
48 |
]
|
49 |
|
50 |
MIDDLEWARE = [
|
51 |
'django.middleware.security.SecurityMiddleware',
|
|
|
52 |
'django.contrib.sessions.middleware.SessionMiddleware',
|
|
|
53 |
'django.middleware.csrf.CsrfViewMiddleware',
|
54 |
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
55 |
'django.contrib.messages.middleware.MessageMiddleware',
|
56 |
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
57 |
-
|
58 |
-
'whitenoise.middleware.WhiteNoiseMiddleware',
|
59 |
-
'corsheaders.middleware.CorsMiddleware',
|
60 |
'debug_toolbar.middleware.DebugToolbarMiddleware',
|
61 |
-
|
62 |
-
'django.middleware.common.CommonMiddleware',
|
63 |
-
]
|
64 |
-
|
65 |
-
INTERNAL_IPS = [
|
66 |
-
# ...
|
67 |
-
'127.0.0.1',
|
68 |
-
# ...
|
69 |
]
|
70 |
|
71 |
-
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
72 |
-
|
73 |
-
CORS_ORIGIN_ALLOW_ALL = True
|
74 |
-
CORS_ALLOW_CREDENTIALS = True
|
75 |
-
|
76 |
ROOT_URLCONF = 'dtb.urls'
|
77 |
|
78 |
TEMPLATES = [
|
@@ -92,19 +61,16 @@ TEMPLATES = [
|
|
92 |
]
|
93 |
|
94 |
WSGI_APPLICATION = 'dtb.wsgi.application'
|
95 |
-
ASGI_APPLICATION = 'dtb.asgi.application'
|
96 |
-
|
97 |
|
98 |
# Database
|
99 |
-
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
|
100 |
-
|
101 |
DATABASES = {
|
102 |
-
'default': dj_database_url.config(
|
|
|
|
|
|
|
103 |
}
|
104 |
|
105 |
# Password validation
|
106 |
-
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
|
107 |
-
|
108 |
AUTH_PASSWORD_VALIDATORS = [
|
109 |
{
|
110 |
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
|
@@ -120,64 +86,63 @@ AUTH_PASSWORD_VALIDATORS = [
|
|
120 |
},
|
121 |
]
|
122 |
|
123 |
-
|
124 |
# Internationalization
|
125 |
-
# https://docs.djangoproject.com/en/3.0/topics/i18n/
|
126 |
-
|
127 |
LANGUAGE_CODE = 'en-us'
|
128 |
TIME_ZONE = 'UTC'
|
129 |
USE_I18N = True
|
130 |
USE_L10N = True
|
131 |
USE_TZ = True
|
132 |
|
133 |
-
|
134 |
# Static files (CSS, JavaScript, Images)
|
135 |
-
|
|
|
136 |
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
|
137 |
|
138 |
-
|
139 |
-
|
140 |
-
|
|
|
|
|
|
|
141 |
|
|
|
|
|
142 |
|
143 |
-
#
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
CELERY_RESULT_BACKEND = REDIS_URL
|
148 |
-
CELERY_ACCEPT_CONTENT = ['application/json']
|
149 |
CELERY_TASK_SERIALIZER = 'json'
|
150 |
-
CELERY_RESULT_SERIALIZER = 'json'
|
151 |
-
CELERY_TIMEZONE = TIME_ZONE
|
152 |
-
CELERY_TASK_DEFAULT_QUEUE = 'default'
|
153 |
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
|
155 |
-
#
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
# Remove all token validation from settings.py
|
161 |
-
# (Validation will happen in tgbot/main.py instead)
|
162 |
-
|
163 |
-
TELEGRAM_LOGS_CHAT_ID = os.getenv("TELEGRAM_LOGS_CHAT_ID", default=None)
|
164 |
|
165 |
-
#
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
170 |
|
171 |
-
#
|
172 |
-
|
173 |
-
# integrations=[
|
174 |
-
# DjangoIntegration(),
|
175 |
-
# CeleryIntegration(),
|
176 |
-
# RedisIntegration(),
|
177 |
-
# ],
|
178 |
-
# traces_sample_rate=0.1,
|
179 |
|
180 |
-
#
|
181 |
-
|
182 |
-
# send_default_pii=True
|
183 |
-
# )
|
|
|
|
|
1 |
import os
|
2 |
+
import logging
|
3 |
import sys
|
4 |
+
from pathlib import Path
|
5 |
|
6 |
import dj_database_url
|
7 |
+
from django.core.exceptions import ImproperlyConfigured
|
8 |
|
9 |
+
# Build paths inside the project like this: BASE_DIR / 'subdir'
|
|
|
|
|
10 |
BASE_DIR = Path(__file__).resolve().parent.parent
|
11 |
|
12 |
+
# Security settings
|
13 |
+
SECRET_KEY = os.getenv('DJANGO_SECRET_KEY', 'django-insecure-development-key')
|
14 |
|
15 |
+
DEBUG = os.getenv('DEBUG', 'False').lower() == 'true'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
ALLOWED_HOSTS = ['*']
|
18 |
|
19 |
+
# Application definition
|
20 |
INSTALLED_APPS = [
|
21 |
'django.contrib.admin',
|
22 |
'django.contrib.auth',
|
|
|
24 |
'django.contrib.sessions',
|
25 |
'django.contrib.messages',
|
26 |
'django.contrib.staticfiles',
|
|
|
|
|
27 |
'django_celery_beat',
|
28 |
'debug_toolbar',
|
29 |
+
'users',
|
30 |
+
'tgbot',
|
|
|
31 |
]
|
32 |
|
33 |
MIDDLEWARE = [
|
34 |
'django.middleware.security.SecurityMiddleware',
|
35 |
+
'whitenoise.middleware.WhiteNoiseMiddleware',
|
36 |
'django.contrib.sessions.middleware.SessionMiddleware',
|
37 |
+
'django.middleware.common.CommonMiddleware',
|
38 |
'django.middleware.csrf.CsrfViewMiddleware',
|
39 |
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
40 |
'django.contrib.messages.middleware.MessageMiddleware',
|
41 |
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
|
|
|
|
|
|
42 |
'debug_toolbar.middleware.DebugToolbarMiddleware',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
]
|
44 |
|
|
|
|
|
|
|
|
|
|
|
45 |
ROOT_URLCONF = 'dtb.urls'
|
46 |
|
47 |
TEMPLATES = [
|
|
|
61 |
]
|
62 |
|
63 |
WSGI_APPLICATION = 'dtb.wsgi.application'
|
|
|
|
|
64 |
|
65 |
# Database
|
|
|
|
|
66 |
DATABASES = {
|
67 |
+
'default': dj_database_url.config(
|
68 |
+
default=os.getenv('DATABASE_URL', 'sqlite:////data/db.sqlite3'),
|
69 |
+
conn_max_age=600
|
70 |
+
)
|
71 |
}
|
72 |
|
73 |
# Password validation
|
|
|
|
|
74 |
AUTH_PASSWORD_VALIDATORS = [
|
75 |
{
|
76 |
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
|
|
|
86 |
},
|
87 |
]
|
88 |
|
|
|
89 |
# Internationalization
|
|
|
|
|
90 |
LANGUAGE_CODE = 'en-us'
|
91 |
TIME_ZONE = 'UTC'
|
92 |
USE_I18N = True
|
93 |
USE_L10N = True
|
94 |
USE_TZ = True
|
95 |
|
|
|
96 |
# Static files (CSS, JavaScript, Images)
|
97 |
+
STATIC_URL = '/static/'
|
98 |
+
STATIC_ROOT = '/static'
|
99 |
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
|
100 |
|
101 |
+
# Telegram configuration
|
102 |
+
def get_telegram_token():
|
103 |
+
token = os.getenv("TELEGRAM_TOKEN")
|
104 |
+
if not token:
|
105 |
+
raise ImproperlyConfigured("TELEGRAM_TOKEN must be set in environment")
|
106 |
+
return token
|
107 |
|
108 |
+
TELEGRAM_TOKEN = get_telegram_token()
|
109 |
+
TELEGRAM_LOGS_CHAT_ID = os.getenv("TELEGRAM_LOGS_CHAT_ID")
|
110 |
|
111 |
+
# Celery configuration (optional)
|
112 |
+
CELERY_BROKER_URL = os.getenv('REDIS_URL', 'redis://localhost:6379/0')
|
113 |
+
CELERY_RESULT_BACKEND = os.getenv('REDIS_URL', 'redis://localhost:6379/0')
|
114 |
+
CELERY_ACCEPT_CONTENT = ['json']
|
|
|
|
|
115 |
CELERY_TASK_SERIALIZER = 'json'
|
|
|
|
|
|
|
116 |
|
117 |
+
# Security headers for production
|
118 |
+
if not DEBUG:
|
119 |
+
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
|
120 |
+
SECURE_SSL_REDIRECT = True
|
121 |
+
SESSION_COOKIE_SECURE = True
|
122 |
+
CSRF_COOKIE_SECURE = True
|
123 |
|
124 |
+
# Internal IPs for debug toolbar
|
125 |
+
INTERNAL_IPS = [
|
126 |
+
'127.0.0.1',
|
127 |
+
]
|
|
|
|
|
|
|
|
|
|
|
128 |
|
129 |
+
# Logging configuration
|
130 |
+
LOGGING = {
|
131 |
+
'version': 1,
|
132 |
+
'disable_existing_loggers': False,
|
133 |
+
'handlers': {
|
134 |
+
'console': {
|
135 |
+
'class': 'logging.StreamHandler',
|
136 |
+
},
|
137 |
+
},
|
138 |
+
'root': {
|
139 |
+
'handlers': ['console'],
|
140 |
+
'level': 'INFO',
|
141 |
+
},
|
142 |
+
}
|
143 |
|
144 |
+
# Custom user model
|
145 |
+
AUTH_USER_MODEL = 'users.User'
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
|
147 |
+
# Default primary key field type
|
148 |
+
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
|
|
|