Skip to content

Commit

Permalink
introduce django-environ
Browse files Browse the repository at this point in the history
  • Loading branch information
artsyjian committed Sep 2, 2024
1 parent ce6b0df commit e2a1ed4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies = [
"django-celery-beat>=2.6.0",
"django-celery-results>=2.5.1",
"django-crispy-forms>=2.1",
"django-environ>=0.11.2",
"django-handyhelpers>=0.3.22",
"django-markdownify>=0.9.3",
"django-storages[azure]>=1.14.2",
Expand Down
47 changes: 29 additions & 18 deletions src/spokanetech/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,22 @@
import os
from pathlib import Path

import environ
import dj_database_url
import sentry_sdk
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", "organizers@spokanetech.org"),
Expand All @@ -30,7 +37,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 @@ -46,12 +54,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 @@ -63,7 +74,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 @@ -182,7 +193,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 @@ -191,11 +202,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 @@ -219,15 +230,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 @@ -239,11 +250,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 @@ -295,9 +306,9 @@


# Email
DEFAULT_FROM_EMAIL = os.environ.get("DEFAULT_FROM_EMAIL", "DoNotReply@spokanetech.org")
SERVER_EMAIL = os.environ.get("SERVER_EMAIL", DEFAULT_FROM_EMAIL)
DEFAULT_FROM_EMAIL = env.str("DEFAULT_FROM_EMAIL", default="DoNotReply@spokanetech.org")
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")

0 comments on commit e2a1ed4

Please sign in to comment.