|
1 | 1 | import contextlib |
2 | 2 | import socket |
| 3 | +from pathlib import Path |
3 | 4 |
|
4 | | -import environs |
5 | | - |
6 | | -env = environs.Env() |
| 5 | +from django_envtools import Env |
7 | 6 |
|
8 | 7 | """ |
9 | 8 | Django settings for config project. |
10 | 9 |
|
11 | 10 | For more information on this file, see |
12 | | -https://docs.djangoproject.com/en/4.2/topics/settings/ |
| 11 | +https://docs.djangoproject.com/en/dev/topics/settings/ |
13 | 12 |
|
14 | 13 | For the full list of settings and their values, see |
15 | | -https://docs.djangoproject.com/en/4.2/ref/settings/ |
| 14 | +https://docs.djangoproject.com/en/dev/ref/settings/ |
16 | 15 | """ |
17 | 16 |
|
18 | | -BASE_DIR = environs.Path(__file__).parent.parent.parent # type: ignore |
| 17 | +BASE_DIR = Path(__file__).parent.parent.parent # type: ignore |
| 18 | + |
| 19 | +env = Env() |
19 | 20 |
|
20 | 21 | READ_DOT_ENV_FILE = env.bool("READ_DOT_ENV_FILE", default=True) |
21 | 22 |
|
22 | 23 | if READ_DOT_ENV_FILE is True: |
23 | 24 | env.read_env(str(BASE_DIR.joinpath(".env"))) |
24 | 25 |
|
25 | 26 | # Quick-start development settings - unsuitable for production |
26 | | -# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/ |
| 27 | +# See https://docs.djangoproject.com/en/dev/howto/deployment/checklist/ |
27 | 28 |
|
28 | 29 | # SECURITY WARNING: keep the secret key used in production secret! |
29 | | -SECRET_KEY = env("SECRET_KEY") |
| 30 | +SECRET_KEY = env.str( |
| 31 | + "SECRET_KEY", |
| 32 | + help_text="Django's secret key, see" |
| 33 | + "https://docs.djangoproject.com/en/dev/ref/settings/#secret-key for more information", |
| 34 | + initial_func="django.core.management.utils.get_random_secret_key", |
| 35 | +) |
30 | 36 |
|
31 | 37 | # SECURITY WARNING: don't run with debug turned on in production! |
32 | | -DEBUG = env.bool("DEBUG", default=False) |
| 38 | +DEBUG = env.bool("DEBUG", default=False, initial="on", help_text="Set to `on` to enable debugging") |
33 | 39 |
|
34 | | -ALLOWED_HOSTS: list[str] = env.list("ALLOWED_HOSTS", default=[]) |
35 | | -INTERNAL_IPS = env.list("INTERNAL_IPS", default=["127.0.0.1"]) |
| 40 | +ALLOWED_HOSTS = env.list( |
| 41 | + "ALLOWED_HOSTS", |
| 42 | + default=[], |
| 43 | + help_text="List of allowed hosts (e.g., `127.0.0.1,example.com`), see " |
| 44 | + "https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts for more information", |
| 45 | +) |
| 46 | + |
| 47 | +INTERNAL_IPS = env.list( |
| 48 | + "INTERNAL_IPS", |
| 49 | + default=["127.0.0.1"], |
| 50 | + initial="127.0.0.1,0.0.0.0", |
| 51 | + help_text="IPs that are allowed to use debug() (e.g., `127.0.0.1,example.com`), see " |
| 52 | + "https://docs.djangoproject.com/en/dev/ref/settings/#internal-ips for more information", |
| 53 | +) |
36 | 54 |
|
37 | 55 | # Get the IP to use for Django Debug Toolbar when developing with docker |
38 | | -if env.bool("USE_DOCKER", default=False) is True: |
| 56 | +if ( |
| 57 | + env.bool( |
| 58 | + "USE_DOCKER", default=False, help_text="Boolean used to add docker's internal ip to the `INTERNAL_IPS` setting" |
| 59 | + ) |
| 60 | + is True |
| 61 | +): |
39 | 62 | ip = socket.gethostbyname(socket.gethostname()) |
40 | 63 | INTERNAL_IPS += [ip[:-1] + "1"] |
41 | 64 |
|
|
56 | 79 | "allauth.account", |
57 | 80 | "crispy_forms", |
58 | 81 | "crispy_bootstrap5", |
| 82 | + "django_envtools", |
59 | 83 | "storages", |
60 | 84 | ] |
61 | 85 |
|
|
92 | 116 | }, |
93 | 117 | ] |
94 | 118 |
|
95 | | -WSGI_APPLICATION = env("WSGI_APPLICATION", default="config.wsgi.application") |
96 | | -DB_SSL_REQUIRED = env.bool("DB_SSL_REQUIRED", default=not DEBUG) |
| 119 | +WSGI_APPLICATION = env.str( |
| 120 | + "WSGI_APPLICATION", |
| 121 | + default="config.wsgi.application", |
| 122 | + help_text="WSGI application callable, see https://docs.djangoproject.com/en/dev/ref/settings/#wsgi-application for " |
| 123 | + "more information", |
| 124 | +) |
| 125 | +DB_SSL_REQUIRED = env.bool( |
| 126 | + "DB_SSL_REQUIRED", |
| 127 | + default=not DEBUG, |
| 128 | + help_text="Set to `on` to require SSL for database connections, default is `off` when DEBUG is `on`", |
| 129 | +) |
97 | 130 |
|
98 | | -# Database |
99 | | -# See https://github.com/jacobian/dj-database-url for more examples |
100 | 131 | DATABASES = { |
101 | 132 | "default": env.dj_db_url( |
102 | | - "DATABASE_URL", default="postgres://postgres@postgres/postgres", ssl_require=DB_SSL_REQUIRED |
| 133 | + "DATABASE_URL", |
| 134 | + default="postgres://postgres@postgres/postgres", |
| 135 | + ssl_require=DB_SSL_REQUIRED, |
| 136 | + initial="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}", |
| 137 | + help_text="Database URL for the default database, see https://github.com/jacobian/dj-database-url for " |
| 138 | + "more examples", |
103 | 139 | ) |
104 | 140 | } |
105 | 141 |
|
106 | 142 | # Custom User Model |
107 | | -# https://docs.djangoproject.com/en/4.2/topics/auth/customizing/#substituting-a-custom-user-model |
| 143 | +# https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a-custom-user-model |
108 | 144 | AUTH_USER_MODEL = "accounts.User" |
109 | 145 |
|
110 | 146 | # Password validation |
111 | | -# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators |
| 147 | +# https://docs.djangoproject.com/en/dev/ref/settings/#auth-password-validators |
112 | 148 |
|
113 | 149 | AUTH_PASSWORD_VALIDATORS = [ |
114 | 150 | { |
|
127 | 163 |
|
128 | 164 |
|
129 | 165 | # Internationalization |
130 | | -# https://docs.djangoproject.com/en/4.2/topics/i18n/ |
| 166 | +# https://docs.djangoproject.com/en/dev/topics/i18n/ |
131 | 167 |
|
132 | 168 | LANGUAGE_CODE = "en-us" |
133 | 169 |
|
|
140 | 176 |
|
141 | 177 |
|
142 | 178 | # Static files (CSS, JavaScript, Images) |
143 | | -# https://docs.djangoproject.com/en/4.2/howto/static-files/ |
| 179 | +# https://docs.djangoproject.com/en/dev/howto/static-files/ |
144 | 180 |
|
145 | 181 | STATICFILES_FINDERS = ( |
146 | 182 | "django.contrib.staticfiles.finders.FileSystemFinder", |
147 | 183 | "django.contrib.staticfiles.finders.AppDirectoriesFinder", |
148 | 184 | ) |
149 | 185 |
|
| 186 | +DEFAULT_FILE_STORAGE = env.str( |
| 187 | + "DEFAULT_FILE_STORAGE", |
| 188 | + default="django.core.files.storage.FileSystemStorage", |
| 189 | + help_text="Default file storage backend, see https://docs.djangoproject.com/en/dev/ref/settings/#storages for " |
| 190 | + "more information", |
| 191 | +) |
| 192 | +STATICFILES_STORAGE = env.str( |
| 193 | + "DEFAULT_FILE_STORAGE", |
| 194 | + default="django.core.files.storage.FileSystemStorage", |
| 195 | + help_text="Default file storage for staticfiles, see https://docs.djangoproject.com/en/dev/ref/settings/#storages " |
| 196 | + "for more information", |
| 197 | +) |
150 | 198 | STORAGES = { |
151 | 199 | "default": { |
152 | | - "BACKEND": env("DEFAULT_FILE_STORAGE", default="django.core.files.storage.FileSystemStorage"), |
| 200 | + "BACKEND": DEFAULT_FILE_STORAGE, |
153 | 201 | }, |
154 | 202 | "staticfiles": { |
155 | | - "BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage", |
| 203 | + "BACKEND": STATICFILES_STORAGE, |
156 | 204 | }, |
157 | 205 | } |
158 | 206 |
|
159 | 207 |
|
160 | 208 | if STORAGES["default"]["BACKEND"].endswith("MediaS3Storage") is True: |
161 | | - STORAGES["staticfiles"]["BACKEND"] = env("STATICFILES_STORAGE") |
162 | | - AWS_ACCESS_KEY_ID = env("AWS_ACCESS_KEY_ID") |
163 | | - AWS_SECRET_ACCESS_KEY = env("AWS_SECRET_ACCESS_KEY") |
164 | | - AWS_STORAGE_BUCKET_NAME = env("AWS_STORAGE_BUCKET_NAME") |
| 209 | + STORAGES["staticfiles"]["BACKEND"] = STATICFILES_STORAGE |
| 210 | + AWS_ACCESS_KEY_ID = env.str("AWS_ACCESS_KEY_ID", help_text="AWS Access Key ID for S3 storage") |
| 211 | + AWS_SECRET_ACCESS_KEY = env.str("AWS_SECRET_ACCESS_KEY", help_text="AWS Secret Access Key for S3 storage") |
| 212 | + AWS_STORAGE_BUCKET_NAME = env.str("AWS_STORAGE_BUCKET_NAME", help_text="AWS S3 Bucket Name for storage") |
165 | 213 | AWS_DEFAULT_ACL = "public-read" |
166 | | - AWS_S3_REGION = env("AWS_S3_REGION", default="us-east-2") |
| 214 | + AWS_S3_REGION = env.str("AWS_S3_REGION", default="us-east-1", help_text="AWS S3 Region for storage") |
167 | 215 | AWS_S3_CUSTOM_DOMAIN = f"s3.{AWS_S3_REGION}.amazonaws.com/{AWS_STORAGE_BUCKET_NAME}" |
168 | 216 | AWS_S3_OBJECT_PARAMETERS = {"CacheControl": "max-age=86400"} |
169 | 217 | STATIC_URL = f"https://{AWS_S3_CUSTOM_DOMAIN}/static/" |
|
181 | 229 | STATIC_URL = "/public/static/" |
182 | 230 |
|
183 | 231 | # Default primary key field type |
184 | | -# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field |
| 232 | +# https://docs.djangoproject.com/en/dev/ref/settings/#default-auto-field |
185 | 233 | DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" |
186 | 234 |
|
187 | 235 | # CACHE SETTINGS |
188 | | -# Redis scheme docs: https://redis-py.readthedocs.io/en/stable/connections.html#redis.connection.ConnectionPool.from_url |
189 | | -REDIS_URL = env("REDIS_URL", "redis://redis:6379/0") |
190 | | -REDIS_PREFIX = env("REDIS_PREFIX", default="") |
| 236 | +REDIS_URL = env.str( |
| 237 | + "REDIS_URL", |
| 238 | + default="redis://redis:6379/0", |
| 239 | + help_text="URL used to connect to Redis, see https://docs.djangoproject.com/en/dev/ref/settings/#location for " |
| 240 | + "more information", |
| 241 | +) |
| 242 | +REDIS_PREFIX = env.str( |
| 243 | + "REDIS_PREFIX", |
| 244 | + default="", |
| 245 | + help_text="Prefix for all Redis keys, useful to avoid key collisions in shared Redis instances", |
| 246 | +) |
191 | 247 | CACHES = { |
192 | 248 | "default": { |
193 | 249 | "BACKEND": "django.core.cache.backends.redis.RedisCache", |
|
232 | 288 | ACCOUNT_SIGNUP_OPEN = False |
233 | 289 | ACCOUNT_SHOW_POST_LOGIN_MESSAGE = False |
234 | 290 |
|
235 | | -# See https://github.com/migonzalvar/dj-email-url for more examples on how to set the EMAIL_URL |
236 | 291 | email = env.dj_email_url( |
237 | 292 | "EMAIL_URL", |
238 | 293 | default="smtp://[email protected]:[email protected]:587/?ssl=True&_default_from_email=President%20Skroob%20%[email protected]%3E", |
| 294 | + help_text="Email URL for sending emails, see https://github.com/migonzalvar/dj-email-url for more examples", |
239 | 295 | ) |
240 | 296 | DEFAULT_FROM_EMAIL = email["DEFAULT_FROM_EMAIL"] |
241 | 297 | EMAIL_HOST = email["EMAIL_HOST"] |
@@ -266,7 +322,7 @@ def log_format() -> str: |
266 | 322 |
|
267 | 323 |
|
268 | 324 | log_level = "WARNING" |
269 | | -IS_DEBUG_LOGGING_ON = env.bool("IS_DEBUG_LOGGING_ON", default=False) |
| 325 | +IS_DEBUG_LOGGING_ON = env.bool("IS_DEBUG_LOGGING_ON", default=False, help_text="Set to `on` to enable debug logging") |
270 | 326 | if IS_DEBUG_LOGGING_ON is True: |
271 | 327 | log_level = "DEBUG" |
272 | 328 |
|
@@ -305,4 +361,6 @@ def log_format() -> str: |
305 | 361 | MAINTENANCE_MODE_STATE_BACKEND = "maintenance_mode.backends.CacheBackend" |
306 | 362 | MAINTENANCE_MODE_STATE_BACKEND_FALLBACK_VALUE = True |
307 | 363 |
|
308 | | -VITE_DEV_MODE = env.bool("VITE_DEV_MODE", default=DEBUG) |
| 364 | +VITE_DEV_MODE = env.bool( |
| 365 | + "VITE_DEV_MODE", default=DEBUG, help_text="Set to `on` to enable Vite development mode for HMR in the browser" |
| 366 | +) |
0 commit comments