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

IDPF-92 Added redis, updated config files, added support for dbt-platform #4

Merged
merged 7 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ APP_ENV=local
DJANGO_SETTINGS_MODULE=config.settings.local
SECRET_KEY=this_is_an_example_use_a_proper_key_in_production
DATABASE_URL=postgres://postgres:postgres@postgres/postgres
REDIS_URL=redis://redis:6379
IDENTITY_REDIS_URL=redis://redis:6379
IDENTITY_REDIS=redis://redis:6379
ALLOWED_HOSTS=localhost

# Vite
VITE_DEV=True
Expand Down
140 changes: 75 additions & 65 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,59 @@

from pathlib import Path

import dj_database_url
import environ
import sentry_sdk
from dbt_copilot_python.database import database_url_from_env
from dbt_copilot_python.network import is_copilot, setup_allowed_hosts
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.integrations.redis import RedisIntegration


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


env = environ.Env()
env: environ.Env = environ.Env()
environ.Env.read_env(BASE_DIR / ".env")

APP_ENV = env("APP_ENV")

SECRET_KEY = env("SECRET_KEY")

DEBUG = False

ALLOWED_HOSTS = []

APP_ENV: str = env.str("APP_ENV")
GIT_COMMIT: str = env.str("GIT_COMMIT", None)
# Django
# https://docs.djangoproject.com/en/5.1/topics/settings/
SECRET_KEY: str = env.str("SECRET_KEY")
DEBUG: bool = False
ALLOWED_HOSTS: list[str] = setup_allowed_hosts(env.list("ALLOWED_HOSTS", default=[]))
ROOT_URLCONF: str = "config.urls"
WSGI_APPLICATION: str = "config.wsgi.application"
# Redis
#
IDENTITY_REDIS_URL: str = env.str("IDENTITY_REDIS_URL", None)
IDENTITY_REDIS: str = env.str("IDENTITY_REDIS", None)
# Sentry
# https://docs.sentry.io/platforms/python/integrations/django/
SENTRY_DSN: str = env.str("SENTRY_DSN")
SENTRY_ENABLE_TRACING: bool = env.bool("SENTRY_ENABLE_TRACING", False)
SENTRRY_TRACES_SAMPLE_RATE: float = env.float("SENTRY_TRACES_SAMPLE_RATE", 0.0)
DATABASE_URL: str = env.str("DATABASE_URL")
# Vite
# https://vitejs.dev/guide/backend-integration.html
VITE_DEV: bool = env.bool("VITE_DEV")
VITE_DEV_SERVER_URL: str = env.str("VITE_DEV_SERVER_URL")
VITE_MANIFEST_PATH: str = BASE_DIR / "frontend" / "dist" / "manifest.json"
# Internationalization
# https://docs.djangoproject.com/en/5.1/topics/i18n/
LANGUAGE_CODE: str = "en-gb"
TIME_ZONE: str = "UTC"
USE_I18N: bool = True
USE_TZ: bool = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.1/howto/static-files/
STATIC_URL: str = "static/"
# Default primary key field type
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD: str = "django.db.models.BigAutoField"

# Application definition

INSTALLED_APPS = [
INSTALLED_APPS: list[str] = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
Expand All @@ -46,7 +75,7 @@
"core.apps.CoreConfig",
]

MIDDLEWARE = [
MIDDLEWARE: list[str] = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
Expand All @@ -56,9 +85,7 @@
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = "config.urls"

TEMPLATES = [
TEMPLATES: list[dict[str]] = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
Expand All @@ -74,43 +101,49 @@
},
]

WSGI_APPLICATION = "config.wsgi.application"

# Sentry


def init_sentry():
sentry_environment = env("APP_ENV")

if SENTRY_DSN:
sentry_sdk.init(
dsn=env("SENTRY_DSN"),
environment=sentry_environment,
integrations=[
DjangoIntegration(),
],
enable_tracing=env.bool("SENTRY_ENABLE_TRACING", False),
traces_sample_rate=env.float("SENTRY_TRACES_SAMPLE_RATE", 0.0),
dsn=SENTRY_DSN,
environment=APP_ENV,
release=GIT_COMMIT,
integrations=[DjangoIntegration(), RedisIntegration()],
enable_tracing=SENTRY_ENABLE_TRACING,
traces_sample_rate=SENTRRY_TRACES_SAMPLE_RATE,
send_default_pii=True,
)


# Database
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases

DATABASES = {
"default": env.db(),
if is_copilot():
DATABASES: dict[str] = {
"default": dj_database_url.config(
default=database_url_from_env("DATABASE_CREDENTIALS")
)
}
else:
DATABASES: dict[str] = {"default": env.db()}

# Redis
if is_copilot():
IDENTITY_REDIS_URL = IDENTITY_REDIS_URL
else:
IDENTITY_REDIS_URL = IDENTITY_REDIS

CACHES: dict[str] = {
"default": {
"BACKEND": "django.core.cache.backends.redis.RedisCache",
"LOCATION": IDENTITY_REDIS_URL,
"KEY_PREFIX": "wp_",
}
}

# Vite
# https://vitejs.dev/guide/backend-integration.html

VITE_DEV = env.bool("VITE_DEV")
VITE_DEV_SERVER_URL = env("VITE_DEV_SERVER_URL")
VITE_MANIFEST_PATH = BASE_DIR / "frontend" / "dist" / "manifest.json"

# Password validation
# https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
AUTH_PASSWORD_VALIDATORS: dict[str, str] = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
Expand All @@ -124,26 +157,3 @@ def init_sentry():
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
},
]


# Internationalization
# https://docs.djangoproject.com/en/5.1/topics/i18n/

LANGUAGE_CODE = "en-gb"

TIME_ZONE = "UTC"

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.1/howto/static-files/

STATIC_URL = "static/"

# Default primary key field type
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
8 changes: 2 additions & 6 deletions config/settings/local.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
from .base import *


DEBUG = True

ALLOWED_HOSTS = ["localhost"]

DEBUG: bool = True
ALLOWED_HOSTS: list[str] = ["localhost"]
INSTALLED_APPS.append(
"django_extensions",
)

init_sentry()
21 changes: 21 additions & 0 deletions config/settings/prod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from .base import * # noqa


DEBUG: bool = False

AWS_S3_URL_PROTOCOL: str = "https:"
AWS_S3_CUSTOM_DOMAIN: str = env.str("AWS_S3_CUSTOM_DOMAIN") # noqa F405
AWS_QUERYSTRING_AUTH: bool = False

SESSION_COOKIE_AGE: int = 60 * 60

SECURE_BROWSER_XSS_FILTER: bool = True
X_FRAME_OPTIONS: str = "DENY"
SECURE_CONTENT_TYPE_NOSNIFF: bool = True
SECURE_HSTS_SECONDS: int = 15768000
SECURE_HSTS_INCLUDE_SUBDOMAINS: bool = True
SECURE_HSTS_PRELOAD: bool = True
SECURE_SSL_REDIRECT: bool = True
CSRF_COOKIE_SECURE: bool = True
SESSION_COOKIE_SECURE: bool = True
SECURE_PROXY_SSL_HEADER: tuple[str, str] = ("HTTP_X_FORWARDED_PROTO", "https")
2 changes: 1 addition & 1 deletion manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def main():
"""Run administrative tasks."""
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.prod")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
Expand Down
Loading