Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce django-environ #136

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies = [
"django-celery-beat>=2.6.0",
"django-celery-results>=2.5.1",
"django-crispy-forms>=2.1",
"django-environ>=0.11.2",
artsyjian marked this conversation as resolved.
Show resolved Hide resolved
"django-handyhelpers>=0.3.22",
"django-markdownify>=0.9.3",
"django-storages[azure]>=1.14.2",
Expand All @@ -36,6 +37,7 @@ dependencies = [
[project.optional-dependencies]
dev = [
"django-debug-toolbar>=4.3",
"django-environ>=0.11.2",
"freezegun>=1.4",
"mkdocs>=1.6",
"mkdocs-material[imaging]>=9.5",
Expand Down
11 changes: 11 additions & 0 deletions requirements.dev.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ asgiref==3.8.1
# via
# spokanetech (pyproject.toml)
# django
async-timeout==4.0.3
# via
# aiohttp
# redis
attrs==24.2.0
# via aiohttp
azure-common==1.1.28
Expand Down Expand Up @@ -129,6 +133,8 @@ django-crispy-forms==2.3
# crispy-bootstrap5
django-debug-toolbar==4.4.6
# via spokanetech (pyproject.toml)
django-environ==0.11.2
# via spokanetech (pyproject.toml)
django-filter==24.3
# via djangorestframework-filters
django-handyhelpers==0.3.26
Expand All @@ -149,6 +155,8 @@ drf-dynamic-fields==0.4.0
# via django-handyhelpers
eventbrite==3.3.5
# via spokanetech (pyproject.toml)
exceptiongroup==1.2.2
# via pytest
flower==2.0.1
# via spokanetech (pyproject.toml)
freezegun==1.5.1
Expand Down Expand Up @@ -349,10 +357,13 @@ tinycss2==1.2.1
# bleach
# cairosvg
# cssselect2
tomli==2.0.1
# via pytest
tornado==6.4.1
# via flower
typing-extensions==4.12.2
# via
# asgiref
# azure-core
# azure-storage-blob
# beautifulsoup4
Expand Down
7 changes: 7 additions & 0 deletions requirements.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ asgiref==3.8.1
# via
# spokanetech (pyproject.toml)
# django
async-timeout==4.0.3
# via
# aiohttp
# redis
attrs==24.2.0
# via aiohttp
azure-common==1.1.28
Expand Down Expand Up @@ -108,6 +112,8 @@ django-crispy-forms==2.3
# via
# spokanetech (pyproject.toml)
# crispy-bootstrap5
django-environ==0.11.2
# via spokanetech (pyproject.toml)
django-filter==24.3
# via djangorestframework-filters
django-handyhelpers==0.3.26
Expand Down Expand Up @@ -242,6 +248,7 @@ tornado==6.4.1
# via flower
typing-extensions==4.12.2
# via
# asgiref
# azure-core
# azure-storage-blob
# beautifulsoup4
Expand Down
49 changes: 30 additions & 19 deletions src/spokanetech/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,23 @@
import os
from pathlib import Path

import environ
import dj_database_url
import sentry_sdk
from django.urls import reverse_lazy
from dotenv import load_dotenv

load_dotenv()

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# Take environment variables from an env file
ENV_PATH = os.environ.get("ENV_PATH", f"{BASE_DIR.parent}/envs/.env")
env = environ.Env()
if os.path.exists(ENV_PATH):
print(f"Loading ENV vars from {ENV_PATH}")
environ.Env.read_env(ENV_PATH)
else:
print("NO ENV_PATH found!")

ADMINS = [
("Organizers", "[email protected]"),
Expand All @@ -31,7 +38,8 @@
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/

IS_DEVELOPMENT = os.environ.get("SPOKANE_TECH_DEV", "false") == "true"
IS_DEVELOPMENT = env.bool("SPOKANE_TECH_DEV", default=False)

if IS_DEVELOPMENT:
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "django-insecure-t9*!4^fdn*=pmz4%8u_we!88e!8@_!drx0)u_@6$@!nx$4svjp" # nosec: Development-only key.
Expand All @@ -47,12 +55,15 @@
]
else:
try:
SECRET_KEY = os.environ["DJANGO_SECRET_KEY"]
SECRET_KEY = env.str("DJANGO_SECRET_KEY")
except KeyError as e:
raise KeyError(f"{e}: If running in development, set 'SPOKANE_TECH_DEV' to any value.") from e

DEBUG = False
ALLOWED_HOSTS = os.environ.get("ALLOWED_HOSTS", "spokanetech.org,spokanetech-py.fly.dev").split(",")
ALLOWED_HOSTS = env.str(
"ALLOWED_HOSTS",
default="spokanetech.org,spokanetech-py.fly.dev"
).split(",")
CSRF_TRUSTED_ORIGINS = [f"https://{host}" for host in ALLOWED_HOSTS]

# SSL Options
Expand All @@ -64,7 +75,7 @@
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True

if sentry_dsn := os.environ.get("SENTRY_DSN"):
if sentry_dsn := env.str("SENTRY_DSN"):
sentry_sdk.init(
dsn=sentry_dsn,
traces_sample_rate=1.0,
Expand Down Expand Up @@ -194,7 +205,7 @@
]

# Storages
USE_AZURE = os.environ["USE_AZURE"] == "true" if "USE_AZURE" in os.environ else not DEBUG
USE_AZURE = env.bool("USE_AZURE") if env("USE_AZURE") != "" else not DEBUG
if USE_AZURE:
DEFAULT_FILE_STORAGE = "spokanetech.backend.AzureMediaStorage"
STATICFILES_STORAGE = "spokanetech.backend.AzureStaticStorage"
Expand All @@ -203,11 +214,11 @@
MEDIA_LOCATION = "media"

AZURE_URL_EXPIRATION_SECS = None
AZURE_ACCOUNT_NAME = os.environ["AZURE_ACCOUNT_NAME"]
AZURE_ACCOUNT_KEY = os.environ["AZURE_ACCOUNT_KEY"]
AZURE_CUSTOM_DOMAIN = os.environ.get(
AZURE_ACCOUNT_NAME = env.str("AZURE_ACCOUNT_NAME")
AZURE_ACCOUNT_KEY = env.str("AZURE_ACCOUNT_KEY")
AZURE_CUSTOM_DOMAIN = env.str(
"AZURE_CDN_DOMAIN",
f"{AZURE_ACCOUNT_NAME}.blob.core.windows.net",
default=f"{AZURE_ACCOUNT_NAME}.blob.core.windows.net",
)
STATIC_URL = f"https://{AZURE_CUSTOM_DOMAIN}/{STATIC_LOCATION}/"
MEDIA_URL = f"https://{AZURE_CUSTOM_DOMAIN}/{MEDIA_LOCATION}/"
Expand All @@ -231,15 +242,15 @@

# Celery
try:
CELERY_BROKER_URL = os.environ["CELERY_BROKER_URL"]
CELERY_BROKER_URL = env.str("CELERY_BROKER_URL")
CELERY_ENABLED = True
except KeyError:
if IS_DEVELOPMENT:
CELERY_ENABLED = False
else:
raise

CELERY_RESULT_BACKEND = os.environ.get("CELERY_RESULT_BACKEND", "django-db")
CELERY_RESULT_BACKEND = env.str("CELERY_RESULT_BACKEND", default="django-db")
CELERY_RESULT_EXTENDED = True
CELERY_ACCEPT_CONTENT = ["application/json"]
CELERY_TASK_SERIALIZER = "json"
Expand All @@ -251,11 +262,11 @@


# Discord
DISCORD_WEBHOOK_URL = os.environ["DISCORD_WEBHOOK_URL"]
DISCORD_WEBHOOK_URL = env.str("DISCORD_WEBHOOK_URL")


# Eventbrite
EVENTBRITE_API_TOKEN = os.environ["EVENTBRITE_API_TOKEN"]
EVENTBRITE_API_TOKEN = env.str("EVENTBRITE_API_TOKEN")


# Markdownify
Expand Down Expand Up @@ -307,11 +318,11 @@


# Email
DEFAULT_FROM_EMAIL = os.environ.get("DEFAULT_FROM_EMAIL", "[email protected]")
SERVER_EMAIL = os.environ.get("SERVER_EMAIL", DEFAULT_FROM_EMAIL)
DEFAULT_FROM_EMAIL = env.str("DEFAULT_FROM_EMAIL", default="[email protected]")
SERVER_EMAIL = env.str("SERVER_EMAIL", default=DEFAULT_FROM_EMAIL)

if USE_AZURE:
EMAIL_BACKEND = "django_azure_communication_email.EmailBackend"
AZURE_COMMUNICATION_CONNECTION_STRING = os.environ["AZURE_COMMUNICATION_CONNECTION_STRING"]
AZURE_COMMUNICATION_CONNECTION_STRING = env.str("AZURE_COMMUNICATION_CONNECTION_STRING")
elif DEBUG:
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
Loading